CSV output is not in specific columns

Hi,

Script below, I need some help because the output to CSV is not clean, meaning I want the EID’s to be in one column and the vendorname to be in the column next to EID. Thanks!

$obj = @()

$s = NETAssignmentsModified

$s | ConvertTo-Json

Foreach($_ in $s){

}

$obj += [PSCustomObject]@{
#breakout all the essential object items
EID = $.specialbill.id -as [String]
Vendor = $
.vendorName -as [String]

}

$obj|export-csv -path “c:\users\bwiley\MyData$((Get-Date).ToString(“MMddyyyy”)).csv” -NoTypeInformation

When you post code, error messages, sample data or console output format it as code, please.
In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”. You can go back edit your post and fix the formatting - you don’t have to create a new one.
Thanks in advance.

What is “NETAssignmentsModified”? Is it a string? What result would you expect when you convert a string into JSON? You should not use $_ as a variable for yourself. It’s the pipeline variable and should not be used for something else. If you had saved a collection you could iterate over in a variable named $s your code could look like this:

$Result = 
Foreach ($Item in $s) {
    [PSCustomObject]@{
        EID    = $Item.specialbill.id -as [String]
        Vendor = $Item.vendorName -as [String]
    }
}

$Result | 
Export-Csv -Path "c:\users\bwiley\MyData$((Get-Date).ToString('MMddyyyy')).csv" -NoTypeInformation
Hello,



Below script is working now but the data in the CSV is all duplicates of the first record/object. Curious why the data is repeating when it should be unique record for each object in the base variable.



$obj = @()

$base = NETAssignmentsModified

For ($i=1; $i -le 20000; $i++){

Foreach($b in $base){

}

$result = $obj += [PSCustomObject]@{
#breakout all the essential object items
EID = $b.specialbill.id -as [String]
Vendor = $b.vendorName -as [String]

}

$result|export-csv -path "c:\users\BWiley\MyData$((Get-Date).ToString("MMddyyyy")).csv" -NoTypeInformation

}

So it’s obviously NOT working. :wink: … again … what is “NETAssignmentsModified”??

$base = NETAssignmentsModified

$result =

Foreach ($b in $base) {

[PSCustomObject]@{

    EID    = $b.specialbill.id -as [String]

    Vendor = $b.vendorName -as [String]

}

}

$result |

Export-Csv -Path “c:\users\BWiley\MyData$((Get-Date).ToString(‘MMddyyyy’)).csv” -NoTypeInformation

NetAssignmentsModified is a local PowerShell function containing a REST call to an endpoint that gives us the records we need.