Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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测试路径输出-eq“;假;不起作用_Powershell_Path - Fatal编程技术网

Powershell测试路径输出-eq“;假;不起作用

Powershell测试路径输出-eq“;假;不起作用,powershell,path,Powershell,Path,所以我正在检查路径是否可用。我使用测试路径来实现这一点 看起来是这样的: $c="C:\" $d="D:\" $e="E:\" if(Test-Path $c -eq "False"){ } elseif(Test-Path $d -eq "False"){ } elseif(Test-Path $e -eq "False"){ } else{ "The File doesn't exist" } Test-Path : A parameter cannot be found that ma

所以我正在检查路径是否可用。我使用测试路径来实现这一点

看起来是这样的:

$c="C:\"
$d="D:\"
$e="E:\"

if(Test-Path $c -eq "False"){
}
elseif(Test-Path $d -eq "False"){
}
elseif(Test-Path $e -eq "False"){
}
else{
"The File doesn't exist"
}
Test-Path : A parameter cannot be found that matches parameter name 'eq'.
At C:\Users\boriv\Desktop\ps\Unbenannt1.ps1:23 char:17
+ If(Test-Path $c -eq "False"){
+                 ~~~
+ CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand`
那么,如果错误如下所示,我做错了什么:

$c="C:\"
$d="D:\"
$e="E:\"

if(Test-Path $c -eq "False"){
}
elseif(Test-Path $d -eq "False"){
}
elseif(Test-Path $e -eq "False"){
}
else{
"The File doesn't exist"
}
Test-Path : A parameter cannot be found that matches parameter name 'eq'.
At C:\Users\boriv\Desktop\ps\Unbenannt1.ps1:23 char:17
+ If(Test-Path $c -eq "False"){
+                 ~~~
+ CategoryInfo          : InvalidArgument: (:) [Test-Path], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.TestPathCommand`

将您的
测试路径$c
括在括号中,以便首先对其进行评估:

$c="C:\"
$d="D:\"
$e="E:\"

if((Test-Path $c) -eq "False"){
    Write-Output '$c is false'
}
elseif((Test-Path $d) -eq "False"){
    Write-Output '$d is false'
}
elseif((Test-Path $e) -eq "False"){
    Write-Output '$e is false'
}
else{
    "The File doesn't exist"
} 

您不想比较
测试路径
cmdlet的结果,因此需要使用括号,否则
-eq
参数会传递到
测试路径
cmdlet,这就是您出现错误的原因

我会使用
-not
操作符,因为我发现它更可读。例如:

if(-not (Test-Path $c)) {
}

另一个不使用
if/elseif/else
的选项是将路径放入数组中,并在其中循环,直到找到有效路径

这样,对于任意数量的路径,代码都保持不变

$paths = "C:\","D:\","E:\"

foreach ($path in $paths) {
    if(Test-Path $path){
        $validpath = $path
        break
    }
}

if ($validpath){
    "File exists here: $validpath"
}
else {
    "The File doesn't exist in any path"
}

if((测试路径$c)-eq$False){}
我喜欢你的解决方案。这就是为什么我要接受你的答案。但这两个答案都是正确的。