I was provided this excellent script by one of the community members to assist me in auditing our VMs and what CPU resources they are using. I am just hoping someone knows how what line of command to also list the guest OS operating in additions to all of the other details. Also, is it possible to run this against more than one vCenter at a time?
$report = foreach($vc in $global:DefaultVIServers){
Get-VM -Server $vc |
Select @{N='vCenter';E={$vc.Name}},
@{N='VMHost';E={$_.VMHost.Name}},
@{N='VM';E={$_.Name}},
@{N='vCPU';E={$_.NumCpu}},
@{N='CPU sockets';E={$_.NumCpu/$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
@{N='Cores/socket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
You can do that in the script like this
$report = foreach($vc in $global:DefaultVIServers){
Get-VM -Server $vc |
Select @{N='vCenter';E={$vc.Name}},
@{N='VMHost';E={$_.VMHost.Name}},
@{N='VM';E={$_.Name}},
@{N='OS';E={$_.Guest.OSFullName}},
@{N='vCPU';E={$_.NumCpu}},
@{N='CPU sockets';E={$_.NumCpu/$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
@{N='Cores/socket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
PowerState
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
The difference in both scripts is the way they handle the VirtualMachine object(s) that is (are) returned by the Get-VM cmdlet.
In the example you mention the VirtualMachine object is stored in a variable ($vm), and the properties are extracted by referring to them ($vm.PowerState).
My script uses the pipeline to pass the VirtualMachine object(s) to the Select-Object cmdlet.
On a Select-Object cmdlet (short Select), you can specify a property (like PowerState), or you can use a 'calculated property' (like 'CPU sockets').
Both of the methods use the same VirtualMachine object and its properties.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this.
It assumes the VMware Tools are installed.
$report = foreach($vc in $global:DefaultVIServers){
Get-VM -Server $vc |
Select @{N='vCenter';E={$vc.Name}},
@{N='VMHost';E={$_.VMHost.Name}},
@{N='VM';E={$_.Name}},
@{N='OS';E={$_.Guest.OSFullName}},
@{N='vCPU';E={$_.NumCpu}},
@{N='CPU sockets';E={$_.NumCpu/$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
@{N='Cores/socket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
It works like a charm! You are a real time saver (life saver). I can now run this against all of our vCenters and give the report to our MS rep lic rep. I thought this would have been an easy process because I assumed they only wanted to know the CPU and core info on our physical esxi host and other physical servers but at the last min I was asked to pull together report on VMs. Thanks again!
Ok just one more thing :smileyblush:
Where do you get these properties from? I have seen different ways of pulling properties. For example I have a script I was trying to use before that provides the power state of the machine. Is there a same for this one? I highlighted in red the property from another script that gave me the power state of the vms. Is there something similar that can be added to your script? I thought about trying to add that line to your script but it just did seem right. gheesh I have a long way to go
# Get all VMs
$vms = get-vm;
# Loop through each VM found
foreach ($vm in $vms) {
# Get VM details
$vmname = $vm.name;
$Operatingsystem = [string]$vm
$totalcores = [string]$vm.numcpu
$corespersocket = [string]((get-view $vm).config.hardware.numcorespersocket);
$sockets = [string]([int]($totalcores / $corespersocket));
$powerstate = [string]$vm.powerstate;
# Build CSV output
$out = $vmname + "," + $totalcores + "," + $sockets + "," + $corespersocket + "," + $powerstate;
# Output to file
$out >> d:\temp\vms.csv;
}
You can do that in the script like this
$report = foreach($vc in $global:DefaultVIServers){
Get-VM -Server $vc |
Select @{N='vCenter';E={$vc.Name}},
@{N='VMHost';E={$_.VMHost.Name}},
@{N='VM';E={$_.Name}},
@{N='OS';E={$_.Guest.OSFullName}},
@{N='vCPU';E={$_.NumCpu}},
@{N='CPU sockets';E={$_.NumCpu/$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
@{N='Cores/socket';E={$_.ExtensionData.Config.Hardware.NumCoresPerSocket}},
PowerState
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
The difference in both scripts is the way they handle the VirtualMachine object(s) that is (are) returned by the Get-VM cmdlet.
In the example you mention the VirtualMachine object is stored in a variable ($vm), and the properties are extracted by referring to them ($vm.PowerState).
My script uses the pipeline to pass the VirtualMachine object(s) to the Select-Object cmdlet.
On a Select-Object cmdlet (short Select), you can specify a property (like PowerState), or you can use a 'calculated property' (like 'CPU sockets').
Both of the methods use the same VirtualMachine object and its properties.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD!
The script worked perfect and I was able to get the needed info on all of our VMs. We have 13 v Center servers so I had to connect via PowerCLI to each one individually but that was a heck of a lot better then how I started out manually checking each VM.
Thanks again!
You can store the names of the vCenters in a file, and then provided SSO is enabled for the account under which you run the script, you can just use a ForEach loop and do a Connect-VIServer to each vCenter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
