【发布时间】:2018-05-11 13:48:03
【问题描述】:
我正在学习powershell并尝试编写一个脚本,该脚本可以通过字符串在目录中查找文件,然后对找到的文件进行查找和替换。我想将文件列表存储为变量,然后遍历文件并替换特定的字符串。这是我的脚本和错误,如果您有任何想法,将不胜感激。谢谢!
$GetFiles = Select-String -path "C:\temp\*.xml" -pattern "<cmn:BusinessName>ABC INC</cmn:BusinessName>"|Select-Object filename
foreach ($file in $GetFiles)
{
(Get-Content $Files.PSPath) |Foreach-Object {
$_ -replace "<cmn:FileNumber>0001234</cmn:FileReceiverNumber>", "<cmn:FileReceiverNumber>12345678</cmn:FileReceiverNumber>" `
-replace "<cmn:DropIndicator>DROP</cmn:Indicator>", "<cmn:DropIndicator>DONTDROP</cmn:DropIndicator>"
} |Set-Content $Files.PSPath
}
错误
Get-Content:无法将参数绑定到参数“路径”,因为它是 空值。在 C:\scripts\script.ps1:5 char:18 +(获取内容 $Files.PSPath)|Foreach 对象 { + ~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
【问题讨论】:
标签: powershell replace