powershell 传递变量抛出 try/catch

格斯

我正在尝试使用带有 if 语句的 try / catch 来验证目录是否存在(我希望这是一个正确的解决方案!?),但是我在设置和将 $RepoDir 变量的值传递到 catch 语句时遇到了问题

try {

    if (Notexists -Path $RepoDoc) {
       $RepoDir = "a"
       -ErrorAction Stop
    } 
    if (Notexists -Path $RepoExcel) {
       $RepoDir = "b"
       -ErrorAction Stop
    }
    if (Notexists -Path $RepoAttach) {
       $RepoDir = "c"
       -ErrorAction Stop
    }
}
catch {
   Write-Host "Directory $RepoDir not exist !" ---> Directory not exist ! 
   break
}

是范围问题吗?
我怎么能那样做?
谢谢

安斯加·威彻斯

做你想做的事情的正确方法是这样的:

$RepoDoc, $RepoExcel, $RepoAttach | Where-Object {
    -not (Test-Path -LiteralPath $_)
} | ForEach-Object {
    "Directory ${_} does not exist."
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章