Expert
Expert

CPU over commitment Report

Jump to solution

Hi All,

I'm looking to convert the CPU over commitment report into CSV instead of table view format. Also, is it possible to get Memory over commitment as well ?

# Connect to vCenter using passthrough credentials

connect-viserver "vc.com";

# Get a list of hosts in a specific cluster (omit the get-cluster to get all hosts, and just use get-vmhost)

$hosts = get-cluster "MYCLUSTER" | get-vmhost;

# Define an empty table

$table = @();

# Loop through each host found

foreach ($vmhost in $hosts) {

    # Create an empty row

    $row = "" | select Hostname, LogicalCPUs, CPUsAssigned, Overcommited;

    # Get the number of vCPUs assigned

    $cpumeasure = $vmhost | get-vm | where {$_.powerstate -eq "poweredon"} | measure-object numcpu -sum;

    # Add the hostname to the row

    $row.Hostname = (($vmhost.name).split("."))[0];

    # Add the number of logical CPUs to the row

    $row.logicalcpus = $vmhost.numcpu * 2;

    # Add the number of allocated vCPUs to the row

    $row.cpusassigned = [int]$cpumeasure.sum;

    # Get the overcommitment level as a percentage

    $perc = [int](($cpumeasure.sum / $row.logicalcpus)*100);

    # Warn if overcommitted

    if ($perc -gt 100) {

        $row.Overcommited = "YES - " + $perc + "%";

    }

    else {

        $row.Overcommited = "No";

    }

# Add the current row to the table

$table = $table + $row;

}

# Display the table

$table | out-gridview;

Thanks

V

Tags (1)
1 Solution

Accepted Solutions
Leadership
Leadership

Sure, try like this

Connect-VIServer "vc.com"


# Define an empty table

$table = @()


Get-Cluster -PipelineVariable cluster | ForEach-Object -Process {

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {


        # Create an empty row

        $row = "" | Select-Object Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

        # Add the clustername to the row

        $row.Cluster = $cluster.Name


        # Add the hostname to the row

        $row.Hostname = (($vmhost.name).split("."))[0]

        # Add the number of logical CPUs to the row

        $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum


        # Get the number of vCPUs assigned

        $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

        if ($row.CPUsAssigned -eq $null) {

            $row.CPUsAssigned = 0

        }


        # Get the CPU overcommitment level as a percentage

        $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

        # Warn if CPU overcommitted

        if ($perc -gt 100) {

            $row.CPUOvercommited = "YES - " + $perc + "%"

        }

        else {

            $row.CPUOvercommited = "No"

        }


        # Add the available memory to the row

        $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)


        # Get the memory assigned

        $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

        if ($row.MemoryAssignedGB -eq $null) {

            $row.MemoryAssignedGB = 0

        }

        # Get the memory overcommitment level as a percentage

        $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

        # Warn if memory overcommitted

        if ($perc -gt 100) {

            $row.MemoryOvercomitted = "YES - " + $perc + "%"

        }

        else {

            $row.MemoryOvercomitted = "No"

        }


        # Add the current row to the table

        $table = $table + $row

    }

}

# Save the table to CSV

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


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

View solution in original post

11 Replies
Leadership
Leadership

Try like this

# Connect to vCenter using passthrough credentials

Connect-VIServer "vc.com"


# Get a list of hosts in a specific cluster (omit the get-cluster to get all hosts, and just use get-vmhost)

$hosts = get-cluster "MYCLUSTER" | Get-VMHost


# Define an empty table

$table = @()


# Loop through each host found

foreach ($vmhost in $hosts) {

    # Create an empty row

    $row = "" | Select-Object Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

    # Add the hostname to the row

    $row.Hostname = (($vmhost.name).split("."))[0]

    # Add the number of logical CPUs to the row

    $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum

    # Get the number of vCPUs assigned

    $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

    if ($row.CPUsAssigned -eq $null) {

        $row.CPUsAssigned = 0

    }


    # Get the CPU overcommitment level as a percentage

    $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

    # Warn if CPU overcommitted

    if ($perc -gt 100) {

        $row.CPUOvercommited = "YES - " + $perc + "%"

    }

    else {

        $row.CPUOvercommited = "No"

    }


    # Add the available memory to the row

    $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)

    # Get the memory assigned

    $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

    if ($row.MemoryAssignedGB -eq $null) {

        $row.MemoryAssignedGB = 0

    }

    # Get the memory overcommitment level as a percentage

    $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

    # Warn if memory overcommitted

    if ($perc -gt 100) {

        $row.MemoryOvercomitted = "YES - " + $perc + "%"

    }

    else {

        $row.MemoryOvercomitted = "No"

    }


    # Add the current row to the table

    $table = $table + $row

}

# Save the table to CSV

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


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

