我正在尝试通过if语句使用try / catch来验证目录是否存在(我希望这是一个正确的解决方案!?),但是我在设置和传递问题时遇到了问题catch语句中$ RepoDir变量的值
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
}
是范围问题吗?
我该怎么办?
谢谢
答案 0 :(得分:2)
做你想要的事情的正确方法是这样的:
$RepoDoc, $RepoExcel, $RepoAttach | Where-Object {
-not (Test-Path -LiteralPath $_)
} | ForEach-Object {
"Directory ${_} does not exist."
}