 |
Add-VMSnapshotCount |
-
-
-
-
-
-
-
-
- $VCServerName = "MYVCSERVER"
- $CustomFieldName = "Snapshots"
- $ManagedObjectType = "VirtualMachine"
-
-
- $VC = Connect-VIServer $VCServerName
- $SI = Get-View ServiceInstance
- $CFM = Get-View $SI.Content.CustomFieldsManager
-
- $myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName}
- If (!$myCustomField)
- {
-
- $FieldCopy = $CFM.Field[0]
- $CFM.AddCustomFieldDef($CustomFieldName,$ManagedObjectType,$FieldCopy.FieldDefPrivileges,$FieldCopy.FieldInstancePrivileges)
- }
-
-
- $VMs = Get-VM
- ForEach ($VM in $VMs)
- {
- $SnapshotCount = (Get-Snapshot -VM $VM | Measure-Object).Count
-
- $VMView = $VM | Get-View
- If ($SnapShotCount -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value)
- {
-
- $VMView.setCustomValue($CustomFieldName,$SnapShotCount)
- }
- }
- Disconnect-VIServer -Confirm:$False