【问题标题】:Powershell - Set-Content : Cannot bind argument to parameter 'Path' because it is nullPowershell - Set-Content:无法将参数绑定到参数“路径”,因为它为空
【发布时间】:2021-06-08 07:19:34
【问题描述】:

我正在尝试替换文件中与条件不匹配的某些行,然后我需要将输出写入差异文件 [我实际上需要将输出重定向到同一个文件,但我不知道如何做它]并得到以下错误

代码-

   $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

任何解决问题的帮助/直接写入同一文件将不胜感激

【问题讨论】:

  • 谢谢,是的,引号是问题
  • @Theo 同意,我删除了我的评论。我将指出错误在哪里。似乎您忘记在 $fileName 变量中添加引号,因此为什么 Set-Content 会抛出该错误。

标签: powershell


【解决方案1】:

考虑使用Theo's improved solution,但只是指出当前的问题是什么:

您的 $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

cmd.exebash 等shell 不同,字符串 值分配给变量需要在PowerShell 中引用

由于您的路径是 literal 值,因此最好使用 single-quoting:

# OK - string must be *quoted*.
$fileName = 'E:\onlinecode\afarm_2106.txt'

请参阅this answer 的底部,了解 PowerShell 字符串文字的概述。

请注意,引用字符串的需要适用于所有形式的赋值,包括属性,可能作为哈希表或自定义的一部分- 对象字面量(例如,@{ foo = 'bar' }[pscustomobject] @{ foo = 'bar' }


背景信息

PowerShell 有两个基本的parsing modes参数模式(类似于 shell)和 表达式模式(类似于编程语言)。

第一个字符(在这种情况下位于= 的右轴)决定进入哪种模式,并且由于您的值既不是以'" 开头的, $, @, (, [, 也不是 {, argument 模式已进入,并且您的 - 不带引号的 - 文件路径被解释为 命令 em>。

即使文件本身不是可执行文件,PowerShell 也接受文件路径来执行,在这种情况下,它会要求 GUI shell 打开文件,就像您在文件中双击它一样例如,Windows 上的资源管理器。

因此,未引用的.txt 文件路径将在您系统的默认文本编辑器中打开,并且此操作将 (a) 是异步的并且 (b) 不会产生任何输出。

相反,如果您的文件路径由于空格和/或基于变量而需要引用,但您确实想将其作为命令执行,请使用&call operator;例如& 'E:\online code\afarm_2106.txt'

因此,$fileName 未分配任何值,这实际上使其成为$null[1]


[1] 严格来说,分配了“自动化空值”[System.Management.Automation.Internal.AutomationNull]::Value,但在大多数情况下,其行为类似于$null
请参阅GitHub issue #13465 讨论何时不支持,以及为什么应该支持通过-is [AutomationNull] 区分两者。

【讨论】:

    【解决方案2】:

    为什么不使用switch

    $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*'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-14
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多