If-Else语句Powershell CSV和输出CSV
我是powershell脚本编写方面的新手,需要以下csv格式输出方面的帮助。我正在尝试选择一列,例如ACCOUNT.UPLOAD和make以及if/else语句,以便将其输出到另一个csv文件中。请有人帮忙 输出csv应如下所示:If-Else语句Powershell CSV和输出CSV,powershell,csv,scripting,Powershell,Csv,Scripting,我是powershell脚本编写方面的新手,需要以下csv格式输出方面的帮助。我正在尝试选择一列,例如ACCOUNT.UPLOAD和make以及if/else语句,以便将其输出到另一个csv文件中。请有人帮忙 输出csv应如下所示: 谢谢这会满足您的需要。添加注释以解释我所做的事情 $results = Import-Csv "C:\Users\test\Desktop\Test\customer.csv" | Select-Object "ACCOUNT.UPLOAD" # Created
谢谢这会满足您的需要。添加注释以解释我所做的事情
$results = Import-Csv "C:\Users\test\Desktop\Test\customer.csv" | Select-Object "ACCOUNT.UPLOAD"
# Created array to be able to add individual results from foreach
$TheCSV = @()
ForEach ($row in $results) {
# You can use a -ne in the right hand side, if done like this.
If (($row.'ACCOUNT.UPLOAD' -ne 'CP000101') -and $row.'ACCOUNT.UPLOAD' -ne 'CP000102') {
# Adds the ROW column to the entry and finds the index that it was in from $results.
# Did a +2 as it does not include the header and it starts at value 0. So to match it up with the actual excel row numbers, add 2.
$row | Add-Member -Name 'ROW' -type NoteProperty -Value "$([array]::IndexOf($results.'ACCOUNT.UPLOAD',$row.'ACCOUNT.UPLOAD')+2)"
$TheCSV += $row
}
}
$TheCSV | Export-Csv "C:\Users\test\Desktop\Test\test.csv" -NoTypeInformation
这样做:
param(
[Parameter(Position=0)]
$InputFile = 'D:\\Temp\\Data.csv',
[Parameter(Position=1)]
$OutputFile = 'D:\\Temp\\Output.csv'
)
Import-Csv $InputFile |
Select-Object "ACCOUNT.UPLOAD" |
%{
$lineno++
if ($_.'ACCOUNT.UPLOAD' -notin @('CP000101', 'CP000102')) {
$_ | Add-Member -Name 'ROW' -type NoteProperty -Value $lineno
$_ # Output to pipeline
}
} -Begin { $lineno = 1 } |
Export-Csv $OutputFile -NoTypeInformation
使用:
.\Script.ps1
.\Script.ps1 inputfilename.csv outputfilefname.csv
(a)
$results
只包含一个属性的对象,即帐户。上载用选择对象提取的属性。(b) 因此,循环中的$row
对象没有Type0
属性(它甚至不在CSV输入中)。(c) -ne
的RHS不支持数组-请使用操作符-in
。(d) $row。“ACCOUNT.UPLOAD”=“$($row.ACCOUNT.UPLOAD)”
是一个不可操作的选项,因为从CSV文件读取的所有内容都是以字符串开头的-您想做什么?请通过直接更新您的问题来澄清。哇,先生!你让我开心!谢谢你
.\Script.ps1
.\Script.ps1 inputfilename.csv outputfilefname.csv