Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test-path returns permission denied - how to do error handling

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?

Function Check-Path($Db)
{
    If ((Test-Path $Db) –eq $false) {
        Write-Output "The file $Db does not   exist"
        break
    }
}

It Returns:

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
like image 905
Dominik00000 Avatar asked Mar 17 '23 18:03

Dominik00000


1 Answers

Somewhat confusingly Test-Path actually generates an error in a number of cases. Set the standard ErrorAction parameter to SilentlyContinue to ignore it.

if ((Test-Path $Db -ErrorAction SilentlyContinue) -eq $false) {
like image 98
Mike Zboray Avatar answered Mar 20 '23 07:03

Mike Zboray