1 2 3 Previous Next 32 Replies Latest reply on Feb 25, 2020 3:19 AM by ehsanijavad

    Powercli Script to Capture ESXi Host CPU & Memory Usage

    MrJohnson Enthusiast

      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.

        • 1. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
          MrJohnson Enthusiast

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

          • 2. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
            LucD Guru
            vExpertUser ModeratorsCommunity Warriors

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

             

            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
            • 3. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
              MrJohnson Enthusiast

              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.

              • 4. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

                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.

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                1 person found this helpful
                • 5. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                  MrJohnson Enthusiast

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

                  • 6. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                    LucD Guru
                    vExpertUser ModeratorsCommunity Warriors

                    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.

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    • 7. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                      MrJohnson Enthusiast

                      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?

                      • 8. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                        MrJohnson Enthusiast

                        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"

                        • 9. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                          LucD Guru
                          User ModeratorsCommunity WarriorsvExpert

                          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

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                            MrJohnson Enthusiast

                            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.

                            • 11. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                              LucD Guru
                              Community WarriorsUser ModeratorsvExpert

                              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.

                              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                              • 12. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                                MrJohnson Enthusiast

                                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:

                                • 13. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                                  LucD Guru
                                  vExpertCommunity WarriorsUser Moderators

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

                                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                  • 14. Re: Powercli Script to Capture ESXi Host CPU & Memory Usage
                                    MrJohnson Enthusiast

                                    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?

                                    1 2 3 Previous Next