在下面的代码中,我可以使用 -and 运算符来创建复合 if 语句,但是当使用 Test-Path 和 -and 时,会出现语法错误。
将 -and 与 Test-Path 这样的命令一起使用的正确方法是什么?
$a = 1
$b = 1
$c = 1
if ($a -eq $b -and $a -eq $c ) {
write-host "Variables are equal."
}
$path1 = "C:\Windows"
$path2 = "C:\Users"
if (Test-Path $path1 -and Test-Path $path2) {
write-host "paths exist."
}
最佳答案
如果您将 Test-Path 的用法放在括号中,那么它就可以工作,即
$path1 = "C:\Windows"
$path2 = "C:\Users"
if ((Test-Path $path1) -and (Test-Path $path2)) {
write-host "paths exist."
}
关于powershell -and 运算符与测试路径语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44058076/