Powershell 如何比较两个不同Csv文件的列?
以下是我运行脚本时收到的错误消息:不可能在空表达式中调用方法。 在字符处:ChangeColumnValue.ps1:19:17 $JobstreamColumn=$Script.Split(“.”); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:)[],运行时异常 FullyQualifiedErrorId:InvokeMethodOnFullPowershell 如何比较两个不同Csv文件的列?,powershell,Powershell,以下是我运行脚本时收到的错误消息:不可能在空表达式中调用方法。 在字符处:ChangeColumnValue.ps1:19:17 $JobstreamColumn=$Script.Split(“.”); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CategoryInfo:InvalidOperation:(:)[],运行时异常 FullyQualifiedErrorId:InvokeMethodOnFull I am new to powershel
I am new to powershell, I want to read a csv file with four columns (A,B,C,D), I want to know if column C contains the term "technical" the value of column A remains unchanged, but if column C does not contain the term "technical" I recover its value to put it in column A, and finally I generate a new csv file. Please, can you help me, this is what I did but it doesn't work for the moment.
This csv file begining:
[![job.csv][1]][1]
$ElementCsv=Import-csv $env:USERPROFILE\Desktop\Archi\job.csv
$NewModifiedElement= ForEach($Entry in $ElementCsv){
$JobstreamColumn=$($Entry_."Jobstream");
$JobstreamDescription=$($Entry_."Jobstream Description");
$OpNum= $($Entry_."Op num");
$Job= $($Entry_."Job");
$Script=$($Entry_."Script or expected file(s)");
$Server=$($Entry_."Server");
$User=$($Entry_."user");
$Location=$($Entry_."location");
$JobDescription=$($Entry_."Job Description")
if($Script -ne "technical"){
$JobstreamColumn=$Script -split"\.";
}else {
$JobstreamColumn=$Script;
}
$Entry
}
$NewModifiedElement | Export-Csv "$env:USERPROFILE\Desktop\Archi\job2.csv" -NoTypeInformation
为什么拆分时会出现此异常?如果您的CSV文件包含列,您可以执行以下操作: 示例CSV:
Thank you in advance for your help
[1]: https://i.stack.imgur.com/XP7GF.png
代码:
$names=导入Csv-路径“C:\names.Csv”
Person1Age,Person1,Person2Age,Person2
21,Jame,18,Kevin
24,Dan,14,Chris
33,April,31,May
15,Allen,15,Craig
#循环浏览CSV文件中的所有记录
$NewModifiedElement=ForEach($ElementCsv中的条目){
}
在新的csv文件中导出element.csv
$NewModifiedElement | Export Csv“$env:USERPROFILE\Desktop\Archi\Extract\u AGRe\u TWS\u ALL_20200925-02.Csv”-NoTypeInformation-Encoding UTF8-Append输入Csv的标题(列名)是什么?请向我们显示Csv文件本身的前3行或4行(已消毒)。只需在记事本中打开它并复制/粘贴到您的问题中。这样我们就可以看到它是否使用了标题以及使用了什么分隔符。param
$EmployeeID
用于什么?我看不到在您的功能中有任何地方调用它。名字------亚当·巴拉克·米兰达·米歇尔姓氏------伯特伦·奥巴马伯特伦·奥巴马部门------it技术执行办公室经理------米兰达·伯特伦·米歇尔·奥巴马多没有在评论中写可以解释输入csv的部分,因为它不可读。取而代之的是,把你的问题以格式化文本的形式粘贴到那里。好的,去测试一下,非常感谢much@Bankass-没问题,如果有效,请将其标记为“有用”您好,我在检索列中每一行的值时返回一个空值。这是适合我需要的脚本。我将该脚本放在下面的注释中。@Bankass从循环中的变量名“$Entry_”中删除下划线('-')。您正在迭代变量$Entry,但使用的是不同的变量名$Entry。它们必须都匹配。我的脚本在我要拆分“\”时工作正常。字符串包含点,e。g PAKIO-996Q-005S.KSH或PAKIO-996Q-005S.bat,我在csv文件中获得System.String[]
foreach($name in $names)
{
$p1 = $($name.Person1);
$age1 = $($name.Person1Age);
$p2 = $($name.Person2);
$age2 = $($name.Person2Age);
if($age2 -ne '18') {
#Since age2 on the first row equals 18, column1 will retain it's value
#of 21, however, since the other rows don't have a value of 18 in age2
#column, their values in column1 (age1) will be change to reflect age2.
$age1 = $age2;
}
#Results: 21, 14, 31, 15
#Do operations to store the values in another csv file.
}
$ElementCsv=Import-csv $env:USERPROFILE\Desktop\Archi\Extract_AGRe_TWS_ALL_20200925-01.csv -Delimiter ';'
if ($Entry."Script or expected file(s)" -ilike 'technical') {
$Entry.Jobstream=$Entry.Jobstream
}else {
# Get the name of jobSet without extension .ksh ou .bat
$Entry.Jobstream=$Entry."Script or expected file(s)"
# Write-Host $Entry.Jobstream
# Write-Host $Entry.Jobstream.length
$pos_last_point = $Entry.Jobstream.LastIndexOf(".")
#Write-Host $pos_last_point
$Entry.Jobstream = $Entry.Jobstream.Substring(0,$pos_last_point)
}
$Entry