VMware Cloud Community
ppgdtap
Enthusiast
Enthusiast
Jump to solution

retrieve list of VMs and disk sizes for Inventory purposes

Our vCenter is running 6.5 and I've managed to find the fields I need, but I am not sure:

  1. how to export these to CSV - the value in {} seems to need to be converted?
  2. also the results appear in blocks as opposed to a table

VMName    : pnlvspr3310

VMFQDN    : pnlvspr3310.Mgmt.PPSEC.Local

OS        : pnlvspr3310:Microsoft Windows Server 2012 (64-bit)

IPAddress : {169.254.1.12, 10.250.7.68}

CPU Cores : 4

RAM       : 32

Disks     : {Capacity:64421298176, FreeSpace:53013970944, Path:D:\, Capacity:53683879936, FreeSpace:35310731264,             Path:L:\, Capacity:322119335936, FreeSpace:156260106240, Path:S:\, Capacity:269505986560,             FreeSpace:213339930624, Path:P:\...}

$varVM = Get-VM

foreach ($vm in $varVM){

    $guestDetails = Get-VMGuest -VM $vm

    [PSCustomObject]@{

        'VMName' = $guestDetails.VM;

        'VMFQDN' = $guestDetails.HostName;

        'OS' = $vm.Guest;

        'IPAddress' = $guestDetails.IPAddress;

        'CPU Cores' = $vm.NumCpu;

        'RAM' = $vm.MemoryGB;

        'Disks' = $guestDetails.Disks;

    }

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are a couple of issues here.

- with a ForEach like that the code block that follows doesn't place anything on the pipeline. You better use a ForEach-Object

- the disks are an array, where each entry has multiple properties. When you assign an object like that it will be represented as a {...} on screen

- an Export-Csv can not handle complex objects in the input. You have to provide scalars. One way of doing that is to join multiple values into 1 string

- you can tackle the other properties on the Disk array in a similar way

Get-VM |

   ForEach-Object -Process {

   [PSCustomObject]@{

   'VMName' = $_.Name

   'VMFQDN' = $_.Guest.HostName

   'OS' = $_.Guest.OSFullName

   'IPAddress' = $_.Guest.IPAddress[0]

   'CPU Cores' = $_.NumCpu

   'RAM' = $_.MemoryGB

   'DisksPaths' = $_.Guest.Disks.Path -join '|'

   }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

There are a couple of issues here.

- with a ForEach like that the code block that follows doesn't place anything on the pipeline. You better use a ForEach-Object

- the disks are an array, where each entry has multiple properties. When you assign an object like that it will be represented as a {...} on screen

- an Export-Csv can not handle complex objects in the input. You have to provide scalars. One way of doing that is to join multiple values into 1 string

- you can tackle the other properties on the Disk array in a similar way

Get-VM |

   ForEach-Object -Process {

   [PSCustomObject]@{

   'VMName' = $_.Name

   'VMFQDN' = $_.Guest.HostName

   'OS' = $_.Guest.OSFullName

   'IPAddress' = $_.Guest.IPAddress[0]

   'CPU Cores' = $_.NumCpu

   'RAM' = $_.MemoryGB

   'DisksPaths' = $_.Guest.Disks.Path -join '|'

   }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos