gpt4 book ai didi

Powershell 测试路径输出 -eq "False"不工作

转载 作者:行者123 更新时间:2023-12-02 06:28:02 24 4
gpt4 key购买 nike

所以我正在尝试检查路径是否可用。我使用测试路径执行此操作

看起来像这样:

$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`

最佳答案

您不想比较 Test-Path cmdlet 的结果,因此您需要使用括号,否则 -eq 参数会传递给 Test -Path cmdlet,这就是您收到错误的原因。

我会使用 -not 运算符,因为我发现它更具可读性。示例:

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

关于Powershell 测试路径输出 -eq "False"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com