Add-VMRdmSize


posted by Richard Giles
02-27-2009

Downloads: 468
File size: 1.3kB
Views: 1,172

Embed
Add-VMRdmSize
  1. ############################## 
  2. # Script created by Hugo Peeters  
  3. # http://www.peetersonline.nl/index.php/vmware/add-rdm-size-info-to-vi-client-using-powershell/ 
  4. # Script that adds the total size of all Raw Device Mappings (RDMs) on a VM to a Custom Field in the VI Client. 
  5. # Requires: VI Toolkit 
  6. ############################## 
  7.  
  8. # Variables 
  9. $VCServerName = "MYVCSERVER" 
  10. $CustomFieldName = "RDM Size (GB)" 
  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.     $RDMSize = [math]::Round((($VM | Get-HardDisk | Where {$_.DiskType -ne "flat"} | Measure-Object CapacityKB -Sum).Sum * 1KB / 1GB),0) 
  31.     $VMView = $VM | Get-View 
  32.     # Compare value to current value 
  33.     If ($RDMSize -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value
  34.         
  35.         # Set Custom Value 
  36.         $VMView.setCustomValue($CustomFieldName,$RDMSize
  37.         
  38.     
  39. Disconnect-VIServer -Confirm:$False 

Script that adds the total size of all Raw Device Mappings (RDMs) on a VM to a Custom Field in the VI Client.

Attributed To: Hugo Peeters

URL: http://www.peetersonline.nl/index.php/vmware/add-rdm-size-info-to-vi-client-using-powershell/

Copyright 2012 PowerShell.com. All rights reserved.