powershell -and 运算符与测试路径语法错误

标签 powershell syntax-error operators

在下面的代码中,我可以使用 -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/

相关文章:

MYSQL更新查询语法问题

c - Visual Studio 2010 在尝试调试时给我语法错误,但没有语法错误!

php - PHP中@符号的用途是什么?

R:+=(加等于)和++(加加)等价于c++/c#/java等?

powershell - 对使用 New-Item cmdlet 创建的项目进行 Psedit 会引发异常

visual-studio - 有人找到了适用于 Visual Studio 的 PowerShell 语法突出显示或 IntelliSense 插件吗?

powershell - 使用 Powershell 将文本文件文件夹打印为 PDF(保留原始基本名称)

powershell - 打开文件查询以查看打开的文件

java - 运算符 & 未为类型( boolean 值、int)定义

javascript - JS : What's the difference between =+ and +=