Add-VMHostVersion


posted by Richard Giles
02-27-2009

Downloads: 430
File size: 1.2kB
Views: 755

Embed
Add-VMHostVersion
  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. # Did you update all your ESX Servers to the latest version? Check it quickly using this script. 
  5. # Requires: VI Toolkit 
  6. ############################## 
  7.  
  8. # Variables 
  9. $VCServerName = "MYVCSERVER" 
  10. $CustomFieldName = "Version" 
  11. $ManagedObjectType = "HostSystem" 
  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. $VMHosts = Get-VMHost 
  28. $VMHostViews = $VMHosts | Get-View 
  29. ForEach ($VMHostView in $VMHostViews
  30.     
  31.     $Version = $VMHostView.Config.Product.FullName 
  32.     # Compare value to current value 
  33.     If ($Version -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value
  34.         
  35.         # Set Custom Value 
  36.         $VMHostView.setCustomValue($CustomFieldName,$Version
  37.         
  38.     
  39. Disconnect-VIServer -Confirm:$False 
Filed under: ,

Did you update all your ESX Servers to the latest version? Check it quickly using this script.

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.