Add-VMSnapshotCount


posted by Richard Giles
02-27-2009

Downloads: 420
File size: 1.2kB
Views: 955

Embed
Add-VMSnapshotCount
  1. ############################## 
  2. # Script created by Hugo Peeters  
  3. # http://www.peetersonline.nl/index.php/vmware/add-custom-fields-to-vi-client-with-powershell-samples/ 
  4. # Add Snapshot Count to a VI Client 
  5. # Requires: VI Toolkit 
  6. ############################## 
  7.  
  8. # Variables 
  9. $VCServerName = "MYVCSERVER" 
  10. $CustomFieldName = "Snapshots" 
  11. $ManagedObjectType = "VirtualMachine" 
  12.  
  13. # Script 
  14. $VC = Connect-VIServer $VCServerName 
  15. $SI = Get-View ServiceInstance 
  16. $CFM = Get-View $SI.Content.CustomFieldsManager 
  17.  
  18. $myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName
  19. If (!$myCustomField
  20.     
  21.     # Create Custom Field 
  22.     $FieldCopy = $CFM.Field[0] 
  23.     $CFM.AddCustomFieldDef($CustomFieldName,$ManagedObjectType,$FieldCopy.FieldDefPrivileges,$FieldCopy.FieldInstancePrivileges
  24.     
  25.      
  26. # Fill Custom Fields 
  27. $VMs = Get-VM 
  28. ForEach ($VM in $VMs
  29.     
  30.     $SnapshotCount = (Get-Snapshot -VM $VM | Measure-Object).Count 
  31.     # Compare value to current value 
  32.     $VMView = $VM | Get-View 
  33.     If ($SnapShotCount -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value
  34.         
  35.         # Set Custom Value 
  36.         $VMView.setCustomValue($CustomFieldName,$SnapShotCount
  37.         
  38.     
  39. Disconnect-VIServer -Confirm:$False 
Filed under: ,

Add Snapshot Count to a VI Client

Attributed To: Hugo Peeters

URL:  http://www.peetersonline.nl/index.php/vmware/add-custom-fields-to-vi-client-with-powershell-samples/

Copyright 2012 PowerShell.com. All rights reserved.