VMware Cloud Community
MrJohnson
Enthusiast
Enthusiast
Jump to solution

Powercli Script to Capture ESXi Host CPU & Memory Usage

Hello All,

I'm having the hardest time finding a script that will give me ESXi Host CPU and Memory Usage. I just need something simple if there is such a thing?

Example of what I'm looking for:

HostnameCPU GHz CapacityCPU GHz UsedCPU GHz Free

  

HostnameMemory CapacityMemory UsedMemory Free

Please help.

37 Replies
MrJohnson
Enthusiast
Enthusiast
Jump to solution

This almost works for me in adding the storage as well.  The only problem is it only exports the storage info and not the CPU and Mem to the report.csv, and I can figure out how to get all the vClusters in vCenter in the same script.

Get-Cluster -Name vCluster |

Get-VMHost|

Select Name,

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}}

$Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

   @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

   @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

   @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

   @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"|

Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture

0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

I found the following.  Do you know if it is possible to add what you gave me to this script?

# Author: Amol Patil

# DataStoreUsageReport

set-executionpolicy Unrestricted -Force # Execute Policy

##### Add VMWare Snanpin.

if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue))

{

   Add-PSSnapin VMware.VimAutomation.Core

}

#*****************************************

$SCRIPT_PARENT   = Split-Path -Parent $MyInvocation.MyCommand.Definition

#************** Remove old files ***************************

remove-item ($SCRIPT_PARENT + "\Report\V*.html") -force

########### Connect VCs from VC_List.txt ############

$VCs= Get-Content ($SCRIPT_PARENT + "\vc_list.txt") -ErrorAction SilentlyContinue # mention vcenter name where you want to check resources.

# $VCs= Get-Content  -Path N:\Scripts\VMWare\Storage_Report_HTML\vc_list.txt 

$D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.

Write-Host "Connecting to VC" -foregroundcolor yellow

#*****************************************

foreach($vc in $VCs)

{

Connect-VIServer $VC -WarningAction 0

$outputfile = ($SCRIPT_PARENT + "\Report\$($VC).html") #".\Report\$($VC).html"

Write-Host ""

Write-Host "Collecting details from $VC" -foregroundcolor green

$Result = @()

$Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

  @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

  @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

  @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

  @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"

 

      $HTML = '<style type="text/css">

      #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}

      #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}

      #Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#cccccc;color:#000000;}

      #Header tr.alt td {color:#000;background-color:#EAF2D3;}

      </Style>'

    $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">~$VC-DataStore Verification Report~ </h1></font>

    <h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>

              

            <TR>

                  <TH><B>DataStore Name</B></TH>

                  <TH><B>Free Space (GB)</B></TD>

                  <TH><B>Capacity (GB)</B></TH>

                  <TH><B>Provisioned Space (GB)</B></TH>

                  <TH><B>Free Space (%)</B></TH>

                 

            </TR>"

    Foreach($Entry in $Result)

    {

        if($Entry.FreeSpace -lt "20")

            {

                  $HTML += "<TR bgColor=Red>"

            }

            else

            {

                  $HTML += "<TR>"

            }

            $HTML += "

                                    <TD>$($Entry.Name)</TD>

                                    <TD>$($Entry.FreeSpaceGB)</TD>

                                    <TD>$($Entry.CapacityGB)</TD>

                                    <TD>$($Entry.ProvisionedSpaceGB)</TD>

                                    <TD>$($Entry.FreeSpace)</TD>

                              </TR>"

    }

    $HTML += "</Table></BODY></HTML>"

      $HTML | Out-File $OutputFile

Disconnect-VIServer $VC -Confirm:$false

}

$Uname = Get-Content Env:USERNAME

$Comp = Get-Content Env:COMPUTERNAME

  

#Send mail->

    # Add email IDs in email_id.txt file with , and in next line.   

    $mailto = Get-Content ($SCRIPT_PARENT + "\email_id.txt") -ErrorAction SilentlyContinue

    $SMTPserver = "mail-server" # SMTP server

    $msg = new-object Net.Mail.MailMessage 

    $smtp = new-object Net.Mail.SmtpClient($SMTPserver) 

    $msg.From = "email-address" # Sender ID

    $msg.IsBodyHTML = $true

    $msg.To.Add($mailto)  # Mail To id get from list

    $msg.Subject = "Datastores Usage Report - $vcs" # Subject of the email.

foreach($vc in $vcs)

     {

     $MailTextT =  Get-Content ($SCRIPT_PARENT + "\Report\V*.html") -ErrorAction SilentlyContinue

     $Sig =  "<html><p><o:p> </o:p></p><B> Capacity Report, <p> ProdSE (email-address)</B></p></html>"

     $Top = "<html> This Script is executed on Server - <B>$Comp</B> by User - <b> $Uname </b></html>"

     $MailText= $Top + $MailTextT + $Sig

    

    }

   

   $msg.Body = $MailText

   $smtp.Send($msg)

  

