VMware Cloud Community
COS
Expert
Expert
Jump to solution

Add Memory assigned in my script and set output to CSV

I have the script below...

$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

    $obj | Add-Member -MemberType NoteProperty -Name RAMAssigned -Value $VM.MemoryGB

    $result += $obj

}

$result | Out-File c:\temp\cputest.txt

It runs but Memory is not populating. I must calling the value/property wrong.

Also, how can I format the output to CSV?

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this you mean?

$obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value $vm.Config.GuestFullName

The short ID is

$obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value $vm.Config.GuestId


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Since you use Get-View to retrieve the VMs, you are working with the vSphere object VirtualMachine.

In contrast, the Get-VM cmdlet returns a .NET object VirtualMachine, which is not the same.

The Value should be done like this for a vSphere object.

$obj | Add-Member -MemberType NoteProperty -Name RAMAssigned -Value ([math]::Round($vm.Config.Hardware.MemoryMB/1KB))


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
COS
Expert
Expert
Jump to solution

OK, thanks!

The values returned correctly......

ServerName   : goofy

CPUs         : 2

Sockets      : 2

CPUPersocket : 1

RAMAssigned  : 6

ServerName   : pluto

CPUs         : 4

Sockets      : 4

CPUPersocket : 1

RAMAssigned  : 8

Changed my output to below for a CSV....

$result | Export-Csv -Path c:\temp\cputest.txt -NoTypeInformation

Now one more item, how do I get the OS profile that is set for the VM?

$obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value ?????

Thanks

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this you mean?

$obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value $vm.Config.GuestFullName

The short ID is

$obj | Add-Member -MemberType NoteProperty -Name OSProfile -Value $vm.Config.GuestId


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
COS
Expert
Expert
Jump to solution

Dude, I would have never guessed the "$vm.Config.GuestFullName" calls for the Operating System type for the VM..........lmao.

Thanks again!

Reply
0 Kudos