2020 | Scott W. Head  |  Sr. Systems Admin | Certified Microsoft Windows Server Administrator

PowerShell PowerCLI Get-VMHost

If connected to vCenter, display all ESXI hosts


Quick review of firewall and services on ESXI hosts

Get-VMHost | Get-VMHostFirewallException | export-csv C:\Temp\Firewalls.csv

Gets values requested From ESXI hosts and added some numeric formatting and math, and send via email.         

    PowerCLI 6.0 Load
    & 'C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'

    PowerCLI 6.5 Load
    & 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'


    Remove-Item "C:\temp\MyHTMLFile.html"

    & 'C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'


    Connect-VIServer -Server $vCenter -Protocol https -User $UserName -Password $Password

     $MyHosts=  Get-VMHost | Select Name, ConnectionState, Version, Build,@{l='RAM Total GB';e={($_.MemoryTotalGB).tostring("#.##")}}, @{l='RAM Usage GB';e={($_.MemoryUsageGB).tostring("#.##")}}, @{l='RAM Free GB';e={($_.MemoryTotalGB - $_.MemoryUsageGB).tostring("#.##")}}, @{l='CPU Used GHz';e={($_.CPUUsageMHZ / 1000)}}, @{l='CPU Total GHz';e={($_.CpuTotalMhz / (1000))}} | Sort-Object Name

    # HTML/CSS style for the output file

    $head = " <style>"
    $head += "BODY{background-color:white;}"
    $head += "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; width:1200px}"
    $head += "TH{border-width: 1px;padding:0px;border-style: solid;border-color: black;background-color:yellow}"
    $head += "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black}"
    $head += "</style>"

    # Write the output to an HTML file
    $myHosts | ConvertTo-HTML -Head $head -Body $strMail | Out-File C:\temp\MyHTMLFile.html

    # Mail the output file
    $msg = new-object Net.Mail.MailMessage
    $att = new-object Net.Mail.Attachment("C:\temp\MyHTMLFile.html")
    $smtp = new-object Net.Mail.SmtpClient("YourMailRelayHere")
    $msg.From = "YourEmail@Your.com"
    $msg.Subject = "ESXI Build Information"
    $msg.IsBodyHtml = 1
    $msg.Body = Get-Content C:\temp\MyHTMLFile.html

    Disconnect-viserver -server $vCenter -Confirm:$False        

Gets values requested From ESXI hosts and numeric formatting | out to grid

Get-VMHost | select-object Name, LicenseKey, state, connectionstate, powerstate, manufacturer, model, ProcessorType, NumCPU, @{l='CPUtotalMhz';e={("{0:N0}" -f $_.CPUtotalMhz)}}, @{l='CpuUsageMHz';e={("{0:N0}" -f $_.CpuUsageMHz)}} , @{l='MemoryTotalGB';e={($_.memorytotalGB).tostring("##.##")}},  @{l='MemoryUsageGB';e={($_.memoryUsageGB).tostring("##.##")}}, version | Out-GridView