Advertisement
Get-Ryan

[PowerCLI] Get-VMReport

Aug 12th, 2016
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-VMReport {
  2.  
  3.     [cmdletbinding()]
  4.  
  5.     param(
  6.         [parameter(Position=0)]
  7.         [String]
  8.         $VMname,
  9.  
  10.         [parameter(Position=1)]
  11.         [String]
  12.         $VIServerName = "<DefaultVIServerName>"
  13.          )
  14.  
  15.     If(!$global:DefaultVIServer){
  16.         Connect-VIServer $VIServerName
  17.     }
  18.  
  19.     If($global:DefaultVIServer.Name -notlike $VIServerName){
  20.         Disconnect-VIServer -Server $global:DefaultVIServer
  21.         Connect-VIServer $VIServerName
  22.     }
  23.    
  24.     If($VMname){
  25.         $VMList = Get-VM -Name $VMname
  26.     }
  27.     Else{$VMList = Get-VM}
  28.  
  29.     $vmarray = New-Object -TypeName System.Collections.ArrayList
  30.    
  31.     ForEach($VM in $VMList){
  32.        
  33.         $disks = $VM | Get-HardDisk
  34.        
  35.         ForEach($disk in $disks){
  36.  
  37.             $guestdisk = $null
  38.  
  39.             ForEach($guest in $VM.Guest.Disks){
  40.  
  41.                 If([math]::Abs(($disk.CapacityGB - $guest.CapacityGB)) -lt 2){
  42.                     $guestdisk = $guest
  43.                 }
  44.  
  45.             }
  46.  
  47.             $temp = [pscustomobject] @{'Host' = $VM.VMHost.Name.Split('.')[0]
  48.                                        'Name' = $VM.Name
  49.                                        'vCPUs' = $VM.NumCpu
  50.                                        'Memory' = $VM.MemoryGB
  51.                                        'OS Disk Name' = $guestdisk.Path
  52.                                        'Disk Lun' = $disk.Filename.Split(' ')[0]
  53.                                        'Disk Size (GB)' = $disk.CapacityGB
  54.                                        'Free Space (GB)' = [math]::Round($guestdisk.FreeSpaceGB,2)
  55.                                        'Disk File' = $disk.Filename.Split(' ')[1]}
  56.  
  57.        
  58.             $vmarray.Add($temp) | Out-Null
  59.             Write-Progress -Activity "Processing VMs..." -Status "Objects processed: $($vmarray.Count)"
  60.  
  61.         }
  62.  
  63.     }
  64.    
  65.     $vmarray | Sort-Object Host, Name | Out-GridView -Title "Host/VM Distribution" -OutputMode None
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement