Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell中的参数计算不正确_Powershell_Powershell 4.0 - Fatal编程技术网

Powershell中的参数计算不正确

Powershell中的参数计算不正确,powershell,powershell-4.0,Powershell,Powershell 4.0,powershell中的参数不会像我期望的那样得到评估。 如果参数类型为object或string,则将数学表达式作为参数强制转换为字符串 Powershell代码:写入主机1+1 屏幕上打印的结果:1+1 预期结果:2 谁能告诉我为什么会这样? 这种行为背后的逻辑是什么 在语言规范中,它明确指出,如果参数类型为object,则值将按原样传递,而不进行强制转换 你能给我指一下描述这种行为的语言规范吗?因为我找不到它 注意:任何接受对象或字符串的函数都将遭受相同的行为。此链接 处理命令时,Wi

powershell中的参数不会像我期望的那样得到评估。 如果参数类型为object或string,则将数学表达式作为参数强制转换为字符串

  • Powershell代码:写入主机1+1
  • 屏幕上打印的结果:1+1
  • 预期结果:2
谁能告诉我为什么会这样? 这种行为背后的逻辑是什么

在语言规范中,它明确指出,如果参数类型为object,则值将按原样传递,而不进行强制转换

你能给我指一下描述这种行为的语言规范吗?因为我找不到它

注意:任何接受对象或字符串的函数都将遭受相同的行为。

此链接

处理命令时,Windows PowerShell解析器将运行 在表达式模式或参数模式下:

    - In expression mode, character string values must be contained in
      quotation marks. Numbers not enclosed in quotation marks are treated
      as numerical values (rather than as a series of characters). 

    - In argument mode, each value is treated as an expandable string 
      unless it begins with one of the following special characters: dollar
      sign ($), at sign (@), single quotation mark ('), double quotation
      mark ("), or an opening parenthesis (().


Example            Mode         Result
------------------ ----------   ----------------
2+2                Expression   4 (integer)
Write-Output 2+2   Argument     "2+2" (string)
Write-Output (2+2) Expression   4 (integer)
$a = 2+2           Expression   $a = 4 (integer)
Write-Output $a    Expression   4 (integer)
Write-Output $a/H  Argument     "4/H" (string)