#*****************************************

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are querying for information from 2 different objects (VMHost and Datastore).

That means you will have to combine this in 1 select cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

I added the following.  But not sure what I'm missing.  I get the following in PowerCLI when running it.

Key   : N

Value : CPUGHzCapacity

Name  : N

Key   : E

Value : [math]::Round($_.CpuTotalMhz/1000,2)

Name  : E

Key   : N

Value : CPUGHzUsed

Name  : N

Key   : E

Value : [math]::Round($_.CpuUsageMhz/1000,2)

Name  : E

Key   : N

Value : CPUGHzFree

Name  : N

Key   : E

Value : [math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)

Name  : E

Key   : N

Value : MemoryCapacityGB

Name  : N

Key   : E

Value : [math]::Round($_.MemoryTotalGB,2)

Name  : E

Key   : N

Value : MemoryUsedGB

Name  : N

Key   : E

Value : [math]::Round($_.MemoryUsageGB,2)

Name  : E

Key   : N

Value : MemoryFreeGB

Name  : N

Key   : E

Value : [math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)

Name  : E

My changes

########### Connect VCs from VC_List.txt ############

$VCs= Get-Content ($SCRIPT_PARENT + "\vc_list.txt") -ErrorAction SilentlyContinue # mention vcenter name where you want to check resources.

# $VCs= Get-Content  -Path N:\Scripts\VMWare\Storage_Report_HTML\vc_list.txt

$D = get-date -uformat "%m-%d-%Y-%H:%M" # To get a current date.

Write-Host "Connecting to VC" -foregroundcolor yellow

#*****************************************

foreach($vc in $VCs)

{

Connect-VIServer $VC -WarningAction 0

$outputfile = ($SCRIPT_PARENT + "\Report\$($VC).html") #".\Report\$($VC).html"

Write-Host ""

Write-Host "Collecting details from $VC" -foregroundcolor green

$Result = @()

$Result += Get-View -ViewType Datastore | Where-Object {$_.Name -notmatch "pag"} | Select-Object -Property Name,

  @{N="FreeSpaceGB";E={[Math]::Round($_.Summary.FreeSpace/1GB,0)}},

  @{N="CapacityGB"; E={[Math]::Round($_.Summary.Capacity/1GB,0)}},

  @{N="ProvisionedSpaceGB";E={[Math]::Round(($_.Summary.Capacity - $_.Summary.FreeSpace + $_.Summary.Uncommitted)/1GB,0)}},

  @{N="FreeSpace";E={[math]::Round(((100* ($_.Summary.FreeSpace/1GB))/ ($_.Summary.Capacity/1GB)),0)}} | sort -Property "FreeSpace"

  @{N="CPUGHzCapacity";E={[math]::Round($_.CpuTotalMhz/1000,2)}},

  @{N="CPUGHzUsed";E={[math]::Round($_.CpuUsageMhz/1000,2)}},

  @{N="CPUGHzFree";E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

  @{N="MemoryCapacityGB";E={[math]::Round($_.MemoryTotalGB,2)}},

  @{N="MemoryUsedGB";E={[math]::Round($_.MemoryUsageGB,2)}},

  @{N="MemoryFreeGB";E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}}

      $HTML = '<style type="text/css">

      #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}

      #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}

      #Header th {font-size:14px;text-align:center;padding-top:5px;padding-bottom:4px;background-color:#cccccc;color:#000000;}

      #Header tr.alt td {color:#000;background-color:#EAF2D3;}

      </Style>'

    $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header><caption><font size=3 color=green><h1 align=""center"">~$VC-DataStore Verification Report~ </h1></font>

    <h4 align=""right""><font size=3 color=""#00008B"">Date: $D </font></h4></caption>

            

            <TR>

                  <TH><B>DataStore Name</B></TH>

                  <TH><B>Free Space (GB)</B></TD>

                  <TH><B>Capacity (GB)</B></TH>

                  <TH><B>Provisioned Space (GB)</B></TH>

                  <TH><B>Free Space (%)</B></TH>

                  <TH><B>CPU GHz Capacity (MHz)</B></TH>

                  <TH><B>CPU GHz Free (MHz)</B></TD>

                  <TH><B>CPU GHz Used (MHz)</B></TH>

                  <TH><B>Memory Capacity (GB)</B></TH>

                  <TH><B>MemoryFree (GB)</B></TD>

                  <TH><B>Memory Used (GB)</B></TH>

               

            </TR>"

    Foreach($Entry in $Result)

    {

        if($Entry.FreeSpace -lt "20")

            {

                  $HTML += "<TR bgColor=Red>"

            }

            else

            {

                  $HTML += "<TR>"

            }

            $HTML += "

                                    <TD>$($Entry.Name)</TD>

                                    <TD>$($Entry.FreeSpaceGB)</TD>

                                    <TD>$($Entry.CapacityGB)</TD>

                                    <TD>$($Entry.ProvisionedSpaceGB)</TD>

                                    <TD>$($Entry.FreeSpace)</TD>

                                    <TD>$($Entry.CPUGHzCapacity)</TD>

                                    <TD>$($Entry.CPUGHzUsed)</TD>

                                    <TD>$($Entry.CPUGHzFree)</TD>

                                    <TD>$($Entry.MemoryCapacityGB)</TD>

                                    <TD>$($Entry.MemoryUsedGB)</TD>

                                    <TD>$($Entry.MemoryFreeGB)</TD>

                              </TR>"

    }

    $HTML += "</Table></BODY></HTML>"

      $HTML | Out-File $OutputFile

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Like I said before, you are mixing properties from 2 different objects in 1 Select, that will not work.

With the Get-View -ViewType Datastore you get Datastore objects, but you also select properties that come from a VMHost object.

And there is this Sort-Object in the middle of the Select, that is causing errors.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

Understood.  Can you help me in getting all the ESXi in each vClusters added to one report?

I have about 4 vClusters in vCenter if that matters.

Get-Cluster -Name -vCluster |

Get-VMHost|

Select Name,

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, try like this

Get-Cluster |

Get-VMHost|

Select Name,

    @{N='Cluster';E={$_.Parent.Name}},

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

Thank you LucD

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Hi,

Thank you for your Scripts.

Please let me know, how can i write this with percent.

Thank you for your answer.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which property do you want in percentage?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Thank you so much for your responsive.

CPU and RAM.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-Cluster |

Get-VMHost|

Select Name,

    @{N='Cluster';E={$_.Parent.Name}},

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU % Used';E={[math]::Round($_.CpuUsageMhz/$_.CpuTotalMhz*100,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Used %';E={[math]::Round($_.MemoryUsageGB/$_.MemoryTotalGB*100,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ehsanijavad
Enthusiast
Enthusiast
Jump to solution

Thank you so much LucD.

0 Kudos
naveenstisys
Contributor
Contributor
Jump to solution

Hi LucD,

Can we get this report for more than one ESXi?

I have a list of 20 ESXi servers and looking for same kind of report in one script and need this report every week via email, is this possible?

 

0 Kudos
naveenstisys
Contributor
Contributor
Jump to solution

connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxx

connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxx

connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxx

connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxx

Get-Cluster |

Get-VMHost|

Select Name,

    @{N='Cluster';E={$_.Parent.Name}},

    @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}},

    @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}},

    @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}},

    @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}},

    @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}},

    @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} |

Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture

