[英]Output CSV using Powershell
I got a problem with understanding of output CSV file from powershell script! 我在理解Powershell脚本输出CSV文件时遇到问题!
I have a Request to restapi server and I'm getting a variable which contain 10 or more lines, like: 我有一个对restapi服务器的请求,我得到一个包含10条或更多行的变量,例如:
>$ExpenseDescription
Taxi to airport
Taxi to seaport
Taxi to spaceport
Taxi to home
And the I'm creating table 而我正在创建表格
$tableExpenses=@"
Created On | Description | Expense Category
$ExpenseCreatedOnt | $ExpenseDescription |$ExpenseReport
$tableExpense|Out-File C:\Users\book.xls
$tableExpense|Out-File C:\Users\book.csv
And as output file I'm getting .xls and .csv ! 作为输出文件,我得到.xls和.csv !
So the problem is that I have 10 lines in variable $ExpenseDescription
and the OutFile contain all 10 lines in 1 cell in book.xls ! 所以问题是我在变量
$ExpenseDescription
有10行,而OutFile在book.xls的 1个单元格中包含所有10行!
How can I split them in code and make OutFile in format like this: 如何将它们拆分为代码,并使OutFile的格式如下:
Created On | Description | Expense Category
10.10.2018|Taxi to airport| Money
11.10.2018|Taxi to seaport| Visa
Because now I'm having this in output 因为现在我在输出中
Created On | Description | Expense Category
10.10.2018 11.10.2018|Taxi to airport Taxi to seaport| Money Visa|
OK, I'll add more code) 好的,我将添加更多代码)
$ReportURI = ("https://api.rest.com/data/query") $ReportQuery = @{"q"="SELECT Category,Description,CreatedOn from Expense"}
Try {$ResponseReport = Invoke-RestMethod -Method Post -Uri $ReportURI -Headers @{"Authorization" = $SessionId} -Body ( $ReportQuery | ConvertTo-Json) -ContentType "application/json" -ErrorAction Stop} Write-Host $ResponseReport} ConvertTo-Json $ResponseReport
$ReportURI = ("https://api.rest.com/data/query") $ReportQuery = @{"q"="SELECT Category,Description,CreatedOn from Expense"}
Try {$ResponseReport = Invoke-RestMethod -Method Post -Uri $ReportURI -Headers @{"Authorization" = $SessionId} -Body ( $ReportQuery | ConvertTo-Json) -ContentType "application/json" -ErrorAction Stop} Write-Host $ResponseReport} ConvertTo-Json $ResponseReport
$ExpenseCreatedOn = $ResponseReport.CreatedOn $ExpenseDescription = $ResponseReport.Description $ExpenseReport = $ResponseReport.Category.Name
$tableExpense=@" Created On Description Expense Category $ExpenseCreatedOn $ExpenseDescription $ExpenseReport
$tableExpense|Out-File C:\\Users\\book.xls $tableExpense|Out-File C:\\Users\\book.csv
You're not outputting a CSV. 您没有输出CSV。 With
Out-File
, you're exporting a text file. 使用
Out-File
,您可以导出文本文件。
Providing that your variables hold an array of strings, you could index into them to create an object, then use Export-Csv
to export that: 假设变量包含一个字符串数组,则可以在它们中建立索引以创建对象,然后使用
Export-Csv
导出该字符串:
foreach($i in 0..($ExpenseDescription.Count - 1)){
[array]$tableExpenses += [pscustomobject]@{
"Created On" = $ExpenseCreatedOnt[$i]
Description = $ExpenseDescription[$i]
"Expense Category" = $ExpenseReport[$i]
}
}
$tableExpenses | Export-Csv C:\Users\book.csv -NoType
$tableExpenses | Export-Csv C:\Users\book2.csv -NoType -Delimiter "|"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.