Powershell-设置内容:无法将参数绑定到参数';路径';因为它是空的
我试图替换文件中不符合条件的某些行,然后我需要将输出写入一个diff文件[我实际上需要将输出重定向到同一个文件,但我不知道如何做],并得到以下错误 代码-Powershell-设置内容:无法将参数绑定到参数';路径';因为它是空的,powershell,Powershell,我试图替换文件中不符合条件的某些行,然后我需要将输出写入一个diff文件[我实际上需要将输出重定向到同一个文件,但我不知道如何做],并得到以下错误 代码- $fileName = E:\onlinecode\afarm_2106.txt $FileModified = @() [System.IO.StreamReader]$afarmfile = [System.IO.File]::Open("E:\onlinecode\Btnbar64\a_farm.txt"
$fileName = E:\onlinecode\afarm_2106.txt
$FileModified = @()
[System.IO.StreamReader]$afarmfile = [System.IO.File]::Open("E:\onlinecode\Btnbar64\a_farm.txt",
[System.IO.FileMode]::Open)
while (-not $afarmfile.EndOfStream){
$line = $afarmfile.ReadLine()
if ( $Line -like "*2104 Training Markets*" )
{
$FileModified += $Line
# Write-Output $FileModified
}
else
{
$FileModified += $Line -replace "2104","2106" -replace "21043","21062"
}
}
$afarmfile.Close()
Set-Content -Path $filename -value $FileModified
错误-
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:19 char:19
+ Set-Content -Path $filename -value $FileModified
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId :
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SetContentCommand
如果您能帮助解决问题/直接写入同一文件,我们将不胜感激为什么不使用
开关
$fileIn = 'E:\onlinecode\Btnbar64\a_farm.txt'
$fileOut = 'E:\onlinecode\afarm_2106.txt' # or use the same filename of $fileIn to overwrite
# loop through the file line-by-line and capture the results in variable $fileModified
$fileModified = switch -Regex -File $fileIn {
'2104 Training Markets' {
# don't change this line, just output unchanged
$_
}
default {
# output the line with certain strings replaced
$_ -replace '2104','2106' -replace '21043','21062'
}
}
$fileModified | Set-Content -Path $fileOut -Force
除了{..}中的switch-Regex-File$fileIn,您还可以
switch -Wildcard -File $fileIn {..}
在这种情况下,将条件替换为
“*2104培训市场*”
为什么不使用开关
$fileIn = 'E:\onlinecode\Btnbar64\a_farm.txt'
$fileOut = 'E:\onlinecode\afarm_2106.txt' # or use the same filename of $fileIn to overwrite
# loop through the file line-by-line and capture the results in variable $fileModified
$fileModified = switch -Regex -File $fileIn {
'2104 Training Markets' {
# don't change this line, just output unchanged
$_
}
default {
# output the line with certain strings replaced
$_ -replace '2104','2106' -replace '21043','21062'
}
}
$fileModified | Set-Content -Path $fileOut -Force
除了{..}
中的switch-Regex-File$fileIn,您还可以
switch -Wildcard -File $fileIn {..}
在这种情况下,将条件替换为“”*2104 Training Markets*”
考虑使用,但仅指出眼前的问题是什么:
您的$fileName=
作业的RHS缺少引用:
# WRONG: Lack of quoting of the RHS string value.
# This *opens the file in a text editor* and assigns $null to $fileName
$fileName = E:\onlinecode\afarm_2106.txt
与shell(如cmd.exe
或bash
)不同,为变量分配字符串值需要在PowerShell中引用
由于路径是文字值,因此最好使用单引号:
有关PowerShell字符串文字的概述,请参见的底部部分
请注意,引用字符串的需要适用于所有形式的赋值,包括属性,可能作为哈希表或自定义对象文本的一部分(例如,{foo='bar'}
或[pscustomobject]@{foo='bar'}
背景信息:
# WRONG: Lack of quoting of the RHS string value.
# This *opens the file in a text editor* and assigns $null to $fileName
$fileName = E:\onlinecode\afarm_2106.txt
PowerShell有两种基本模式:参数模式(类似于shell)和表达式模式(类似于编程语言)
它是决定进入哪种模式的第一个字符(在本例中,位于=
的右侧),因为您的值既不是以'
,“
,$
,@
,(
,[
,也不是开头的{
,已进入参数模式,并且您的-unquote-文件路径被解释为命令
PowerShell接受一个文件路径来执行,即使该文件本身不是可执行文件,在这种情况下,它会要求GUI外壳打开该文件,就像您在Windows上的文件资源管理器中双击它一样
因此,将在系统的默认文本编辑器中打开一个不带引号的.txt
文件路径,并且此操作将(a)是异步的,(b)不产生输出
相反,如果您的文件路径由于空格和/或基于变量而需要引用,但您确实希望将其作为命令执行,请使用&
,例如&'e:\online code\afarm_2106.txt'
结果,$fileName
没有赋值,这实际上使它$null
[1]
[1] 严格地说,“automation null”值是赋值的,
[System.Management.automation.Internal.AutomationNull]::value
,但在大多数上下文中其行为类似于$null
。请参阅,以了解何时不支持,以及为什么应支持通过
-is[AutomationNull]
区分这两个选项。考虑使用,但仅指出直接的问题是什么:
您的$fileName=
作业的RHS缺少引用:
# WRONG: Lack of quoting of the RHS string value.
# This *opens the file in a text editor* and assigns $null to $fileName
$fileName = E:\onlinecode\afarm_2106.txt
与shell(如cmd.exe
或bash
)不同,为变量分配字符串值需要在PowerShell中引用
由于路径是文字值,因此最好使用单引号:
有关PowerShell字符串文字的概述,请参见的底部部分
请注意,引用字符串的需要适用于所有形式的赋值,包括属性,可能作为哈希表或自定义对象文本的一部分(例如,{foo='bar'}
或[pscustomobject]@{foo='bar'}
背景信息:
# WRONG: Lack of quoting of the RHS string value.
# This *opens the file in a text editor* and assigns $null to $fileName
$fileName = E:\onlinecode\afarm_2106.txt
PowerShell有两种基本模式:参数模式(类似于shell)和表达式模式(类似于编程语言)
它是决定进入哪种模式的第一个字符(在本例中,位于=
的右侧),因为您的值既不是以'
,“
,$
,@
,(
,[
,也不是开头的{
,已进入参数模式,并且您的-unquote-文件路径被解释为命令
PowerShell接受一个文件路径来执行,即使该文件本身不是可执行文件,在这种情况下,它会要求GUI外壳打开该文件,就像您在Windows上的文件资源管理器中双击它一样
因此,将在系统的默认文本编辑器中打开一个不带引号的.txt
文件路径,并且此操作将(a)是异步的,(b)不产生输出
相反,如果您的文件路径由于空格和/或基于变量而需要引用,但您确实希望将其作为命令执行,请使用&
,例如&'e:\online code\afarm_2106.txt'
结果,$fileName
没有赋值,这实际上使它$null
[1]
[1] 严格地说,“automation null”值是赋值的,
[System.Management.automation.Internal.AutomationNull]::value
,但在大多数上下文中其行为类似于$null
。请参阅,了解关于何时不区分以及为什么通过
-is[AutomationNull]来区分这两者的讨论
因此应该得到支持。谢谢,是的,报价是最重要的problem@Theo同意,我删除了我的评论。我只想指出错误所在。似乎您忘记了在$fileName
变量中添加引号,因此设置内容
会抛出该错误。谢谢,是的,引号是problem@Theo同意