As of late I’ve been working on a PowerCLI script to assist me with the deployment of around 50 or so ESXi hosts. For the most part the PowerCLI cmdlets have been pretty self explanatory and pretty simple to use. Really I just need to setup some DNS, Hostnames, Networks, Datastores, NTP settings, and permissions…you know, everything you would normally do when setting up a host. The problem I’ve ran into is when I hit that permissions section.
Essentially what I wanted to do was create a new role containing only those permissions that I wanted then simply assign an active directory group to that role. So the role part, not too bad, pretty simple really…I used the following…
1 2 3 4 5 |
New-VIRole -Name "Elevated VM User" -Privilege (Get-VIPrivilege -ID "VirtualMachine.Interact") Get-VIRole "Elevated VM User" | Set-VIRole -AddPrivilege (Get-VIPrivilege -ID "ScheduledTask.Run") Get-VIRole "Elevated VM User" | Set-VIRole -AddPrivilege (Get-VIPrivilege -ID "ScheduledTask.Create") Get-VIRole "Elevated VM User" | Set-VIRole -AddPrivilege (Get-VIPrivilege -Name "Power") Get-VIRole "Elevated VM User" | Set-VIRole -AddPrivilege (Get-VIPrivilege -Name "Maintenance") |
Basically this just creates my new role (Elevated VM User) and adds a handful of permissions to it. In order to find out the name for all the priveleges I simply just ran Get-VIPrivelege -Role ‘Admin’.
So now that I had my role created I just need to add an AD Account to that role. A lot of the documentation led me to the New-VIPermission cmdlet – sounds easy, but running the following New-VIPermission -Role ‘Elevated VM User’ -Principal ‘DOMAIN\Username’ produced nothing but the following errors for me….
New-VIPermission : 9/12/2012 2:47:40 PM New-VIPermission Could not find VIAccount with name ‘Domain\Username’.
At line:1 char:17
+ New-VIPermission <<<< -Role ‘Admin’ -Principal ‘Domain\Username’
+ CategoryInfo : ObjectNotFound: (Domain\Username:String) [New-VIPermission], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.PermissionManagement.NewVIPermission
New-VIPermission : 9/12/2012 2:47:40 PM New-VIPermission Value cannot be found for the mandatory parameter Entity
At line:1 char:17
+ New-VIPermission <<<< -Role ‘Admin’ -Principal ‘Domain\Username’
+ CategoryInfo : NotSpecified: (:) [New-VIPermission], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.PermissionManagement.NewVIPermission
Now it seams as if me passing the string of ‘Domain\Username’ is not satisfying whatever object it is that -Principal is looking for. So after a many googling and twittering the following code is how I ended up assigning Domain\Username to the role I created.
1 2 3 4 5 6 7 8 9 |
$groupName = "Domain\Username" $currhost = get-vmhost HOSTNAME | % {Get-View $_.Id} $authmgr = Get-View AuthorizationManager-ha-authmgr $perm = New-Object VMware.VIM.Permission $perm.Principal = $groupName $perm.group = $true $perm.propagate = $true $perm.RoleId = ($authmgr.RoleList | where {$_.Name -eq "Elevated VM User"}).RoleId $authmgr.SetEntityPermissions($currhost.parent,$perm) |
And this worked perfect! As I’ve always said I’m sure there are a million ways to do something and if you think your way is more efficient or better than mine, please share! This is my first crack at it so all suggestions and comments are always welcome 🙂