这个问题类似于Passing empty arguments to executables using powershell但我想扩展问题和答案,并更好地理解“为什么”,因为它看起来像是一个 PowerShell 陷阱。
如果加载 PSCX 并运行 echoargs
(外部命令,即 exe 文件),您可以看到空字符串参数被跳过:
PS> echoargs word "two words" "" 123
Arg 0 is <word>
Arg 1 is <two words>
Arg 2 is <123>
但是如果您使用“CMD escape”(--%
),您可以使其“正确”显示:
PS> echoargs --% word "two words" "" 123
Arg 0 is <word>
Arg 1 is <two words>
Arg 2 is <>
Arg 3 is <123>
类似地,如果编写一个 PowerShell 函数,空字符串就会得到正确处理:
PS> Show-Args word "two words" "" 123
Arg 0 is <word>
Arg 1 is <two words>
Arg 2 is <>
Arg 3 is <123>
由于以下原因,这种差异对我来说似乎很重要。上面显示的示例在命令行上使用文字空字符串,因此至少您可以了解问题的提示。 但是如果使用包含空字符串的变量,结果是完全相同的。这意味着必须:
- 严格监管所有提供给外部命令的变量,或者
- 使用 CMD 转义 (--%) 并放弃在该行的其余部分使用任何 PowerShell 结构
- 使用反引号/双引号引用外部命令的每个参数,例如
`"this`"
或`"$this`"
或`"`"
...否则不好的事情会发生!
(@KeithHill 指出了上面的第三种解决方法,因此我将其添加到那里是为了完整性。它适用于文字或变量,因此虽然丑陋,但可能是三种解决方法中的最佳选择。)
因此,PowerShell 处理函数参数的方式与处理外部命令参数的方式非常不同。这是 PowerShell 行为的不一致吗?如果没有,为什么不呢?
<小时/>附录
下面是上面使用的 PowerShell 函数体,供引用:
function Show-Args()
{
for ($i = 0; $i -lt $args.length; $i++)
{
Write-Host ("Arg {0} is <{1}>" -f $i, $args[$i])
}
}
这是 C# 中的 echoargs 等效项:
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
{
System.Console.WriteLine("Arg {0} is <{1}>", i, args[i]);
}
}
}
最佳答案
这种行为可能被认为是“设计使然”(根据实现和测试很难确定),但我认为这个问题没有得到太多考虑,并且您提出了很好的观点。
我个人认为你是对的,PowerShell 不一致。值得考虑进行一致的更改,尽管这存在破坏现有脚本的风险。
关于powershell - 为什么 PowerShell 在命令行上以不同方式处理空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23640434/