我创建了一个非常简单的powershell脚本来计算表达式。那就是:
$expression = read-host -prompt "Enter an expression"
$result = $expression
write-host "The result is $result"
read-host
但是,当我像这样使用它时:
输入表达式:2 + 3
结果是2 + 3
如何使第二行计算结果?
答案 0 :(得分:2)
也许这对您有帮助:
$expression = read-host -prompt "Enter an expression"
# check for dangerous code
if( ($expression -replace '^[0-9\+\-\*\/ ]+$').Length -eq 0 ) {
try {
$result = Invoke-Expression $expression
write-host "The result is $result"
}
catch {
write-host "Syntax error"
}
}
else {
write-host "Invalid expression"
}