Question:
i try to do error handling within my powershell script. but i always get an fatal. i tried a few things, e. g. try{ } catch{ } – but i did it not get to work.
any ideas or Solutions?
1 2 3 4 5 6 7 8 |
Function Check-Path($Db) { If ((Test-Path $Db) –eq $false) { Write-Output "The file $Db does not exist" break } } |
It Returns:
1 2 3 4 5 6 7 |
Test-Path : Zugriff verweigert In K:\access\access.ps1:15 Zeichen:6 + If ((Test-Path $Db) -eq $false) { + ~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (K:\ss.mdb:String) [Test-Path], UnauthorizedAccessException + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand |
Answer:
Somewhat confusingly Test-Path
actually generates an error in a number of cases. Set the standard ErrorAction parameter to SilentlyContinue to ignore it.
1 2 |
if ((Test-Path $Db -ErrorAction SilentlyContinue) -eq $false) { |