Trupix
Contributor
Contributor

PowerCLI script - VM % of used vCPU, Cores and Memory

Jump to solution

I'm trying to write a script to check VM % of used vCPU, Cores and Memory from all hosts in vCenter.

Could someone help me with this?

Calcuations in attachment.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try something like this

Foreach($esx in Get-VMHost){

    $vms = Get-VM -Location $esx

    $ds = Get-Datastore -RelatedObject $esx


    $TotalvCPU = $vms.NumCpu | Measure-Object -Sum | select -ExpandProperty Sum

    $TotalESXSpace = $ds.CapacityGB | Measure-Object -Sum | Select-Object -ExpandProperty Sum


    $vms | Select Name,

        @{N='vmvCPU';E={$_.NumCpu}},

        @{N='Memory';E={$_.MemoryMB}},

        @{N='UsedMemory';E={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}},

        @{N='UsedSpaceGB';E={$_.UsedSpaceGB}},

        @{N='ProvisionedSpaceGB';E={$_.ProvisionedSpaceGB}},

        @{N='%ofvCPU/TotalvCPU';E={[math]::Round($_.NumCpu/$TotalvCPU*100)}},

        @{N='%ofvCPU/TotalCores';E={[math]::Round($_.NumCpu/$esx.NumCpu*100)}},

        @{N='%ofUsedMemory/TotalESXMemory';E={[math]::Round($_.ExtensionData.Summary.QuickStats.HostMemoryUsage/$esx.MemoryTotalMB*100)}},

        @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::Round($_.UsedSpaceGB/$TotalESXSpace*100)}}

}


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

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership

What do you already have?
And what is the problem you encounter?


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

0 Kudos
Trupix
Contributor
Contributor

I have something like this:

Foreach($esx in Get-VMHost){

    $vCPU = Get-VM -Location $esx | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

$TotalESXCores =  ???

$TotalESXvCPUs = ???

$TotalESXMemory = ???

$TotalESXSpace = ???

$vmname = Get-VM

$vmvCPU = Get-VM -Name $vmname | select NumCPU

$vmMemory = Get-VM -Name $vmname | select MemoryMB

$vmUsedMemory = Get-VM -Name $vmname | ???

$vmUsedSpaceGB = Get-VM -Name $vmname | select UsedSpaceGB

$ProvisionedSpaceGB = Get-VM -Name $vmname | select ProvisionedSpaceGB

$vmname | Select Name,

  @{N='vCPU';E={$vCPU}},

  @{N='Memory';E={$vmMemory}},

  @{N='UsedMemory';E={$vmUsedMemory}},

  @{N='UsedSpaceGB';E={$vmUsedSpaceGB}},

  @{N='ProvisionedSpaceGB';E={$ProvisionedSpaceGB}},

  @{N='%ofvCPU/TotalvCPU';E={[math]::($vCPU/$TotalESXvCPUs)}}

  @{N='%ofvCPU/TotalCores';E={[math]::($vCPU/$TotalESXCores)}}

  @{N='%ofUsedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXMemory)}},

  @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXSpace)}},

 

}

0 Kudos
Trupix
Contributor
Contributor

This is better but  it doesn't work

Foreach($esx in Get-VMHost){

$TotalvCPU = Get-VM -Location $esx | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

$TotalESXCores =  Get-VMHost | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum

$TotalESXMemory = Get-VMHost | Measure-Object -Property MemoryTotalGB -Sum | Select-Object -ExpandProperty Sum

$TotalESXSpace = Get-Datastore | Measure-Object -Property CapacityGB -Sum | Select-Object -ExpandProperty Sum

$vmname = Get-VM

$vmvCPU = Get-VM -Name $vmname | select NumCPU

$vmMemory = Get-VM -Name $vmname | select MemoryMB

$vmUsedMemory = Get-VM -Name $vmname | select @{n="HostMemoryUsage";e={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}

$vmUsedSpaceGB = Get-VM -Name $vmname | select UsedSpaceGB

$ProvisionedSpaceGB = Get-VM -Name $vmname | select ProvisionedSpaceGB

$vmname | Select Name,

  @{N='vmvCPU';E={$vmvCPU}},

  @{N='Memory';E={$vmMemory}},

  @{N='UsedMemory';E={$vmUsedMemory}},

  @{N='UsedSpaceGB';E={$vmUsedSpaceGB}},

  @{N='ProvisionedSpaceGB';E={$ProvisionedSpaceGB}},

  @{N='%ofvCPU/TotalvCPU';E={[math]::($vmvCPU/$TotalvCPU)}}

  @{N='%ofvCPU/TotalCores';E={[math]::($vmvCPU/$TotalESXCores)}}

  @{N='%ofUsedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXMemory)}},

  @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXSpace)}},

}

