Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables - Fatal编程技术网

Powershell 计算变量

Powershell 计算变量,powershell,variables,Powershell,Variables,我想计算变量 例如: $1 = 1 $2 = 2 $Sym = + [int]$1 [int]$Sym [int]$2 到目前为止,我只在尝试转换符号时出错。是否有其他方法可以这样做,以便我仍然可以使用变量作为符号?我们将非常感谢您的任何帮助 您可以使用它。它基本上接受一个字符串,像执行命令一样执行它并输出结果 另外,请记住,在将+赋值给变量时,需要使用引号,否则PowerShell将尝试将其解释为运算符(也不要尝试将其转换为int…) 非常感谢您的帮助,我真的很感激。可能是 $1 = 1

我想计算变量

例如:

$1 = 1
$2 = 2
$Sym = +
[int]$1 [int]$Sym [int]$2 
到目前为止,我只在尝试转换符号时出错。是否有其他方法可以这样做,以便我仍然可以使用变量作为符号?我们将非常感谢您的任何帮助

您可以使用它。它基本上接受一个字符串,像执行命令一样执行它并输出结果

另外,请记住,在将
+
赋值给变量时,需要使用引号,否则PowerShell将尝试将其解释为运算符(也不要尝试将其转换为int…)


非常感谢您的帮助,我真的很感激。可能是
$1 = 1
$2 = 2
$Sym = '+'

Invoke-Expression "$1 $Sym $2"