4

I have created a PowerShell script that will add a VPN connection for Cisco Meraki. The script itself functions as intended, but if a error occures, the "Completed" popup appears, with the error message shown in the PS windows.

Is it possible to supress the error and show a custom error popup based on the error that appears, while stopping the "Completed" popup from appearing?

I am aware of the $ErrorActionPreference= 'silentlycontinue', but unsure of how to implement this with a custom error.

Script to add VPN connections for Cisco Meraki.

$Name = Read-Host -Prompt 'Enter the profile name for this VPN connection'
$password = Read-Host -assecurestring "Please enter your Pre-shared Key"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
Add-VpnConnection -Name "$Name" -ServerAddress 193.214.153.2 -AuthenticationMethod MSChapv2 -L2tpPsk "$password" -TunnelType L2tp -RememberCredential -Force
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("VPN-profile for $Name has been created.
You may now use this connection.
Username and password is required on first time sign on.
Support: _witheld_ | _witheld_",0,"Completed")

2 Answers 2

4

Since your script continues to run after the error occurs, you are dealing with a non-terminating error, so you can use the
-ErrorVariable common parameter to capture a given cmdlet invocation's error(s)
.

Using a simplified example, which you can apply analogously to your Add-VpnConnection call:

 # Call Get-Item with a nonexistent path, which causes a *non-terminating* error. 
 # * Capture the error with -ErrorVariable in variable $err.
 # * Suppress the error console output with -ErrorAction SilentlyContinue
 Get-Item /NoSuch/Path -ErrorVariable err -ErrorAction SilentlyContinue

 $null = (New-Object -ComObject Wscript.Shell).Popup(
    $(if ($err) { "Error: $err" } else { 'Success.' })
 )

If you were dealing with a terminating error, you'd have to use try / catch:

 # Call Get-Item with an unsupported parameter, which causes a 
 # *(statement-)terminating* error. 
 try {
   Get-Item -NoSuchParam
 } catch {
   # Save the error, which is a [System.Management.Automation.ErrorRecord]
   # instance. To save just a the *message* (a string), use 
   # err = "$_"
   $err = $_ 
 }

 $null = (New-Object -ComObject Wscript.Shell).Popup(
    $(if ($err) { "Error: $err" } else { 'Success.' })
 )

Note:

  • Neither -ErrorAction nor -ErrorVariable work with terminating errors.
  • Conversely, try / catch cannot be used to handle non-terminating errors, which is presumably why Ranadip Dutta's answer didn't work for you.

For a comprehensive discussion of PowerShell error handling, see this GitHub issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I have realized it now that its a non terminating error.
1

You have to have the error handling for the script. I have given it as a whole in the below script but you can configure it based on your need:

try
{
$Name = Read-Host -Prompt 'Enter the profile name for this VPN connection'
$password = Read-Host -assecurestring "Please enter your Pre-shared Key"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
Add-VpnConnection -Name "$Name" -ServerAddress 193.214.153.2 -AuthenticationMethod MSChapv2 -L2tpPsk "$password" -TunnelType L2tp -RememberCredential -Force
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("VPN-profile for $Name has been created.You may now use this connection.Username and password is required on first time sign on.Support: _witheld_ | _witheld_",0,"Completed")
}
catch
{
"Your custom message"
$_.Exception.Message
}

For further refence, read TRY/CATCH/FINALLY in Powershell

Hope it helps.

3 Comments

Thanks, but this did not work it seemes. When i use the catch function, I get this output: catch : The term 'catch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t he spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\Username\Documents\01_PowerShell_Script\Ny_VPN_Tilkobling.ps1:17 char:1 + catch + ~~~~~ + CategoryInfo : ObjectNotFound: (catch:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
@user11089156: It would be tough to tell where it is getting the issue. if catch is not getting recognized then there is an issue with the indentation. make sure the catch is beginning next to the closure curly brace of try.
@RanadipDutta: In the OP's code Add-VpnConnection issues a non-terminating error, which your try / catch handler cannot trap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.