1 Reply Latest reply on Jan 10, 2019 12:21 PM by LucD

    retrieve list of VMs and disk sizes for Inventory purposes

    ppgdtap Lurker

      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;
          } 
      }
      
        • 1. Re: retrieve list of VMs and disk sizes for Inventory purposes
          LucD Guru
          Community WarriorsvExpertUser Moderators

          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