powershell - 为什么我不能使用带有 set-content 的 value 参数的脚本块?(延迟绑定)
问题描述
嗯,为什么这不起作用?通常,如果参数采用管道输入,我可以使用脚本块。
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
解决方案
推荐阅读
- ruby-on-rails - 通过字符串插值在 ruby on rails 应用程序中远程执行代码
- cassandra - 数据被插入到与 cassandra 不同的列中
- testing - 功能点分析中的工作量和成本估算
- r - 如何在将未知数量的变量传递给dplyr中的函数时使用pivot_wider
- c - 使转义序列在字符串中可读
- r - 用缺失值绘制正确的颜色和有序因子
- c - 为什么第一个代码与第二个代码不同?
- react-native - 如何在 expo + react-native 初始化阶段运行 clojurescript 函数?
- html - 带有文本的 HTML/CSS - EMAIL
- python - 如何在 Python 中同步 rx 管道?