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.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
37 Replies
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

MrJohnson
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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?

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, when I copy/paste it works.

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


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

Reply
0 Kudos
MrJohnson
Enthusiast
Enthusiast
Jump to solution

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. 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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 


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

MrJohnson
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos