更改PowerShell中的语言限制错误

时间:2019-02-21 10:19:04

标签: powershell

我想将此脚本中的语言更改为挪威语,并相信我已成功完成,但是,此更改已禁止输出错误消息。 Using-Culture en-us存在相同的问题。

关于这种情况为什么发生以及如何解决的任何想法?

我测试输出错误的方法是通过创建一个与现有名称相同的新VPN连接。 没有使用文化功能,将显示错误。有了它,错误就被抑制了,并且脚本仅显示自定义消息(无论是否存在错误,都会显示该消息)。

  

以下示例显示了预期的错误,但没有将语言更改为挪威语:

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"
}
  

请参阅下面的完整脚本。这不会显示错误,并且我无法确认错误中的语言是否为挪威语。

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"
    }
}

我为语法错误表示歉意,因为英语不是我的母语。  顶部的示例脚本未经过优化,因为它是测试脚本。

0 个答案:

没有答案