VMware Cloud Community
COS
Expert
Expert
Jump to solution

Get VM's and their vcpu, vcores and memory

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

$result = @()

$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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

$result = @()

$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

0 Kudos
COS
Expert
Expert
Jump to solution

Thanks, that worked.

I have been gone from scripting for quite a while so I have more question.

I'll start another thread.

0 Kudos