I have an array, $bigList = @()
it contains for example
VMname | PowerState | Owner | Status |
---|---|---|---|
MyServer1 | PoweredOn | J.Bloggs | Installed |
MyServer2 | PoweredOff | M.Smythe | Decommissioned |
MyServer3 | PoweredOff | M.Smythe | Installed |
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 Name | Owner |
---|---|
System.Object[] | System.Object[] |
Thanks
You should be able to do
Select VMname,Status)
$smallList | export-CSV -path c:\Temp\smallList.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 | Export-Csv -Path c:\Temp\smallList.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
You should be able to do
Select VMname,Status)
$smallList | export-CSV -path c:\Temp\smallList.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, that worked perfectly