I'm using a powercli script query VMs as soon as they are provisioned but if two VMs are built in quick succession it can cause a conflict with the script credential file I use. The relevant section of the script is:
$creds = Get-VICredentialStoreItem -file C:\scripts\cred.xml
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password
So, the first iteration of this script grabs the cred.xml and seems to holds onto it until the script completes. Then if a second iteration launches and looks for the cred.xml it fails. The exact error I'm getting is:
The process cannot access the file 'C:\scripts\cred.xml' because it is being used by another process
So, my question is - is there a way to avoid this by adding a check at the start of the script perhaps to check if the file is locked? Then pause for 60 seconds....? Has anyone got something already I could reuse?