Guest User

Basic PowerCLI vCenter Report

a guest
Feb 19th, 2014
1,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Clear-Host
  2. Write-Host "
  3. ***********************************
  4. **   VMware Information Report   **
  5. ***********************************
  6. "
  7. if (Get-PSSnapin vmware* -ErrorAction SilentlyContinue) {
  8. Write-Host "vSphere snapin already loaded, proceeding."
  9. } else {
  10. Write-Host "Loading vSphere snapin."
  11. Add-pssnapin VMWare.VimAutomation.Core -ErrorAction SilentlyContinue
  12.     if (Get-PSSnapin vmware* -ErrorAction SilentlyContinue) {
  13.     Write-Host "vSphere snapin loaded"
  14.     } else {
  15.     Write-Host -ForegroundColor Red "Error loading vSphere snapin. Halting."
  16.     Write-Host -ForegroundColor Red "VMware PowerCLI is required to run this script."
  17.     break
  18.     }
  19.  }
  20. ""
  21.  
  22.  
  23. function ping-ip
  24.  {  
  25.   param( $ip )
  26.    trap {$false; continue}  
  27.    $timeout = 1000  
  28.    $object = New-Object system.Net.NetworkInformation.Ping  
  29.    (($object.Send($ip, $timeout)).Status -eq 'Success')
  30.  }
  31.  
  32.     $vcenter = Read-Host "Please enter the IP for your vCenter server or VMware host"
  33.     #$vcusername = Read-Host "Enter username"
  34.     #$vcpassword = Read-Host -AsSecureString "Enter password for $vcusername"
  35.     #$pw = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vcpassword))
  36.  
  37.     if (ping-ip($vcenter)){
  38.     Write-Host "Please wait while we attempt to connect to $vcenter"
  39.     Connect-VIServer -Server $vcenter <#-User $vcusername -Password $pw#> > $NULL 2>&1
  40.     if (!($vcusername)) { $vcusername = [Environment]::UserName }
  41.     Write-Host "Connected"
  42.     } else {
  43.     Write-Host -ForegroundColor Red "Halt: $vcenter is not reachable."
  44.     break
  45.     }
  46.  
  47.    
  48.  
  49. function Generate-Report {
  50.     Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal-left {text-align:left;}.SubTitle {background: #0077D4;color: #FFFFFF;text-align:center;}.Normal {text-align:right;}</style></head><body bgcolor=""cccccc"" text=""333333""><p class=""subtitle"">Report run on <a href=""https://$vcenter"" target=""_blank"">$vcenter</a> by $vcusername - $datefull</p><table><tr class=""Title""><td colspan=""5"">VMware Host Report</td></tr><tr><td>Cluster Name </td><td>Server</td><td>CPU Cores  </td><td>CPU Speed </td><td>Total Memory  </td><td>Version</td></tr>"
  51.                 Foreach ($hh in $hreport){
  52.                     Write-Output "<td class=""Normal-left"">$($hh.ClusterName)</td><td class=""Normal"">$($hh.Hostname)</td><td class=""Normal"">$($hh.CPUCores)</td><td class=""Normal"">$($hh.CPUSpeed) GHz</td><td class=""Normal"">$($hh.MemorySizeGB) GB</td><td class=""Normal"">$($hh.Version)</td></tr>"
  53.                 }
  54.                     Write-Output "</table><br /><table><tr class=""Title""><td colspan=""4"">VMware Datastore Report</td></tr><tr><td>Datastore  </td><td>Capacity  </td><td>Used  </td><td>% Free  </td></tr>"
  55.                 Foreach ($store in $dreport){
  56.                 if (([int]$($store.PercFree) -le "25") -and ([int]$($store.PercFree) -gt "11")) {$PercFree = "<font color=""yellow"">$($store.PercFree) "}
  57.                 elseif ([int]$($store.PercFree) -le "10") {$PercFree = "<font color=""red"">$($store.PercFree) "}
  58.                 else {$PercFree = "<font>$($store.PercFree)"}
  59.                     Write-Output "<td class=""Normal-left"">$($store.name)</td><td class=""Normal"">$($store.CapacityGB) GB</td><td class=""Normal"">$($store.UsedGB) GB</td><td class=""Normal"">$PercFree%</font></td></tr>"
  60.                 }
  61.                     Write-Output "</table><br /><table><tr class=""Title""><td colspan=""4"">VMware Snapshot Report</td></tr><tr><td>Server</td><td>Name</td><td>Created</td><td>Size</td></tr>"
  62.                 Foreach ($sp in $sreport){
  63.                     Write-Output "<td class=""Normal-left"">$($sp.vm)</td><td class=""Normal"">$($sp.name)</td><td class=""Normal"">$($sp.Created)</td><td class=""Normal"">$($sp.SizeGB) GB</td></tr>"
  64.                 }
  65.                     Write-Output "</table><br /><table><tr class=""Title""><td colspan=""3"">VMware Tools Report</td></tr><tr><td>Server  </td><td>Server Status </td><td>VM Tools Status</td></tr>"
  66.                 Foreach ($t in $treport){
  67.                        if ($($t.Tools) -eq "toolsOK") { $toolstatus = "<font color=""green"">Up to date</font>" }
  68.                        if ($($t.Tools) -eq "toolsOld") { $toolstatus = "<font color=""red"">Out of date</font>" }
  69.                        if ($($t.Tools) -eq "toolsNotRunning") { $toolstatus = "Not running" }
  70.  
  71.                        if ($($t.Online) -eq "PoweredOn") {$onlinestatus = "Online"}
  72.                        if ($($t.Online) -eq "PoweredOff") {$onlinestatus ="<font color=""red"">Offline</font>"}
  73.                     Write-Output "<td class=""Normal-left"">$($t.name)</td><td class=""Normal"">$onlinestatus</td><td class=""Normal"">$toolstatus</td></tr>"
  74.                 }                
  75.         Write-Output "</table></body></html>"
  76.     }
  77.  
  78. $digits = 2
  79. $SystemDrive = [system.environment]::getenvironmentvariable("SystemDrive")
  80. $Folder = "$SystemDrive\VMwareReports"
  81.  if (!(Test-Path $folder)) {  New-Item $Folder -Type Directory | Out-Null }
  82.  
  83. # Initialize arrays
  84. $DReport = @()
  85. $SReport = @()
  86. $TReport = @()
  87. $HReport = @()
  88.  
  89. "Gathering information for report. This may take several minutes."
  90. # Get Datastore information from hosts
  91.  
  92. $datastores = Get-Datastore | Sort-Object Name
  93.  
  94.         Get-Datastore | Sort-Object Name | %{
  95.             $Store = {} | Select Name, CapacityGB, UsedGB, PercFree
  96.             $Store.Name = $_.name
  97.             $Store.CapacityGB = [math]::Round($_.capacityMB/1024,$digits)
  98.             $Store.UsedGB = [math]::Round(($_.CapacityMB - $_.FreeSpaceMB)/1024,$digits)
  99.             $Store.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)
  100.             $DReport += $Store
  101.          }
  102.  
  103.  
  104. # Get Snapshot and VMware tools information
  105.  
  106. $VMs = Get-VM
  107.  
  108. foreach ($vm in $VMs)
  109. {
  110. if (Get-Snapshot $vm)
  111.     {
  112.  
  113.     Get-Snapshot $vm |  %{
  114.  
  115.     $snapinfo = {} | select VM, Name, Created, SizeGB
  116.     $snapinfo.VM = $vm
  117.     $snapinfo.Name = $_.name
  118.     $snapinfo.Created = $_.created
  119.     $snapinfo.SizeGB = [system.math]::ceiling($_.SizeGB)
  120.     $SReport += $snapinfo
  121.      }
  122.     }
  123.    
  124.     Get-View $vm.id |Sort-Object Name | %{
  125.  
  126.     $toolinfo = {} | select Name, Tools, Online
  127.     $toolinfo.Name = $_.name
  128.     $toolinfo.Online = $vm.PowerState
  129.     $toolinfo.Tools = $_.Guest.ToolsStatus
  130.     $TReport += $toolinfo
  131.     }
  132. }
  133. # Gather information for VM Hosts
  134. if (Get-Cluster)
  135. {
  136. ForEach ($Cluster in Get-Cluster)
  137.     {
  138.         ForEach ($vmhost in ($cluster | Get-VMHost))
  139.         {
  140.             $VMView = $VMhost | Get-View
  141.                         $VMSummary = {} | Select HostName, ClusterName, MemorySizeGB, CPUCores, CpuSpeed, Version
  142.                         $VMSummary.HostName = $VMhost.Name
  143.                         $VMSummary.ClusterName = $Cluster.Name
  144.                         $VMSummary.MemorySizeGB = [system.math]::ceiling($VMview.hardware.memorysize / 1024Mb)
  145.                         $VMSummary.CPUCores = $VMview.hardware.cpuinfo.numCpuCores
  146.                         $VMSummary.CpuSpeed = [math]::Round($VMview.hardware.cpuinfo.Hz/"1000000000", $digits)
  147.                         $VMSummary.Version = $VMview.Config.Product.FullName
  148.                         $HReport += $VMSummary
  149.                     }
  150.             }
  151. }
  152. else
  153. {
  154.         ForEach ($vmhost in (Get-VMHost))
  155.         {
  156.             $VMView = $VMhost | Get-View
  157.                         $VMSummary = {} | Select HostName, ClusterName, MemorySizeGB, CPUCores, CpuSpeed, Version
  158.                         $VMSummary.HostName = $VMhost.Name
  159.                         $VMSummary.ClusterName = "No cluster"
  160.                         $VMSummary.MemorySizeGB = [system.math]::ceiling($VMview.hardware.memorysize / 1024Mb)
  161.                         $VMSummary.CPUCores = $VMview.hardware.cpuinfo.numCpuCores
  162.                         $VMSummary.CpuSpeed = [math]::Round($VMview.hardware.cpuinfo.Hz/"1000000000", $digits)
  163.                         $VMSummary.Version = $VMview.Config.Product.FullName
  164.                         $HReport += $VMSummary
  165.                   }
  166.          }
  167.  
  168.  
  169.  
  170. Disconnect-VIServer * -Confirm:$False
  171. $date = Get-Date -uformat "%m-%d-%Y"
  172. $datefull = Get-Date
  173. Generate-Report > "$Folder\VMware-$vcenter-$date.html"
  174.  
  175. if (Test-Path "$Folder\VMware-$vcenter-$date.html") {
  176. "Done. Report is located at $Folder\VMware-$vcenter-$date.html"
  177. Invoke-Expression "$Folder\VMware-$vcenter-$date.html"
  178. }
  179. else
  180. {
  181. "Problem occured. Verify $folder exists and you have permissions to write to that folder."
  182. }
  183. Clear-Variable vcenter
  184. Clear-Variable vcusername
  185. #Clear-Variable pw
Advertisement
Add Comment
Please, Sign In to add comment