Forum Discussion
MalHenderson
Apr 30, 2021Copper Contributor
Expanding an array before Export-csv.
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 -------- ...
MalHenderson
Apr 30, 2021Copper Contributor
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?
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?
farismalaeb
May 04, 2021Iron Contributor
Yes,
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.
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.