简体   繁体   English

Powershell - 在不同的列中将数组导出到 CSV

[英]Powershell - Export array to CSV in different columns

I am trying to automate below API calls from a csv file.我正在尝试从 csv 文件自动执行以下 API 调用。

http_uri
/ModuleName/api/12345/moverequest/MoveRequestQueue?batchSize=200
/ModuleName/api/Portal/GetGarageLocations?email=Dummy@mail.com
/ModuleName/api/DeliveryDate/CommitEta?ref=H7J3M1EA4LF
/ModuleName/api/35345/moverequest/MoveRequestQueue?batchSize=500

The output should be like below in a csv file.在 csv 文件中的输出应如下所示。

ScenarioName        Parameter   Value
MoveRequestQueue    batchSize   200
GetGarageLocations  email       Dummy@mail.com
CommitEta           ref         H7J3M1EA4LF
MoveRequestQueue    batchSize   500

I am using below code我正在使用下面的代码

$csv = Import-Csv C:\Powershell\Documents\Source.csv

$scenario   = @()

ForEach ($row in $csv){
$httpuri = $($row.http_uri)


#Iterating through CSV rows and segregate values
if ($httpuri -match "="){
    $equalarr = $httpuri -split '='
    if ($equalarr[0] -match "\?"){
        $questionarr = $equalarr[0] -split '\?'
        $scenarionamearr = $questionarr[0] -split '/'
        $totalelements = $scenarionamearr.Count
        $scenarioname = $scenarionamearr[$totalelements-1]

        $Scenario += $scenarioname
        $Scenario += $questionarr[1]
        $Scenario += $equalarr[1]

        }
    }


}

#Adding columns to csv
$columnName = '"Scenario","Parameter","Value"'
Add-Content -Path C:\Powershell\Documents\Output.csv -Value $columnName

#Writing values to CSV
$Scenario | foreach { Add-Content -Path C:\Powershell\Documents\Output.csv -Value $_ }

But Outout is generated like below但是 Outout 生成如下

 Scenario                   Parameter   Value
 DequeueMoveRequestQueue        
 batchSize      
 200        
 GetCarrierLocations        
 email      
 x-qldanxqldanx     

Since i am a newbie, searched a lot to solve this issue but couldn't succeed.由于我是新手,搜索了很多来解决这个问题,但未能成功。 Please throw some light on this.请对此有所了解。

Thanks in advance....提前致谢....

If you store your scenarios in structured objects you can use Powershell's built in Export-Csv command to generate your csv.如果您将场景存储在结构化对象中,您可以使用 Powershell 内置的Export-Csv命令来生成您的 csv。

So, instead of所以,而不是

    $Scenario += $scenarioname
    $Scenario += $questionarr[1]
    $Scenario += $equalarr[1]

store an array of powershell objects:存储一组 powershell 对象:

    $Scenario += [PSCustomObject]@{
        "Scenario" = $scenarioname; 
        "Parameter" = $questionarr[1]; 
        "Value" = $equalarr[1];}

Then, when creating the csv file, just use Export-Csv :然后,在创建 csv 文件时,只需使用Export-Csv

$Scenario | Export-Csv -NoTypeInformation -Path C:\Powershell\Documents\Output.csv

So the issue is that you make an empty array, then add strings to it one at a time, which just makes it an array of strings.所以问题是你创建一个空数组,然后一次向它添加一个字符串,这只会使它成为一个字符串数组。 Then when you output it to the file it just adds each string to the file on its own line.然后,当您将其输出到文件时,它只会将每个字符串添加到文件中单独的一行。 What you want to do is create an array of objects, then use the Export-Csv cmdlet to output it to a CSV file.您要做的是创建一个对象数组,然后使用Export-Csv cmdlet 将其输出到 CSV 文件。

Creating an array, and then adding things to it one at a time is not a good way to do it.创建一个数组,然后一次一个地向其中添加内容并不是一个好方法。 PowerShell has to recreate the array each time you add something the way you're doing it.每次您按照您的方式添加内容时,PowerShell 都必须重新创建数组。 Better would be to have a pipeline that outputs what you want (objects, rather than strings), and capture them all at once creating the array one time.最好有一个管道来输出你想要的东西(对象,而不是字符串),并一次性捕获它们,创建数组。 Or even better, just output them to the CSV file and not recollect them in general.或者甚至更好,只需将它们输出到 CSV 文件,而不是一般地重新收集它们。

$CSV = Import-Csv C:\Powershell\Documents\Source.csv

$CSV.http_uri -replace '^.*/(.*)$','$1'|ForEach-Object{
    $Record = $_ -split '[=\?]'
    [PSCustomObject]@{
        ScenarioName = $Record[0]
        Parameter = $Record[1]
        Value = $Record[2]
    }
    } | Export-Csv -Path C:\Powershell\Documents\Output.csv -Append

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM