Powershell 为什么可以';t我使用带有设置内容的脚本块';s值参数?(延迟绑定)

Powershell 为什么可以';t我使用带有设置内容的脚本块';s值参数?(延迟绑定),powershell,replace,scriptblock,Powershell,Replace,Scriptblock,嗯,为什么这个不行?通常,如果参数采用管道输入,我可以使用脚本块 PS C:\users\js> (Get-Content -Path file) | Set-Content -Path file -Value { $_ -Replace 1,2 } Set-Content : The input object cannot be bound to any parameters for the command either because the command does not tak

嗯,为什么这个不行?通常,如果参数采用管道输入,我可以使用脚本块

PS C:\users\js> (Get-Content -Path file) | Set-Content -Path file -Value { $_ -Replace 1,2 }

Set-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand

Set-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand

Set-Content : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the
input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:28
+ ... ntent -path file) | set-content -path file -value { $_ -replace 1,2 }
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (111:PSObject) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.SetContentCommand
这种方法有效,但不是我想要的结果。它从字面上理解脚本块

PS C:\users\js> (Get-Content -Path file) | Set-Content -Value { $_ -Replace 1,2 }
PS C:\users\js> Get-Content -Path file
 $_ -Replace 1,2
我希望它能像这样工作:

PS C:\users\js> Write-Output -InputObject 111,111,111 | Write-Output -InputObject { $_ -Replace 1,2 }

222
222
222

迭代行或将
-raw
参数与Get Content一起使用,并直接在括号=>
(Get Content-path file-raw)后面应用
-replace
)-replace 1,2 | set Content-path file
参数和流水线输入似乎与
set Content
一起相互排除,
写入输出
cmdlet的编程方式明显不同。您可以将它插入管道中,
(Get Content-Path file)| Write Output-InputObject{$\uU1-Replace 1,2}| Set Content-Path file
迭代行或使用带有Get Content的
-raw
参数,并直接在括号=>
(Get Content-Path file-raw)后面应用
-Replace
-将1,2 | set content-path file
替换为
-Value
参数和流水线输入似乎与
set content
相互排斥,
Write Output
cmdlet的编程方式明显不同。您可以将其插入管道中,
(获取内容-路径文件)|写入输出-输入对象{$\uU1-Replace 1,2}|设置内容-路径文件