The company I work for uses the vSphere API for all kinds of purposes. Usually this works great however one of our programs returned some errors after we moved a large number of VMs from one datastore to another.
It turns out that from vSphere API 5.0 onwards the VirtualMachineStorageInfo object is not always updated properly. The vSphere Web Services API documentation shows the following information about this issue.
Storage space used by the virtual machine, split by datastore. Can be explicitly refreshed by the RefreshStorageInfo operation. In releases after vSphere API 5.0, vSphere Servers might not generate property collector update notifications for this property. To obtain the latest value of the property, you can use PropertyCollector methods RetrievePropertiesEx or WaitForUpdatesEx. If you use the PropertyCollector.WaitForUpdatesEx method, specify an empty string for the version parameter. Any other version value will not produce any property values as no updates are generated.
To trigger the the RefreshStorageInfo operation that supposedly fixes the issue I wrote a simple PowerCLI function.
function Update-VMStorageInfo {
[cmdletbinding()] #This provides the function with the -Verbose and -Debug parameters
Param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]$Age
)
# Get current date and time
$StartDate = (Get-Date)
# Collect VM inventory
$VMs = get-view -ViewType "VirtualMachine" -Property @("name", "storage")
foreach ($VM in $VMs) {
$StorageInfoAge = (New-TimeSpan -Start $StartDate -End $VM.storage.timestamp).Hours
if ($StorageInfoAge -lt (-$Age)) {
write-host "VirtualMachineStorageInfo on" $VM.name "needs to be updated"
$VM.RefreshStorageInfo()
}
}
}
After you added this function to a PowerCLI session you can run it with an ‘Age’ parameter. This parameter determines the maximum age of the StorageInfo timestamp in hours. If the StorageInfo of a VM is older than the number of hours specified in the Age parameter it will be updated.
0 Comments