[英]Comparing two CSV files in powershell and creating an output
I'm currently working on a script to import 2 csv files, compare the fqdn columns and output the results to a file.我目前正在编写一个脚本来导入 2 个 csv 文件,比较 fqdn 列并将结果输出到一个文件。
The issue is after many hours of testing I'm at the point that it looks like my script is working up until the point of getting the path for each file that needs to be imported but I can't seem to get the import-csv
command to do what I need it to.问题是经过数小时的测试我现在看起来我的脚本正在运行直到获取需要导入的每个文件的路径但我似乎无法获得import-csv
命令做我需要它做的事情。
I'd appreciate any guidance you can provide.如果您能提供任何指导,我将不胜感激。
My script so far and the error I'm getting are below:到目前为止我的脚本和我得到的错误如下:
$CMDB_Installed = Get-ChildItem -Path C:\Users\nha1\Desktop\Reports\CMDBInstall | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String
$SCOM_AgentList = Get-ChildItem -Path C:\Users\nha1\Desktop\Reports\SCOMUAT | Sort CreationTime -Descending | Select -expa FullName -First 1 | Out-String
$SL = Import-Csv $SCOM_AgentList
$CL = Import-Csv $CMDB_Installed
Compare-Object -ReferenceObject $CL -DifferenceObject $SL -Property fqdn |
Export-Csv -NoTypeInformation -Path = C:\Users\nha1\Desktop\Reports\AuditOutput\UATNeedsAgent+SCOM\UATHosts-NotinSCOM$(get-date -format yyyy-MM-dd).csv
Error Message:错误信息:
import-csv : Illegal characters in path.
At line:4 char:7
+ $SL = import-csv $SCOM_AgentList
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
import-csv : Illegal characters in path.
At line:5 char:7
+ $CL = import-csv $CMDB_Installed
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Import-Csv], ArgumentException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.ImportCsvCommand
$CMDB_Installed = ... | Select -expa FullName -First 1 | Out-String
Don't use Out-String
, unless you want a for-display , multi-line string representation.不要使用Out-String
,除非你想要一个for-display ,多行字符串表示。
Your file-path variables therefore contain newlines (line breaks), which Import-Csv
complains about, given that newlines in file names are illegal in NTFS (on Windows).因此,您的文件路径变量包含换行符(换行符), Import-Csv
抱怨这一点,因为文件名中的换行符在 NTFS(在 Windows 上)中是非法的。
Simply omit the Out-String
call, given that Select -expa FullName -First 1
by definition already outputs a string (given that the .FullName
property on the objects output by Get-ChildItem
is [string]
-typed).简单地省略Out-String
调用,因为Select -expa FullName -First 1
根据定义已经输出了一个字符串(假设Get-ChildItem
输出的对象上的.FullName
属性是[string]
类型)。
To recreate the problem:重现问题:
PS> Import-Csv "foo`n" # illegal line break in file path
Import-Csv : Illegal characters in path.
To demonstrate that Out-String
produces a multi-line string even with a single-line string as input:为了证明Out-String
即使使用单行字符串作为输入也能生成多行字符串:
PS> ('foo' | Out-String).EndsWith("`n")
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.