Powershell: IF, TRY and CATCH block in powershell script

Viewed 46

I am using try and catch block inside IF condition, but I am not getting the desired results.

If (Test-Path -Path "$env:DigiCertificate"){

try {
    signtool sign /f $env:DigiCertificate /v /t http://timestamp.sectigo.com /a /fd SHA256 /p $env:DigicertsPassword $MediaFolderPath\scripts\*.ps1 $MediaFolderPath\tools\*.exe
}catch
{
    Write-Host "required certificate not found to sign" -ForegroundColor Yellow
    Write-Host "$($_.Exception.Message)." -ForegroundColor Red
    exit 1
}
}

In the above code I am expecting the IF condition should check for the -Path and it should fail if it does not find the DigiCertificate. Apart from try and catch block even IF condition should work, but I am trying to do it but I am missing something.

0 Answers
Related