With the help of script in this thread, I am able to export a single file (having multiple ESXi Host CPU & Memory Usage) by single script, can we schedule this script to sent the file every week over email? 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sending an email can be done with the Send-MailMessage cmdlet.
There are ample example available in this community.

Scheduling the script to run every week will require a scheduler.
On Windows you could use the Window Task Scheduler.
On Linux you can create an entry in a crontab table.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

naveenstisys
Contributor
Contributor
Jump to solution

Thanks LucD

By this script, i am able to get desired results and i have scheduled this script in Task Scheduler.

connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxxx
connect-viserver -server xx.xx.xx.xx -User administrator@vsphere.local -Password xxxxxx
Get-Cluster | Get-VMHost| Select Name, @{N='Cluster';E={$_.Parent.Name}}, @{N='CPU GHz Capacity';E={[math]::Round($_.CpuTotalMhz/1000,2)}}, @{N='CPU GHz Used';E={[math]::Round($_.CpuUsageMhz/1000,2)}}, @{N='CPU GHz Free';E={[math]::Round(($_.CpuTotalMhz - $_.CpuUsageMhz)/1000,2)}}, @{N='Memory Capacity GB';E={[math]::Round($_.MemoryTotalGB,2)}}, @{N='Memory Used GB';E={[math]::Round($_.MemoryUsageGB,2)}}, @{N='Memory Free GB';E={[math]::Round(($_.MemoryTotalGB - $_.MemoryUsageGB),2)}} | Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture
$MailSender = "VMwareReport@pelican.com"
$MailSmtpServer = "hybrid.pelican.com"
Send-MailMessage -from $MailSender -to "xxxx@xxxx.com" -subject "VMwareReport" -body "Hello Everyone, PFA report" -Attachments .\report.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -smtpServer $MailSmtpServer

0 Kudos
system_noida
Enthusiast
Enthusiast
Jump to solution

Hi @LucD how can I get the % free memory and CPU please

0 Kudos