I am trying to add a domain user directly to the folder permissions for the root folder in vCenter with Admin permissions. I can get it to assign permissions for folders below the root but not at the top of the tree. See code below.
#Create a VIAccount object suitable for use with New-VIPermission, Get-VIPermission, etc. from PowerCLI.
function New-VIAccount($principal) {
$flags = `
http://System.Reflection.BindingFlags::NonPublic -bor
http://System.Reflection.BindingFlags::Public -bor
http://System.Reflection.BindingFlags::DeclaredOnly -bor
http://System.Reflection.BindingFlags::Instance
$method = $defaultviserver.GetType().GetMethods($flags) |
where { $_.Name -eq "VMware.VimAutomation.Types.VIObjectCore.get_Client" }
$client = $method.Invoke($global:DefaultVIServer, $null)
Write-Output `
(New-Object VMware.VimAutomation.Client20.PermissionManagement.VCUserAccountImpl `
-ArgumentList $principal, "", $client)
}
$svcaccount = $domain + "\" + $username
$account = New-VIAccount "$svcaccount"
#(Get-Folder -Name folder) | New-VIPermission -Role Admin -Principal $account -Propagate:$TRUE ### This adds the user to a folder named folder with no issues.
(Get-Folder -Name Datacenters -Id Folder-group-d1) | New-VIPermission -Role Admin -Principal $account -Propagate:$TRUE ### When I try to apply to the root folder I get the following error:
New-VIPermission : 12/24/2009 2:19:30 PM New-VIPermission 9C9AB6A7-E395-42D1-B944-516E1BBBCEF2 Object reference not set to an instance of an object.
At ...vCenterSettings.ps1:93 char:70+ (Get-Folder -Name Datacenters -Id Folder-group-d1) | New-VIPermission <<<< -Role Admin -Principal $account -Propagate:$TRUE