Reload VIAccounts list with PowerCli to select a newly created group

active-directorygroupspermissionspowerclivmware-vcenter

I'm using a script that creates groups in Active Directory, and links them to my vCenter server using New-VIPermission.
The trick is that it doesn't find the groups created unless I restart the script (and so, I guess, the connexion).

$My_Group = New-QADGroup -Name $My_AD_GroupName -SamAccountName $My_AD_GroupName -ParentContainer $My_OU -Connection $My_adConnection 
New-VIPermission -Entity $param_objects -Principal "Domain\$My_AD_GroupName" -Role ($My_Role.Name) -Propagate:($My_Role.Value) | Out-Null

This script will create the group in my AD, but when coming to linking it to a new permission in my vCenter, it will return
New-VIPermission Could not find VIAccount with name 'Domain\MY_GROUP_NAME'.

When restarting the script, it will pass over the group creation directly to the New-VIPermission, and correctly link it to the vCenter object.

I've tried, to add a Wait command, and a 5 seconds sleep, but wont change a thing…

How can I manage to reload the list of availaible groups without restarting the vCenter connexion ?

Thanks in advance !

Best Answer

Got an answer from the vmware forums : cache refreshing was the issue. I added a while to test if the group could be resolved in vCenter to solve the issue :

while (!(Get-VIAccount -Group -Name "DOMAIN\$My_AD_GroupName")){Start-sleep -s 1}

Just after the AD group creation and... well it's slow, but it works :)

Related Topic