0 Kudos
Expert
Expert

LucD,

Thanks for quick turn around. My apologies, i forgot to mention. Can we get the report for all the cluster in a vCenter  instead of specific cluster ?

Thanks

V

0 Kudos
Leadership
Leadership

Sure, try like this

Connect-VIServer "vc.com"


# Define an empty table

$table = @()


Get-Cluster -PipelineVariable cluster | ForEach-Object -Process {

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {


        # Create an empty row

        $row = "" | Select-Object Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

        # Add the clustername to the row

        $row.Cluster = $cluster.Name


        # Add the hostname to the row

        $row.Hostname = (($vmhost.name).split("."))[0]

        # Add the number of logical CPUs to the row

        $row.LogicalCPUs = ($vmhost.numcpu | Measure-Object -Sum).Sum


        # Get the number of vCPUs assigned

        $row.CPUsAssigned = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property numcpu -Sum).Sum

        if ($row.CPUsAssigned -eq $null) {

            $row.CPUsAssigned = 0

        }


        # Get the CPU overcommitment level as a percentage

        $perc = [int](($row.CPUsAssigned / $row.LogicalCPUs) * 100)

        # Warn if CPU overcommitted

        if ($perc -gt 100) {

            $row.CPUOvercommited = "YES - " + $perc + "%"

        }

        else {

            $row.CPUOvercommited = "No"

        }


        # Add the available memory to the row

        $row.MemoryGB = [math]::Round(($vmhost.MemoryTotalGB | Measure-Object -Sum).Sum, 1)


        # Get the memory assigned

        $row.MemoryAssignedGB = ($vmhost | Get-VM | Where-Object { $_.powerstate -eq "poweredon" } | Measure-Object -Property MemoryGB -Sum).Sum

        if ($row.MemoryAssignedGB -eq $null) {

            $row.MemoryAssignedGB = 0

        }

        # Get the memory overcommitment level as a percentage

        $perc = [int](($row.MemoryAssignedGB / $row.MemoryGB) * 100)

        # Warn if memory overcommitted

        if ($perc -gt 100) {

            $row.MemoryOvercomitted = "YES - " + $perc + "%"

        }

        else {

            $row.MemoryOvercomitted = "No"

        }


        # Add the current row to the table

        $table = $table + $row

    }

}

# Save the table to CSV

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


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

View solution in original post

Expert
Expert

Thanks LucD.  It worked  smoothly.

Thanks

V

0 Kudos
Virtuoso
Virtuoso

Hi LucD​,

Is there any reason why the result is duplicating like this:

  

ClusterHostname
PROD1172.135.25.4
PROD1172.135.25.5
PROD1192.168.25.3
PROD1192.168.25.4
PROD1192.168.25.5
PROD1192.168.25.6
PROD1192.168.25.7
PROD2172.135.25.4
PROD2172.135.25.5
PROD2192.168.25.3
PROD2192.168.25.4
PROD2192.168.25.5
PROD2192.168.25.6
PROD2192.168.25.7

Where it should look like these below:

  

ClusterHostname
PROD1172.135.25.4
PROD1172.135.25.5
PROD2192.168.25.3
PROD2192.168.25.4
PROD2192.168.25.5
PROD2192.168.25.6
PROD2192.168.25.7
/* Any kind of comment or input would be greatly appreciated */
0 Kudos
Leadership
Leadership

Unless you have multiple connections to the same vCenter open, it shouldn't.

Check what is in $global:defaultVIServers


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

Virtuoso
Virtuoso

So far only one VCenter returned.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
Leadership
Leadership

Found the issue, the Get-VMHost line should be

    Get-VMHost -Location $cluster -PipelineVariable vmhost | ForEach-Object -Process {

I corrected the code above as well.


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

Virtuoso
Virtuoso

Yes, that does work great.

Thanks, Luc.

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
Virtuoso
Virtuoso

How can I add the VCenter hostname next to the VMHost/ESXi servers in a row when I supply more than 2 VCenter ?

$VcenterServerName = "VCSA-1", "VCSA-2"

$paramConnectVIServer = @{

Server   = $VcenterServerName

    Credential = Get-Credential -Message "Please enter your Admin / service account"

}

Connect-VIServer @paramConnectVIServer

/* Any kind of comment or input would be greatly appreciated */
0 Kudos
Leadership
Leadership

Add the vCenter property to the $row, then use the [uri] type to get the vCenter hostname.

$row = "" | Select-Object vCenter, Cluster, Hostname, LogicalCPUs, CPUsAssigned, CPUOvercommited, MemoryGB, MemoryAssignedGB, MemoryOvercomitted

$row.vCenter = ([uri]$vmhost.ExtensionData.Client.ServiceUrl).Host


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

0 Kudos