【问题标题】:Powershell not handling string parameters properlyPowershell 未正确处理字符串参数
【发布时间】:2011-12-24 03:49:29
【问题描述】:

我正在编写一个简单的 powershell 脚本,但我不明白它的行为。这是代码。

Function print($first,$second){
Write-Host "$first"
}

$one="Dog"
$two="Cat"

print($one,$two)

这是输出。

Dog Cat

我不知道为什么它打印两个参数而不是我要求的那个。我发现发布了一个类似的问题,说解决方案的答案是写

print $one $two

但我不知道为什么。 另一个问题是How do I pass multiple string parameters to a PowerShell script?

谁能说明这个问题?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    就像你提到的,你必须这样称呼它:

    print $one $two
    

    在 Powershell 中,函数的参数用空格而不是逗号分隔,并且不括在括号中(但方法参数是)

    你调用的方式,print($one,$two),就像使用一个数组参数调用 print - ($one,$two)。因此,当您 write-host $first 时,您正在回显数组,因此您会同时看到它们。

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2020-11-12
      相关资源
      最近更新 更多