0 Kudos
Trupix
Contributor
Contributor

Foreach($esx in Get-VMHost){

$TotalvCPU = Get-VM | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

$TotalESXCores =  Get-VMHost | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum

$TotalESXMemory = Get-VMHost | Measure-Object -Property MemoryTotalGB -Sum | Select-Object -ExpandProperty Sum

$TotalESXSpace = Get-Datastore | Measure-Object -Property CapacityGB -Sum | Select-Object -ExpandProperty Sum

$vmname = Get-VM

$vmvCPU = Get-VM -Name $vmname | select NumCPU

$vmMemory = Get-VM -Name $vmname | select MemoryMB

$vmUsedMemory = Get-VM -Name $vmname | select @{n="HostMemoryUsage";e={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}

$vmUsedSpaceGB = Get-VM -Name $vmname | select UsedSpaceGB

$ProvisionedSpaceGB = Get-VM -Name $vmname | select ProvisionedSpaceGB

$vmname | Select Name,

  @{N='vmvCPU';E={$vmvCPU}},

  @{N='Memory';E={$vmMemory}},

  @{N='UsedMemory';E={$vmUsedMemory}},

  @{N='UsedSpaceGB';E={$vmUsedSpaceGB}},

  @{N='ProvisionedSpaceGB';E={$ProvisionedSpaceGB}},

  @{N='%ofvCPU/TotalvCPU';E={[math]::($vmvCPU/$TotalvCPU)}},

  @{N='%ofvCPU/TotalCores';E={[math]::($vmvCPU/$TotalESXCores)}},

  @{N='%ofUsedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXMemory)}},

  @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXSpace)}},

}

0 Kudos
Trupix
Contributor
Contributor

At line:21 char:89

+ ... edSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXSpace)}},

+                                                                          ~

Missing expression after ',' in pipeline element.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : MissingExpression

0 Kudos
Trupix
Contributor
Contributor

Foreach($vmname in Get-VM){

$TotalvCPU = Get-VM | Measure-Object -Property NumCpu -Sum | select -ExpandProperty Sum

$TotalESXCores =  Get-VMHost | Measure-Object -Property NumCpu -Sum | Select-Object -ExpandProperty Sum

$TotalESXMemory = Get-VMHost | Measure-Object -Property MemoryTotalGB -Sum | Select-Object -ExpandProperty Sum

$TotalESXSpace = Get-Datastore | Measure-Object -Property CapacityGB -Sum | Select-Object -ExpandProperty Sum

$vmname = Get-VM

$vmvCPU = Get-VM -Name $vmname | select NumCPU

$vmMemory = Get-VM -Name $vmname | select MemoryMB

$vmUsedMemory = Get-VM -Name $vmname | select @{n="HostMemoryUsage";e={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}}

$vmUsedSpaceGB = Get-VM -Name $vmname | select UsedSpaceGB

$ProvisionedSpaceGB = Get-VM -Name $vmname | select ProvisionedSpaceGB

$vmname | Select Name,

  @{N='vmvCPU';E={$vmvCPU}},

  @{N='Memory';E={$vmMemory}},

  @{N='UsedMemory';E={$vmUsedMemory}},

  @{N='UsedSpaceGB';E={$vmUsedSpaceGB}},

  @{N='ProvisionedSpaceGB';E={$ProvisionedSpaceGB}},

  @{N='%ofvCPU/TotalvCPU';E={[math]::($vmvCPU/$TotalvCPU)}},

  @{N='%ofvCPU/TotalCores';E={[math]::($vmvCPU/$TotalESXCores)}},

  @{N='%ofUsedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXMemory)}},

  @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::($vmUsedMemory/$TotalESXSpace)}},

}

0 Kudos
LucD
Leadership
Leadership

Try something like this

