[英]Comparing two CSV files in powershell and creating an output
我目前正在編寫一個腳本來導入 2 個 csv 文件,比較 fqdn 列並將結果輸出到一個文件。
問題是經過數小時的測試我現在看起來我的腳本正在運行直到獲取需要導入的每個文件的路徑但我似乎無法獲得import-csv
命令做我需要它做的事情。
如果您能提供任何指導,我將不勝感激。
到目前為止我的腳本和我得到的錯誤如下:
$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
錯誤信息:
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
不要使用Out-String
,除非你想要一個for-display ,多行字符串表示。
因此,您的文件路徑變量包含換行符(換行符), Import-Csv
抱怨這一點,因為文件名中的換行符在 NTFS(在 Windows 上)中是非法的。
簡單地省略Out-String
調用,因為Select -expa FullName -First 1
根據定義已經輸出了一個字符串(假設Get-ChildItem
輸出的對象上的.FullName
屬性是[string]
類型)。
重現問題:
PS> Import-Csv "foo`n" # illegal line break in file path
Import-Csv : Illegal characters in path.
為了證明Out-String
即使使用單行字符串作為輸入也能生成多行字符串:
PS> ('foo' | Out-String).EndsWith("`n")
True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.