Powershell将PSCustomObjects数组转换为CSV

hvvq6cgz  于 2023-02-27  发布在  Shell
关注(0)|答案(3)|浏览(171)

我正在使用Blobtrigger Azure函数,我想处理来自Blobstorage容器的输入.csv,然后将输出.csv写入另一个Blobstorage容器。
我正在阅读我的输入并填充一个伪数组,我想将其转换为csv文件,如下所示:

# 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
}

现在$new_users.GetType()说它是一个System. Array。$new_users[1].GetType()说它是一个PSCustomObject(System.Object)。当我输出它的时候,这个自定义对象看起来像这样:

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          : tgiorgietto0@prweb.com.fake
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  :

当我尝试从我的自定义对象的$new_users数组中创建csv时,执行以下操作:

$testCsv = ConvertTo-Csv -InputObject $new_users -NoTypeInformation

当我查看文件内容时,我只看到元数据乱码:

["\"Length\",\"LongLength\",\"Rank\",\"SyncRoot\",\"IsReadOnly\",\"IsFixedSize\",\"IsSynchronized\",\"Count\"","\"4\",\"4\",\"1\",\"System.Object[]\",\"False\",\"True\",\"False\",\"4\""]

现在,当我只导出$new_users的一个元素时(通过使用$new_users[1]索引数组),它实际上包含以下形式的数据:1个字符串用于PSCustomObject的所有键,1个字符串用于PSCustomObject的所有值。

["\"givenName\",\"surname\",\"email\",\"companyName\",\"country\",\"mobilePhone\",\"department\",\"officeLocation\",\"city\",\"postalCode\",\"streetAddress\"","\"Marrrrly\",\"Giorgietto\",\"tgiorrrrgietto0@prweb.com.fake\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\""]

显然,我希望输出CSV看起来与输入CSV一样:

givenName,surname,email,companyName,country,mobilePhone,department,officeLocation,city,postalCode,streetAddress
datapoint1.givenName,datapoint1.surname,...
datapoint2.givenName,...

有人知道如何解决这个问题吗?非常感谢您提前抽出时间!

ztmd8pv5

ztmd8pv51#

“*$InputBlob也是csv文件 *”

显然它是 * 不是 *(因为它与一个csv一起工作得很好)...
请在您的问题中添加您的blob文件的简单示例(例如使用$Blob |ConvertTo-Hex)或创建Minimal, Reproducible Example

function test {
    # 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
    }
    $new_users
}

$Csv = @'
a,b
1,2
3,4
'@
$Blob = [system.Text.Encoding]::UTF8.GetBytes($Csv)
Write-Host "`$Blob = [byte[]]($($Blob -Join ', ')) # use for MCVE"
Test $Blob

产量:

$Blob = [byte[]](97, 44, 98, 10, 49, 44, 50, 10, 51, 44, 52) # use for MCVE

PowerShell Blob trigger function Processed blob! Name:  Size: 11 bytes

a b
- -
1 2
3 4

顺便说一句:try should I avoid using the increase assignment operator (+=) to create a collection

frebpwbc

frebpwbc2#

看起来你的输入blob好像是csv文件的内容,被表示为一个字符串,或者一个字符串数组。如果是这样,你需要做的就是在文件中创建一个副本:

$InputBlob | Out-File myfile.csv

不需要转换。如果要将其转换为PSCustomObject数组,则可以执行以下操作:

$myarray = $InputBlob | ConvertFrom-Csv

如果您输入的blob是其他内容,请修改您的问题。

laik7k3q

laik7k3q3#

最后,McLaytons信息的组合:$csv = $new_users | ConvertTo-Csv以及输出绑定语句Push-OutputBinding -name outputBlob -value ($csv -join "n")`帮助我以正确的格式输出。在csv混合在[ ]中之前,所有内容都写入到单个字段中。感谢:https://social.technet.microsoft.com/Forums/en-US/1041c527-8a45-4e63-8bc4-937bae40d852/azure-function-writing-to-blob-storage-using-output-binding-is-mangling-the-output?forum=windowsazuredata

相关问题