I have the below script....
$result = @()
$vms = Get-view -ViewType VirtualMachine
foreach ($vm in $vms) {
$obj = new-object psobject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $vm.Name
$obj | Add-Member -MemberType NoteProperty -Name CPUs -Value $vm.config.hardware.NumCPU
$obj | Add-Member -MemberType NoteProperty -Name Sockets -Value ($vm.config.hardware.NumCPU/$vm.config.hardware.NumCoresPerSocket)
$obj | Add-Member -MemberType NoteProperty -Name CPUPersocket -Value $vm.config.hardware.NumCoresPerSocket
$result += $obj
}
$result | Out-File c:\temp\cputest.txt
I'm getting the below error....
Attempted to divide by zero.
At line:13 char:1
+ $obj | Add-Member -MemberType NoteProperty -Name Sockets -Value ($vm. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException
How can I handle the error? The math indicates a VM does not have a CPU?
Thanks
The issue might be that for NumCoresPerSocket it says in VirtualHardware "If the value is unset it implies to numCoresPerSocket = 1"
So your code could be something like this
$vms = Get-view -ViewType VirtualMachine
foreach ($vm in $vms) {
if($vm.config.hardware.NumCoresPerSocket -ne $null){
$cores = $vm.config.hardware.NumCoresPerSocket
}
else{
$cores = 1
}
$obj = new-object psobject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $vm.Name
$obj | Add-Member -MemberType NoteProperty -Name CPUs -Value ($vm.config.hardware.NumCPU
$obj | Add-Member -MemberType NoteProperty -Name Sockets -Value ($vm.config.hardware.NumCPU/$cores)
$obj | Add-Member -MemberType NoteProperty -Name CPUPersocket -Value $cores
$result += $obj
}
$result | Out-File c:\temp\cputest.txt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The issue might be that for NumCoresPerSocket it says in VirtualHardware "If the value is unset it implies to numCoresPerSocket = 1"
So your code could be something like this
$vms = Get-view -ViewType VirtualMachine
foreach ($vm in $vms) {
if($vm.config.hardware.NumCoresPerSocket -ne $null){
$cores = $vm.config.hardware.NumCoresPerSocket
}
else{
$cores = 1
}
$obj = new-object psobject
$obj | Add-Member -MemberType NoteProperty -Name ServerName -Value $vm.Name
$obj | Add-Member -MemberType NoteProperty -Name CPUs -Value ($vm.config.hardware.NumCPU
$obj | Add-Member -MemberType NoteProperty -Name Sockets -Value ($vm.config.hardware.NumCPU/$cores)
$obj | Add-Member -MemberType NoteProperty -Name CPUPersocket -Value $cores
$result += $obj
}
$result | Out-File c:\temp\cputest.txt
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks, that worked.
I have been gone from scripting for quite a while so I have more question.
I'll start another thread.