PowerCLI

 View Only
Expand all | Collapse all

Powercli Script to Capture ESXi Host CPU & Memory Usage

LucD

LucDJan 24, 2015 07:00 AMBest Answer

  • 1.  Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 23, 2015 10:13 PM

    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.



  • 2.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 23, 2015 10:33 PM

    I don't need historical data.  I just need what is current when running the script.



  • 3.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage
    Best Answer

    Posted Jan 24, 2015 07:00 AM

    You mean like this

    Get-VMHost -Name MyEsx |

    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)}}



  • 4.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 04:00 PM

    Hi LuCD,

    I think that will do it.  I'm not sure how to run that though.

    I'm still trying to grab the whole scripting idea.  Do I just connect to the vCenter via the vSphereCLI and running it after saving it as a .ps1 file?

    Also,  thank you for replying.



  • 5.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 06:47 PM

    Yes, that is correct.

    Open the PowerCLI prompt, do a Connect-VIServer to the vCenter.

    And then run the script above which you saved in a .ps1 file.

    If you PowerShell session is position in the same folder where you stored the .ps1 file, you can do

    PS C:\Scripts> .\myscript.ps1

    otherwise you can run it by providing the full path

    PS C:\Scripts> C:\Scripts\myscript.ps1

    XtraVirt did a nice Beginners Guide.



  • 6.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 07:04 PM

    One last question.  If i wanted to add the current datastore usage would it be as followed and how can I send the final results to a .html or .csv file?

    Get-VMHost -Name MyEsx |

    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)}}

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

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

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



  • 7.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 07:41 PM

    Afaik, the VMHost object has no StorageUsageGB property.

    To calculate the storage used by a specific ESXi node, you would need to know if there is any shared storage (between multiple ESXi nodes).

    You could get all the VMs hosted on the ESXi node, and then summarise the datastore usage of each of these VMs.



  • 8.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 08:43 PM

    LucD,

    Thank you for taking the time to answer my questions it means a lot.  One last question please.... how can I get the results to a .html or .cvs file?



  • 9.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 09:27 PM

    What do you thing about this?

    Get-VMHost -Name MyEsx |

    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)}}

    Get-ChildItem "C:\Windows\System32" | Out-File"

              C:/script/results.csv"



  • 10.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 09:40 PM

    PowerShell has a cmdlet to create CSV files, just "pipe" the results to the Export-Csv cmdlet.

    Get-VMHost -Name MyEsx |

    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



  • 11.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Mar 27, 2023 07:53 PM

    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?

     



  • 12.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Mar 27, 2023 08:16 PM

    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? 



  • 13.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Mar 27, 2023 08:21 PM

    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.



  • 14.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Mar 27, 2023 09:36 PM

    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



  • 15.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 09:42 PM

    When I change "Get-VMHost" to "Get-Cluster"  I'm getting the following results.  Any suggestions how I can correct this?

    Name               : vCluster

    CPU GHz Capacity   : 0

    CPU GHz Used       : 0

    CPU GHz Free       : 0

    Memory Capacity GB : 0

    Memory Used GB     : 0

    Memory Free GB     : 0

    P.S. Thanks for the "Beginners Guide to Managing VMware

    using PowerShell" doc.



  • 16.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 09:48 PM

    The Cluster object returned by Get-Cluster doesn't have the properties used in the different calculated properties of the Select-Object cmdlet.

    If you want to to calculate the same values for a cluster requires a change of the script.

    You could get the values for all the ESXi nodes in the cluster, and then summarise them.

    But like I said that is a slightly different script.



  • 17.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 09:51 PM

    after adding "Export-Csv -Path C:\report.csv -NoTypeInformation -UseCulture" I get the following.

    cmdlet Export-Csv at command pipeline position 1

    Supply values for the following parameters:

    InputObject:



  • 18.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:07 PM

    Did you also add the pipe symbol ('|') at the end of the 2nd to last line ?



  • 19.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:15 PM

    No, I copy and pasted what you added.  that is not the | sign its an I(eye). Do I need to add the | pipe symbol?



  • 20.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:18 PM

    Sorry I see what your talking about... I just added the | symbol.



  • 21.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:19 PM

    Strange, when I copy/paste it works.

    But yes, that should be a pipe symbol (vertical bar), same as on the 1st line



  • 22.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:31 PM

    It was a user error on my end.  You had it right the first time. Seeing that I have to capture each host in the vCluster one at a time I just need to try and figure out how to append to the same report.csv file. 



  • 23.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:36 PM

    That is quite easy, and one of the nice features of PowerShell.

    Get-Cluster -Name MyCluster |

    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 



  • 24.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 10:57 PM

    Can I ask one last question?  How can I add a second and 3rd vCluster? 

    WoW, I can't tell you how much I appreciate you're help.



  • 25.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 11:14 PM

    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



  • 26.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 26, 2015 11:32 PM

    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)

      

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



  • 27.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 27, 2015 08:08 AM

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

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



  • 28.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 27, 2015 06:36 PM

    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



  • 29.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 27, 2015 06:44 PM

    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.



  • 30.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 27, 2015 07:19 PM

    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



  • 31.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 27, 2015 09:23 PM

    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



  • 32.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jan 28, 2015 05:50 PM

    Thank you LucD



  • 33.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Feb 25, 2020 08:52 AM

    Hi,

    Thank you for your Scripts.

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

    Thank you for your answer.



  • 34.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Feb 25, 2020 09:02 AM

    Which property do you want in percentage?



  • 35.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Feb 25, 2020 09:20 AM

    Thank you so much for your responsive.

    CPU and RAM.



  • 36.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Feb 25, 2020 09:32 AM

    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



  • 37.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Feb 25, 2020 09:47 AM

    Thank you so much LucD.



  • 38.  RE: Powercli Script to Capture ESXi Host CPU & Memory Usage

    Posted Jul 18, 2023 04:15 PM

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