forked from dstamen/PowerCLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-vCenterPermissions.ps1
38 lines (31 loc) · 1.37 KB
/
Set-vCenterPermissions.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# PowerCLI Script for setting vCenter Permissions base don AD Group and Datacenter
# @davidstamen
# http://davidstamen.com
param
(
[Parameter(Mandatory=$False,
HelpMessage='Path to CSV to Import')]
[string[]]$permissionlist
)
#Import PowerCLI Module
Get-Module -ListAvailable VMware* | Import-Module
#Prompt for vCenter to Set Permissions
$vCenter = Read-Host -Prompt "Name or IP address of vcenter"
#Makes sure CSV was passed as parameter, if not it prompts and checks path.
If($permissionlist -eq $NULL){
$permissionlist = Read-host -Prompt "Csv to import"
}
If((Test-Path $permissionlist) -eq $False){Write-host "Could not find CSV.";break}
#tries to connect to vcenter and breaks script if it fails.
Try{Connect-viserver $vCenter -ErrorAction "Stop"|Out-Null}
Catch {Write-Warning "Unable to Logon to $vCenter. Exiting...";break}
#define variables and loop through each entry setting permissions
$permission = Import-csv "$permissionlist"
foreach ($item in $permission) {
$datacenter=$item.datacenter
$role=$item.role
$group=$item.group
$domain=$item.domain
Write-host "Setting $group permissions to $role on $vCenter" -ForegroundColor Green
$Permission=New-VIPermission -Entity (Get-Datacenter $datacenter) -Principal (Get-VIAccount -Domain $domain -Group|Where-Object {$_.Name -like "*$group*"}) -Role (Get-VIRole $role) -Propagate:$true|Out-Null
}