Powershell - 设置内容:无法将参数绑定到参数“路径",因为它为空 [英] Powershell - Set-Content : Cannot bind argument to parameter 'Path' because it is null

查看:400
本文介绍了Powershell - 设置内容:无法将参数绑定到参数“路径",因为它为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to replace certain lines in a file that doesnt match the condition and then i need to write the output to a diff file [ I actually need the output to be redirected to the same file, but i dont know how to do it] and get the below error

代码 -

   $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

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

Any help to resolve the issue / Directly write to the same file would be appreciated

推荐答案

考虑使用 Theo 改进的解决方案,但只是为了指出当前的问题是什么:

Consider using Theo's improved solution, but just to point out what the immediate problem was:

您的 $fileName = 赋值的 RHS 缺少引用:

The RHS of your $fileName = assignment lacked quoting:

# 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 不同,为变量分配 string需要引用em> 在 PowerShell 中.

Unlike in shells such as cmd.exe or bash, assigning string values to variables requires quoting in PowerShell.

由于您的路径是一个文字值,最好使用引用:

Since your path is a literal value, it's best to use single-quoting:

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

请参阅此答案的底部部分,了解 PowerShell 的字符串文字概述.

See the bottom section of this answer for an overview of PowerShell's string literals.

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

Note that the need for quoting strings applies to all forms of assignments, including to properties, possibly as part of a hashtable or custom-object literal (e.g., @{ foo = 'bar' } or [pscustomobject] @{ foo = 'bar' }.

背景信息:

PowerShell 有两种基本的解析模式参数模式(类似shell)和表达式模式(类似编程语言).

PowerShell has two fundamental parsing modes, argument mode (like a shell) and expression mode (like a programming language).

第一个字符(在本例中位于 = 的右侧)决定进入哪种模式,并且由于您的值既不以 开头', ", $, @, (, [>,也没有 {argument 模式被输入,并且你的 - 未加引号的 - 文件路径被解释为命令.

It is the first character (on the RHS of =, in this case) that determines which mode is entered, and since your value started neither with ', ", $, @, (, [, nor {, argument mode was entered, and your - unquoted - file path was interpreted as a command.

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

PowerShell accepts a file path for execution even if the file isn't itself executable, in which case it asks the GUI shell to open the file, as if you had double-clicked it in File Explorer on Windows, for instance.

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

Therefore, an unquoted .txt file path will open in your system's default text editor, and this operation will (a) be asynchronous and (b) produce no output.

相反,如果您的文件路径由于空格和/或基于变量而需要引用,但您确实希望将其作为命令执行,请使用 &呼叫操作员;例如<代码>&'E:\在线代码\afarm_2106.txt'

因此,$fileName 没有被赋值,这实际上使它成为 $null.[1]

As a result, $fileName was assigned no value, which effectively made it $null.[1]

[1] 严格来说,自动化空值"值被分配,[System.Management.Automation.Internal.AutomationNull]::Value,然而,在大多数情况下,它的行为类似于 $null.
请参阅 GitHub 问题 #13465 以讨论何时不存在,以及为什么要区分两个 via -is [AutomationNull] 因此应该被支持.

[1] Strictly speaking, the "automation null" value is assigned, [System.Management.Automation.Internal.AutomationNull]::Value, which, however, behaves like $null in most contexts.
See GitHub issue #13465 for a discussion of when it does not, and why distinguishing the two via -is [AutomationNull] should therefore be supported.

这篇关于Powershell - 设置内容:无法将参数绑定到参数“路径",因为它为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