I have the following code
Connect-VIServer -Server <ServerName> -Protocol https
$VM1 = Get-VM
for($i = 0; $i -lt $VM1.Count; $i++) {
$datastore = Get-Datastore -VM (Get-VM -Name $VM1.SyncRoot[$i].Name)
Write-Host $datastore.Name
}
My problem is that sometimes the datastore is returned and others it is not. Either way there are no errors returned.