运行 `Get-ChildItem | 时如何在空值表达式上调用方法ForEach-对象 { |设置内容 $_ } `

问题描述 投票:0回答:1

我运行这个命令:

Get-ChildItem -recurse -file | ForEach-Object { (Get-Content $_).Replace('hi there','hello') | Set-Content $_ } 

并收到此错误:

InvalidOperation: You cannot call a method on a null-valued expression.
InvalidOperation: You cannot call a method on a null-valued expression.

你知道这是为什么吗?

powershell null expression
1个回答
0
投票

正如 Santiago Squarzon 所指出的 - 也许令人惊讶 - 使用

Get-Content
读取 文件(
0
字节)输出
$null
(无论您是否也使用
-Raw
)。[1]

$null 上调用

any
方法会导致您看到的错误。

一个简单的解决方案是过滤掉空文件,无论如何对它们执行任何替换都是没有意义的:

Get-ChildItem -Recurse -File |
  Where-Object Length -gt 0 |
  ForEach-Object {
    ($_ | Get-Content -Raw).Replace('hi there','hello') | 
      Set-Content -NoNewLine -LiteralPath $_.FullName
  }

请注意以下额外改进:

  • $_ | Get-Content
    相当于
    Get-Content -LiteralPath $_.FullName
    ,并确保每个输入文件都通过其完整路径传递并按字面意思(逐字)进行解释。

  • -Raw

     确保文件内容被读取 
    作为单个(通常是多行字符串),从而加快替换速度。

  • -NoNewLine

     防止 
    Set-Content
     将额外的换行符附加到(可能已修改的)文件内容。


[1] 从技术上讲,使用 -Rawnot

 会发出“可枚举 null”,这是特殊的 
[System.Management.Automation.Internal.AutomationNull]::Value
 单例,表示命令缺少输出。但是,在 
表达式 的上下文中,例如方法调用,该值的处理方式与 $null
 相同。

© www.soinside.com 2019 - 2024. All rights reserved.