Expanding an array before Export-csv.

New Contributor

Hi All.


I have spent a day scouring the internet for a solution, but can't seem to get this right. I am looking to turn this:


NodeName            IOSRelease            CVEs
--------                   ----------               ----
xxx-cen-dcn-5        12.2(35)SE5           {CVE-2019-16009, CVE-2019-12665, CVE-2017-3881}
xxx-nth-dcn-4        12.2(35)SE5           {CVE-2019-16009, CVE-2019-12665, CVE-2017-3881}


Into this:


NodeName            IOSRelease            CVEs
--------                   ----------               ----
xxx-cen-dcn-5        12.2(35)SE5           {CVE-2019-16009}

xxx-cen-dcn-5        12.2(35)SE5           {CVE-2019-12665

xxx-cen-dcn-5        12.2(35)SE5           {CVE-2017-3881}
xxx-nth-dcn-4        12.2(35)SE5           {CVE-2019-16009,
xxx-nth-dcn-4        12.2(35)SE5           {CVE-2019-12665}

xxx-nth-dcn-4        12.2(35)SE5           {CVE-2017-3881}


Any pointers would be greatly appreciated.






3 Replies
This is a nested array, and to do it your way, You need a foreach loop to go inside the CVEs and assign each item to the NodeName, so something like Foreach inside a foreach.
Hi :)
Thank you for your response. I thought something like that, but couldn't get my head around it.

I already have a foreach loop to create this output:

$nodes = Import-Csv -Path $Path

$output = foreach ($node in $nodes) {

$apiurl = 'https://api.cisco.com/security/advisories/ios?version=' + $node.IOSVersion

$advisory = Invoke-RestMethod $apiurl -Method 'GET' -Headers $headers -UseBasicParsing

New-Object -TypeName PSObject -Property @{
NodeName = $node.DisplayName
IOSRelease = $node.IOSVersion
CVEs = ($advisory.advisories.cves -Join ',')
Score = ($advisory.advisories.cvssbaseScore -Join ',')
url = ($advisory.advisories.cvrfUrl -Join ',')

} | Select-Object NodeName,IOSRelease,CVEs,Score,url
$output | Export-Csv -Path 'C:\CVE\NSCiscoNodesupdatedlist.csv'

So should I have another foreach loop, with append, where I write it to the csv?
You will need to another foreach inside the first one, that will get the information for $node and then loop through its properties, assign them to the PSCustom item and then proceed to the next one.