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
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
Try like this
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
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
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
Thanks LucD. It worked smoothly.
Thanks
V
Hi LucD,
Is there any reason why the result is duplicating like this:
Cluster | Hostname |
PROD1 | 172.135.25.4 |
PROD1 | 172.135.25.5 |
PROD1 | 192.168.25.3 |
PROD1 | 192.168.25.4 |
PROD1 | 192.168.25.5 |
PROD1 | 192.168.25.6 |
PROD1 | 192.168.25.7 |
PROD2 | 172.135.25.4 |
PROD2 | 172.135.25.5 |
PROD2 | 192.168.25.3 |
PROD2 | 192.168.25.4 |
PROD2 | 192.168.25.5 |
PROD2 | 192.168.25.6 |
PROD2 | 192.168.25.7 |
Where it should look like these below:
Cluster | Hostname |
PROD1 | 172.135.25.4 |
PROD1 | 172.135.25.5 |
PROD2 | 192.168.25.3 |
PROD2 | 192.168.25.4 |
PROD2 | 192.168.25.5 |
PROD2 | 192.168.25.6 |
PROD2 | 192.168.25.7 |
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
So far only one VCenter returned.
Found the issue, the Get-VMHost line should be
I corrected the code above as well.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, that does work great.
Thanks, Luc.
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
Add the vCenter property to the $row, then use the [uri] type to get the vCenter hostname.
$row.vCenter = ([uri]$vmhost.ExtensionData.Client.ServiceUrl).Host
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference