VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Adding properties to array elements

I have a report that inputs values from a csv.  The csv has 4 columns: vmname, dateCreated, User, vmhost

Using this CSV, I run the following report to see if the VMs are in vCenter, and I save the vms that still exist to a new outputfile:

$array = @()

Import-Csv c:\inputfile.csv -UseCulture | %{

  Try {

      Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null 

      } 

      Catch {return 

      } 

      $array += $_ 

      }

      $array | Export-Csv c:\outputfile.csv -NoTypeInformation -UseCulture

Can I modify this report to add to each array element the amount of RAM that the VM currently has and output that as an additional column to my outputfile? 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can use the Add-Member for that.

Something like this for example

$array = @()

Import-Csv c:\inputfile.csv -UseCulture | %{

  Try {

      Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null

      }

      Catch {return

      }

      $array +=  ($_ | Add-Member -Name NewColumn -Value 123 -MemberType NoteProperty -Passthru)

}

$array | Export-Csv c:\outputfile.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Add-Member for that.

Something like this for example

$array = @()

Import-Csv c:\inputfile.csv -UseCulture | %{

  Try {

      Get-VM -Name $_.VMName -ErrorAction Stop | Out-Null

      }

      Catch {return

      }

      $array +=  ($_ | Add-Member -Name NewColumn -Value 123 -MemberType NoteProperty -Passthru)

}

$array | Export-Csv c:\outputfile.csv -NoTypeInformation -UseCulture


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

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

That works thanks again

0 Kudos