piercj2
Enthusiast
Enthusiast

Create new array from existing array data

Jump to solution

I have an array, $bigList = @()

it contains for example

VMnamePowerStateOwnerStatus
MyServer1PoweredOnJ.BloggsInstalled
MyServer2PoweredOffM.SmytheDecommissioned
MyServer3PoweredOffM.SmytheInstalled

I want to create a second array if PowerState=PoweredOff and Status=Decommissioned

The second Array contents will be output as a .CSV

What i've come up with is

$smallList = @()

foreach ($row in $bigList){

     if(($bigList.PowerState -match 'PoweredOff') and ($bigList.Status -match 'Decommissioned)){

          $smallListProperty = [ordered] @{

               'VM Name' = $bigList.VMname

               'Owner' = $bigList.Owner

          }

     $smallList += new-object -TypeName psobject -Property $smallListProperty

}

$smallList | export-CSV -path c:\Temp\smallList.csv

It kind of works but the output i'm getting is

VM NameOwner
System.Object[]System.Object[]

Thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You should be able to do

$smallList = @($bigList.Where({$_.PowerState -match 'PoweredOff' -and $_.Status -match 'Decommissioned'}) |

    Select VMname,Status)

$smallList | export-CSV -path c:\Temp\smallList.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership

You could use the Where method on the $bigList variable.

Something like this.

I forced an array (although not really needed) should there be only 1 row passing the Where-clause.

$smallList = @($bigList.Where({$_.PowerState -match 'PoweredOff' -and $_.Status -match 'Decommissioned'}))

$smallList | Export-Csv -Path c:\Temp\smallList.csv


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

0 Kudos
piercj2
Enthusiast
Enthusiast

that's really close but, if the conditions are met, i.e.  PowerState=PoweredOff and Status=Decommissioned, that entire row is copied from $bigList to $smallList.

I'm trying to include only specific columns in $smallList. e.g. VMName and PowerState or, VMName and Status

I've tried

$smallList = @($bigList.Where({$_.PowerState -match 'PoweredOff' -and $_.Status -match 'Decommissioned'}))

$smallList | select $smallList.VMname, $smallList.PowerState Export-Csv -Path c:\Temp\smallList.csv

but it fails

I've also tried

$smallList = @($bigList.Where({$_.PowerState -match 'PoweredOff' -and $_.Status -match 'Decommissioned' select $_.'VM Name', $_.'CI Status'}))

$smallList

| select $smallList.VMname, $smallList.PowerState

Export-Csv

-Path c:\Temp\smallList.csv

But that REALLY fails

0 Kudos
LucD
Leadership
Leadership

You should be able to do

$smallList = @($bigList.Where({$_.PowerState -match 'PoweredOff' -and $_.Status -match 'Decommissioned'}) |

    Select VMname,Status)

$smallList | export-CSV -path c:\Temp\smallList.csv -NoTypeInformation -UseCulture


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

0 Kudos
piercj2
Enthusiast
Enthusiast

Thanks Luc, that worked perfectly

0 Kudos