提前谢谢。我尝试导入多个.csv文件,向其中添加一列,然后根据另一列的值写入该列
我想要做的就是遵循这个逻辑:
我的代码在这里:
Function getTag ([string]$vuln){
if($vuln -like "adobe-apsb-")
{return "Adobe Flash"}
if($vuln -like "jre-")
{return "Java"}
else
{return "No vulnerability code available"}
}
$in = "D:\Splunk\Inbound"
$out = 'D:\Splunk\Outbound'
Get-ChildItem $in -Filter *.csv |
ForEach-Object{
Import-Csv $_ | Select-Object *,@{Name='Tag';Expression={getTag $_.'Vulnerability ID'}} | Export-Csv $_.Name
}
现在,$ _以字符串形式出现,而不是CSV,我相信这是我的问题。有没有人有一个很好的方法从嵌套循环中访问该csv文件?
答案 0 :(得分:1)
对我而言,这是更好的可读性(并且有效):
Function getTag ([string]$vuln){
if ($vuln -like "adobe-apsb-*")
{return "Adobe Flash"}
if ($vuln -like "jre-*")
{return "Java"}
else
{return "No vulnerability code available"}
}
$in = "D:\Splunk\Inbound"
$out = "D:\Splunk\Outbound\"
Get-ChildItem $in -Filter *.csv |
ForEach-Object{
Import-Csv $_.FullName |
Select-Object *,@{
Name='Tag';
Expression={getTag $_.'Vulnerability ID'}
} | Export-Csv -path $($out+$_.Name) -notype
}
> tree . /f │ ├───Inbound │ test.csv │ └───Outbound test.csv > gc .\Inbound\test.csv "A","Vulnerability ID","C" 0,"adobe-apsb-blah",2 5,"jre-trash",9 > gc .\Outbound\test.csv "A","Vulnerability ID","C","Tag" "0","adobe-apsb-blah","2","Adobe Flash" "5","jre-trash","9","Java"