I am working with a Blobtrigger Azure Function and I want to process an input.csv from a Blobstorage Container and then write an output.csv to another Blobstorage Container.
I am reading my input and filling a dummy array which I want to convert to a csv-file as follows:
# Input bindings are passed in via param block.
param([byte[]] $InputBlob, $TriggerMetadata)
# Write out the blob name and size to the information log.
Write-Host "PowerShell Blob trigger function Processed blob! Name: $($TriggerMetadata.Name) Size: $($InputBlob.Length) bytes"
$TempFile = New-TemporaryFile
[io.file]::WriteAllBytes($TempFile.FullName, $InputBlob)
$dataSet = Import-Csv $TempFile.FullName
$new_users = @()
foreach($data in $dataSet){
$new_users += $data
}
Now $new_users.GetType() says it is a System.Array. $new_users[1].GetType() says it is a PSCustomObject (System.Object). The custom object looks like this when I output it:
2023-02-22T09:33:42Z [Information] OUTPUT: givenName : Marly
2023-02-22T09:33:42Z [Information] OUTPUT: surname : Giorgietto
2023-02-22T09:33:42Z [Information] OUTPUT: email : [email protected]
2023-02-22T09:33:42Z [Information] OUTPUT: companyName :
2023-02-22T09:33:42Z [Information] OUTPUT: country :
2023-02-22T09:33:42Z [Information] OUTPUT: mobilePhone :
2023-02-22T09:33:42Z [Information] OUTPUT: department :
2023-02-22T09:33:42Z [Information] OUTPUT: officeLocation :
2023-02-22T09:33:42Z [Information] OUTPUT: city :
2023-02-22T09:33:42Z [Information] OUTPUT: postalCode :
2023-02-22T09:33:42Z [Information] OUTPUT: streetAddress :
When I try to create a csv from my $new_users array of custom objects by doing this:
$testCsv = ConvertTo-Csv -InputObject $new_users -NoTypeInformation
I only get meta-data gibberish when I view the file content:
["\"Length\",\"LongLength\",\"Rank\",\"SyncRoot\",\"IsReadOnly\",\"IsFixedSize\",\"IsSynchronized\",\"Count\"","\"4\",\"4\",\"1\",\"System.Object[]\",\"False\",\"True\",\"False\",\"4\""]
Now when I only export one of the elements of $new_users by indexing the array with $new_users[1], it actually contains data in the form: 1 string for all the keys of the PSCustomObject and 1 string for all the values of the PSCustomObject.
["\"givenName\",\"surname\",\"email\",\"companyName\",\"country\",\"mobilePhone\",\"department\",\"officeLocation\",\"city\",\"postalCode\",\"streetAddress\"","\"Marrrrly\",\"Giorgietto\",\"[email protected]\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\""]
Obviously, I want my output CSV to look just like my input CSV:
givenName,surname,email,companyName,country,mobilePhone,department,officeLocation,city,postalCode,streetAddress
datapoint1.givenName,datapoint1.surname,...
datapoint2.givenName,...
Does anyone have an idea how to fix this? Thank you very much for your time in advance!
$InputBlob? Please understand that you can't just write a blob (byte array) to a file and expect it to be a validcsv(Comma-separated values) file.$csv = ConvertTo-Csv -InputObject $new_userstreats the parameter as a single data row so it's serializing your array and putting its properties (Length, Type, SyncRoot, etc) in the csv. If you want to convert the items in the array into csv rows try this instead....$csv = $new_users | ConvertTo-Csv$csv = $new_users | ConvertTo-Csvas well as the output binding statementPush-OutputBinding -name outputBlob -value ($csvstring -join "`n")helped me to output in the right format. Before the csv was mingled in [ ] and all the content was written to a single field. thanks to: social.technet.microsoft.com/Forums/en-US/…