I've looked into this issue as others have experienced it as well but I can't really make sense of the issue at hand in order to actually implement any fixes which have been maybe offered up. Here is what I am dealing with specifically.
I am fetching information from an API, parsing through and building my own hashtables which then get pushed into an array. I want to then export that array of hashes as a simple CSV. I have done virtually the same exact setup in another script and it worked without issue, I am unable to figure out what I am doing wrong here...
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$user = '*****'
$pass = ConvertTo-SecureString '******' -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
$base = '*********'
$url = '*********'
$content = @()
$res = $null
function fetch_data([String] $path) {
$req = Invoke-WebRequest -Credential $cred -Uri $path -Method GET
$global:res = ConvertFrom-Json $req
}
function process_data([String] $arg) {
fetch_data($arg)
foreach($i in $res.data) {
$subscription = @{
'id' = $i.id
'name' = $i.name
'sub_name' = $i.subscriptionFormReference.name
'owner_id' = $i.owner.targetName
'owner_firstName' = $i.owner.firstName
'owner_lastName' = $i.owner.lastName
'owner_recipientType' = $i.owner.recipientType
}
foreach($x in $i.data.criteria.data) {
$sub.lob_name = $x.name
$sub.lob_owner = $x.operator
$sub.lob_values = $x.value
}
$subscription = ConvertTo-Json $subscription
$global:content.add($subscription)
}
if ($res.links.next) {
$link = $base + $res.links.next
process_data($link)
}
}
process_data($url)
Write-Host $content
$content | Export-CSV "\\*******\TEST_CSV.csv" -NoTypeInformation
The outputted file is just a single column with numbers going down as long as the number of objects (hashes I built) that exist in the array above. I'm assuming this is the length of the object in string form maybe and that is what it is recognizing so it may be a formatting issue?
Now, if I print the array to the screen inside the script, I see all the hashtables without issue. The only diff being that they are {}
rather than @{}
like in the other script I mentioned earlier which works without issue.
Any ideas?