Sunday, May 27, 2018

VMware script to get CPU core details.

Save the given file in machine and give extension ".ps1"

$myCol = @()
Foreach ($cluster in Get-Cluster)
    {
        Foreach($vmhost in ($cluster | Get-VMHost))
        {
            Foreach($vm in (Get-VM -Location $vmhost)){
                $VMView = $vm | Get-View
                $VMSummary = "" | Select ClusterName,HostName,VMName,VMSockets,VMCores,CPUSockets,CPUCores
                $VMSummary.ClusterName = $cluster.Name
                $VMSummary.HostName = $vmhost.Name
                $VMSummary.VMName = $vm.Name
                $VMSummary.VMSockets = $VMView.Config.Hardware.NumCpu
                $VMSummary.VMCores = $VMView.Config.Hardware.NumCoresPerSocket
                $VMSummary.CPUSockets = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuPackages
                $VMSummary.CPUCores = $vmhost.ExtensionData.Hardware.CpuInfo.NumCpuCores
                $myCol += $VMSummary
            }
        }
    }
$myCol | Export-csv reports.csv

Now connect vcenter using vmware powercli and execute the script.


Another method to get done this is follow below mentioned file save and execute the same to get the data.


Don't forget to change the given lines with your scenario.



  • $vcenter = vc01.vcdx56.com
  • $vcenteruser = vcdx56\magnus”
  • $vcenterpw = “not secret”
  • $reportdir = c:\vSpherereport
  • $filename = c:\vSphere$date.html
  • $basefile = c:\vSpherereportCPU-oversubscription.html

 # The script will calculate the ESXi host CPU core to VM vCPU oversubscription and create a HTML report

#
# Version 1.0 Magnus Andersson RTS
#————————————————
# Start of script parameters section
#
# vCenter Server configuration
$vcenter = “vcenter-ip-address“
$vcenteruser = “Administrator@vsphere.local“
$vcenterpw = "password"
#
$date=get-date -uformat %Y-%m-%d
#
# Define report directory
$reportdir = "c:\temp\vSpherereport"
#
# Define file name
$filename = "c:\temp\vSphere$date.html"
#
# Define base file
$basefile = "c:\temp\vSpherereportCPU-oversubscription.html"
#
# End of script parameter section
#—————————————— #
#
# Connect to vCenter Server
connect-viserver $vcenter -User $vcenteruser -Password $vcenterpw
#
function Get-Cluster-CPUMem {
$table = @()
foreach($clust in (get-cluster | Sort-Object Name)) {
$esx = $clust | Get-VMHost
$clu = "" | select Cluster, "ESXi host num CPU core", "VM vCPU", "VM vCPU on", "Percent of vCPU on"
$vms = $clust | get-vm
$clu.Cluster = $clust.Name
$clu."ESXi host num CPU core" = ($clust | Get-vmhost | Measure-Object -Sum NumCPU).Sum
$clu."VM vCPU" = ($vms | Measure-Object -Sum NumCPU).Sum
$clu."VM vCPU on" = ($vms | ?{$_.PowerState -eq "PoweredOn"} | Measure-Object -Sum NumCPU).Sum
$clu."Percent of vCPU on" = "{0:P0}" -f ($clu."VM vCPU on" / $clu."ESXi host num CPU core")
$table += $clu
}
$clu = "" | select Cluster, "ESXi host num CPU core", "VM vCPU", "VM vCPU on", "Percent of vCPU on"
$clu.Cluster = "Total"
$clu."ESXi host num CPU core" = ($table | Measure-Object -property "ESXi host num CPU core" -sum).sum
$clu."VM vCPU" = ($table | Measure-Object -property "VM vCPU" -sum).sum
$clu."VM vCPU on" = ($table | Measure-Object -property "VM vCPU on" -sum).sum
$clu."Percent of vCPU on" = "{0:P0}" -f ($clu."VM vCPU on" / $clu."ESXi host num CPU core")
$table += $clu
$table
}
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 4px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 4px;border-style: solid;border-color: black;text-align:center;}"
$a = $a + "</style>"
Get-Cluster-CPUMem | ConvertTo-HTML -head $a -Title "CPU oversubscription report $date" | Out-File $filename
Remove-Item $basefile
Copy-Item $filename $basefile
if ($filename -like "*-01.*") {
Copy-Item $filename $reportdir
}
Remove-item $filename

No comments:

Post a Comment