Foreach($esx in Get-VMHost){

    $vms = Get-VM -Location $esx

    $ds = Get-Datastore -RelatedObject $esx


    $TotalvCPU = $vms.NumCpu | Measure-Object -Sum | select -ExpandProperty Sum

    $TotalESXSpace = $ds.CapacityGB | Measure-Object -Sum | Select-Object -ExpandProperty Sum


    $vms | Select Name,

        @{N='vmvCPU';E={$_.NumCpu}},

        @{N='Memory';E={$_.MemoryMB}},

        @{N='UsedMemory';E={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}},

        @{N='UsedSpaceGB';E={$_.UsedSpaceGB}},

        @{N='ProvisionedSpaceGB';E={$_.ProvisionedSpaceGB}},

        @{N='%ofvCPU/TotalvCPU';E={[math]::Round($_.NumCpu/$TotalvCPU*100)}},

        @{N='%ofvCPU/TotalCores';E={[math]::Round($_.NumCpu/$esx.NumCpu*100)}},

        @{N='%ofUsedMemory/TotalESXMemory';E={[math]::Round($_.ExtensionData.Summary.QuickStats.HostMemoryUsage/$esx.MemoryTotalMB*100)}},

        @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::Round($_.UsedSpaceGB/$TotalESXSpace*100)}}

}


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

0 Kudos
Trupix
Contributor
Contributor

Thank you. It's working great.

How to export this to csv file?

0 Kudos
LucD
Leadership
Leadership

One way of doing that is like this

$report = Foreach($esx in Get-VMHost){

    $vms = Get-VM -Location $esx

    $ds = Get-Datastore -RelatedObject $esx


    $TotalvCPU = $vms.NumCpu | Measure-Object -Sum | select -ExpandProperty Sum

    $TotalESXSpace = $ds.CapacityGB | Measure-Object -Sum | Select-Object -ExpandProperty Sum


    $vms | Select Name,

        @{N='vmvCPU';E={$_.NumCpu}},

        @{N='Memory';E={$_.MemoryMB}},

        @{N='UsedMemory';E={$_.ExtensionData.Summary.QuickStats.HostMemoryUsage}},

        @{N='UsedSpaceGB';E={$_.UsedSpaceGB}},

        @{N='ProvisionedSpaceGB';E={$_.ProvisionedSpaceGB}},

        @{N='%ofvCPU/TotalvCPU';E={[math]::Round($_.NumCpu/$TotalvCPU*100)}},

        @{N='%ofvCPU/TotalCores';E={[math]::Round($_.NumCpu/$esx.NumCpu*100)}},

        @{N='%ofUsedMemory/TotalESXMemory';E={[math]::Round($_.ExtensionData.Summary.QuickStats.HostMemoryUsage/$esx.MemoryTotalMB*100)}},

        @{N='%ofProvisionedSpaceGB/TotalESXSpace';E={[math]::Round($_.UsedSpaceGB/$TotalESXSpace*100)}}

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
Trupix
Contributor
Contributor

How to convert this csv to UTF8noBOM ?

0 Kudos
LucD
Leadership
Leadership

In PSv7 this issue is fixed.

In PSv5.1 you could do something like this

$fileName = '.\report.csv'

$report | Export-Csv -Path $fileName -Encoding UTF8


$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

$Content = Get-Content -Path $fileName -Raw

[System.IO.File]::WriteAllLines($fileName, $Content, $Utf8NoBomEncoding)


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

0 Kudos
Trupix
Contributor
Contributor

Thank you, I tried something like that, but the output is ASCII text.

I have PowerShell 7.0.1

...

$results | Export-CSV -Encoding UTF-8 -Path ReportVMware.csv -NoTypeInformation -UseCulture

$MyPath = "ReportVMware.csv" # read the file in

$MyFile = Get-Content -Raw $MyPath

$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False

$MyPathOut = "ReportVMware_UTF8noBOM.csv" # export it here without BOM

[System.IO.File]::WriteAllLines($MyPathOut, $MyFile, $Utf8NoBomEncoding)

[root@: ~]# file ReportVMware.csv

ReportVMware.csv: UTF-8 Unicode (with BOM) text

[root@: ~]# file ReportVMware_UTF8noBOM.csv

ReportVMware_UTF8noBOM.csv: ASCII text

0 Kudos
LucD
Leadership
Leadership

When I use Encoding UTF8 on Export-Csv the file is UTF8 (no BOM).

At least on a Windows box.


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

0 Kudos
Trupix
Contributor
Contributor

Thanks, I will check this Smiley Happy

0 Kudos