0

I would like to change the language in this script to Norwegian and believe I have done it successfully, however, this change has suppressed error messages that are output. The same issue is present with Using-Culture en-us.

Any ideas on why this happens and how to fix it if possible?

My method of testing what the output error is is by creating a new VPN connection with the same name as an existing one. Without the Using-Culture function, errors are shown. With it, the errors are suppressed and the script only shows the custom message (Which is shown when an error is present regardless).

Example below shows errors as intended, but does not change the language to Norwegian:

try
{
    $Name = Read-Host -Prompt 'Enter the profile name for this VPN connection'
$password = Read-Host -assecurestring "Please enter your Pre-shared Key"

#Default Cisco Meraki parameters
$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 are required on the first-time sign on.
Support: contact | company",0,"Completed") | Out-Null
}
catch
{
    Write-Error $_.Exception.ToString()
    Read-Host -Prompt "The above error occurred, please try again. If the issue persists, please contact support.
Support: contact | company
Please press Enter to exit"
}

See full script below. This does not show errors, and I can not confirm if the language is Norwegian in the errors.

Function Using-Culture([Globalization.CultureInfo]$culture, [ScriptBlock]$script) {
    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
    trap {
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
    }
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
    $ExecutionContext.InvokeCommand.InvokeScript($script)
    [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture
}

Using-Culture nb-NO {
    try {
        $Name = Read-Host -Prompt 'Enter the profile name for this VPN connection'
        $password = Read-Host -AsSecureString "Please enter your Pre-shared Key"

        # Default Cisco Meraki parameters
        $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

        # Gives popup with information on next steps
        $wshell = New-Object -ComObject WScript.Shell
        $wshell.Popup("VPN-profile for $Name has been created.`nYou may now use this connection.`nUsername and password is required on first time sign on.`nSupport: contact | company", 0, "Completed") | Out-Null
    } catch {
        # Reports error and suppresses "Completed"
        Write-Error $_.Exception.ToString()
        Read-Host -Prompt "The above error occurred, please try again. If the issue persists, please contact support.`support: contact | company`nPlease press Enter to exit"
    }
}

I apologize for grammatical errors as English is not my first language. The top example-script is not optimized, as it is a testing script.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Thanks for the edit Ansgar, the code looks a lot cleaner now! As for the spelling mistakes, English is not my first language, thanks for fixing that too. – CodenameGeneral Feb 21 '19 at 11:28
  • I don't know anything about VPN Connection with Cisco Meraki.. But where does your script fail? How is your script, when it works? What did you already try to solve the problem? When I run your script it works withouth problem and creates a new VPN connection... Do you run your script as it is in your question? – Dan Stef Feb 21 '19 at 12:10
  • @DanStef The script works as it should, unless a error is being output. If you try to create a new VPN with the same name as a existing one, a error should be output. With the "Using culture" function, no errors are output. I will also add this information to the question itself. – CodenameGeneral Feb 21 '19 at 12:38
  • I still haven't found the real solution to it, but I think your problem is with the scriptblock.. If you try to run it like `Using-Culture -culture nb-NO -script $block` it does not work.. Maybe try something like here https://stackoverflow.com/questions/11844390/how-do-i-pass-a-scriptblock-as-one-of-the-parameters-in-start-job or https://learn-powershell.net/2014/10/11/using-a-scriptblock-parameter-with-a-powershell-function/ .. Hope it helps somewhat – Dan Stef Feb 21 '19 at 13:30
  • Not sure, but shouldn't you do a `[Threading.Thread]::CurrentThread.CurrentUICulture = $culture` aswell? – Theo Feb 21 '19 at 14:44
  • I tried this now, but it does not seem to be helping. Thanks for the reply anyways! – CodenameGeneral Feb 21 '19 at 15:00

0 Answers0