我想知道Try / Catch
以下是否有效,或者我应该使用 anIF ($problem)
而不是Try/Catch
Try {
New-Item -Path C:\reports -ItemType Directory -ErrorAction SilentlyContinue -ErrorVariable Problem -Force
}
Catch {
Write-Warning "Problem with C:\Report directory. Unable to create report $Type of $SourceEmail $Flag $DestinationEmail"
}
我正在检查目录是否存在,如果不存在则尝试创建目录。我不确定是否因为我使用-ErrorAction SilentlyContinue -ErrorVariable Problem
的try/catch
没有按预期工作?
替代品
New-Item -Path C:\reports -ItemType Directory -ErrorAction SilentlyContinue -ErrorVariable Problem -Force
If ($Problem) {
Write-Warning "Problem trying to create C:\Reports."
}