Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Clear-Host
- Write-Host "
- ***********************************
- ** VMware Information Report **
- ***********************************
- "
- if (Get-PSSnapin vmware* -ErrorAction SilentlyContinue) {
- Write-Host "vSphere snapin already loaded, proceeding."
- } else {
- Write-Host "Loading vSphere snapin."
- Add-pssnapin VMWare.VimAutomation.Core -ErrorAction SilentlyContinue
- if (Get-PSSnapin vmware* -ErrorAction SilentlyContinue) {
- Write-Host "vSphere snapin loaded"
- } else {
- Write-Host -ForegroundColor Red "Error loading vSphere snapin. Halting."
- Write-Host -ForegroundColor Red "VMware PowerCLI is required to run this script."
- break
- }
- }
- ""
- function ping-ip
- {
- param( $ip )
- trap {$false; continue}
- $timeout = 1000
- $object = New-Object system.Net.NetworkInformation.Ping
- (($object.Send($ip, $timeout)).Status -eq 'Success')
- }
- $vcenter = Read-Host "Please enter the IP for your vCenter server or VMware host"
- #$vcusername = Read-Host "Enter username"
- #$vcpassword = Read-Host -AsSecureString "Enter password for $vcusername"
- #$pw = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($vcpassword))
- if (ping-ip($vcenter)){
- Write-Host "Please wait while we attempt to connect to $vcenter"
- Connect-VIServer -Server $vcenter <#-User $vcusername -Password $pw#> > $NULL 2>&1
- if (!($vcusername)) { $vcusername = [Environment]::UserName }
- Write-Host "Connected"
- } else {
- Write-Host -ForegroundColor Red "Halt: $vcenter is not reachable."
- break
- }
- function Generate-Report {
- 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>"
- Foreach ($hh in $hreport){
- 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>"
- }
- 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>"
- Foreach ($store in $dreport){
- if (([int]$($store.PercFree) -le "25") -and ([int]$($store.PercFree) -gt "11")) {$PercFree = "<font color=""yellow"">$($store.PercFree) "}
- elseif ([int]$($store.PercFree) -le "10") {$PercFree = "<font color=""red"">$($store.PercFree) "}
- else {$PercFree = "<font>$($store.PercFree)"}
- 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>"
- }
- 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>"
- Foreach ($sp in $sreport){
- 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>"
- }
- 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>"
- Foreach ($t in $treport){
- if ($($t.Tools) -eq "toolsOK") { $toolstatus = "<font color=""green"">Up to date</font>" }
- if ($($t.Tools) -eq "toolsOld") { $toolstatus = "<font color=""red"">Out of date</font>" }
- if ($($t.Tools) -eq "toolsNotRunning") { $toolstatus = "Not running" }
- if ($($t.Online) -eq "PoweredOn") {$onlinestatus = "Online"}
- if ($($t.Online) -eq "PoweredOff") {$onlinestatus ="<font color=""red"">Offline</font>"}
- Write-Output "<td class=""Normal-left"">$($t.name)</td><td class=""Normal"">$onlinestatus</td><td class=""Normal"">$toolstatus</td></tr>"
- }
- Write-Output "</table></body></html>"
- }
- $digits = 2
- $SystemDrive = [system.environment]::getenvironmentvariable("SystemDrive")
- $Folder = "$SystemDrive\VMwareReports"
- if (!(Test-Path $folder)) { New-Item $Folder -Type Directory | Out-Null }
- # Initialize arrays
- $DReport = @()
- $SReport = @()
- $TReport = @()
- $HReport = @()
- "Gathering information for report. This may take several minutes."
- # Get Datastore information from hosts
- $datastores = Get-Datastore | Sort-Object Name
- Get-Datastore | Sort-Object Name | %{
- $Store = {} | Select Name, CapacityGB, UsedGB, PercFree
- $Store.Name = $_.name
- $Store.CapacityGB = [math]::Round($_.capacityMB/1024,$digits)
- $Store.UsedGB = [math]::Round(($_.CapacityMB - $_.FreeSpaceMB)/1024,$digits)
- $Store.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)
- $DReport += $Store
- }
- # Get Snapshot and VMware tools information
- $VMs = Get-VM
- foreach ($vm in $VMs)
- {
- if (Get-Snapshot $vm)
- {
- Get-Snapshot $vm | %{
- $snapinfo = {} | select VM, Name, Created, SizeGB
- $snapinfo.VM = $vm
- $snapinfo.Name = $_.name
- $snapinfo.Created = $_.created
- $snapinfo.SizeGB = [system.math]::ceiling($_.SizeGB)
- $SReport += $snapinfo
- }
- }
- Get-View $vm.id |Sort-Object Name | %{
- $toolinfo = {} | select Name, Tools, Online
- $toolinfo.Name = $_.name
- $toolinfo.Online = $vm.PowerState
- $toolinfo.Tools = $_.Guest.ToolsStatus
- $TReport += $toolinfo
- }
- }
- # Gather information for VM Hosts
- if (Get-Cluster)
- {
- ForEach ($Cluster in Get-Cluster)
- {
- ForEach ($vmhost in ($cluster | Get-VMHost))
- {
- $VMView = $VMhost | Get-View
- $VMSummary = {} | Select HostName, ClusterName, MemorySizeGB, CPUCores, CpuSpeed, Version
- $VMSummary.HostName = $VMhost.Name
- $VMSummary.ClusterName = $Cluster.Name
- $VMSummary.MemorySizeGB = [system.math]::ceiling($VMview.hardware.memorysize / 1024Mb)
- $VMSummary.CPUCores = $VMview.hardware.cpuinfo.numCpuCores
- $VMSummary.CpuSpeed = [math]::Round($VMview.hardware.cpuinfo.Hz/"1000000000", $digits)
- $VMSummary.Version = $VMview.Config.Product.FullName
- $HReport += $VMSummary
- }
- }
- }
- else
- {
- ForEach ($vmhost in (Get-VMHost))
- {
- $VMView = $VMhost | Get-View
- $VMSummary = {} | Select HostName, ClusterName, MemorySizeGB, CPUCores, CpuSpeed, Version
- $VMSummary.HostName = $VMhost.Name
- $VMSummary.ClusterName = "No cluster"
- $VMSummary.MemorySizeGB = [system.math]::ceiling($VMview.hardware.memorysize / 1024Mb)
- $VMSummary.CPUCores = $VMview.hardware.cpuinfo.numCpuCores
- $VMSummary.CpuSpeed = [math]::Round($VMview.hardware.cpuinfo.Hz/"1000000000", $digits)
- $VMSummary.Version = $VMview.Config.Product.FullName
- $HReport += $VMSummary
- }
- }
- Disconnect-VIServer * -Confirm:$False
- $date = Get-Date -uformat "%m-%d-%Y"
- $datefull = Get-Date
- Generate-Report > "$Folder\VMware-$vcenter-$date.html"
- if (Test-Path "$Folder\VMware-$vcenter-$date.html") {
- "Done. Report is located at $Folder\VMware-$vcenter-$date.html"
- Invoke-Expression "$Folder\VMware-$vcenter-$date.html"
- }
- else
- {
- "Problem occured. Verify $folder exists and you have permissions to write to that folder."
- }
- Clear-Variable vcenter
- Clear-Variable vcusername
- #Clear-Variable pw
Advertisement
Add Comment
Please, Sign In to add comment