【问题标题】:Connect to VPN by Powershell通过 Powershell 连接到 VPN
【发布时间】:2023-03-08 07:38:01
【问题描述】:

我希望我的 Windows 在加载后立即连接到 VPN 服务器。我如何使用 Powershell 来做到这一点?

【问题讨论】:

    标签: powershell vpn


    【解决方案1】:

    试试这适用于 Windows 10

        $vpnName = "YOUR_VPN_NAME";
        $vpn = Get-VpnConnection -Name $vpnName;
        if($vpn.ConnectionStatus -eq "Disconnected"){
        rasdial $vpnName;
        }
    

    【讨论】:

    • 这在技术上确实有效,但 rasdial 要求您输入以下格式的用户名和密码: rasdial $vpnName $Username $password...。即使保存了密码,它也要求您拥有这些明文参数。如果您想利用保存的密码,请使用 rasphone,此处列出的步骤:stackoverflow.com/questions/14180472/…
    • 请注意:如果您已经从 Azure 设置了 VPN(如 P2S),那么这是不可能的。您必须使用从 Azure 获得的信息在 Windows 上手动设置配置。以下指南可以提供帮助:dougrathbone.com/blog/2013/11/27/…
    • 所以无法使用 PowerShell 连接 VPN?对于 rasdialrasdial.exe 并且只是另一个可执行文件,根本不是 powershell cmdlet。我希望 win10 有一些 cmdlet……但没有?
    【解决方案2】:

    你可以试试这样的:

    我没有测试它是否有效。 我安装了PowerShell V3 Beta - 可能需要运行这些命令。

    Register-ScheduledJob -name ConnectVPN -ScriptBlock { & rasphone MyVpnConnection 
    $trigger = New-JobTrigger -AtLogOn
    Add-JobTrigger -Name ConnectVPN -Trigger $trigger
    Get-ScheduledJob -Name ConnectVPN | Get-JobTrigger
    

    【讨论】:

    • 我找到了更简单的方法)我只用一个短语“rasdial myVPN”创建了 .bat 文件,其中 myVPN - 是我之前创建的 VPN 连接的名称。然后我把这个 .bat 放到 StartUp 文件夹中。
    • @Ann:您应该将您的评论表述为答案!这将大大提高知名度!
    【解决方案3】:

    除了其他答案外,Windows 10 还通过名为 Always On 的配置原生支持此功能。有关始终开启的更多详细信息,请访问https://docs.microsoft.com/en-us/windows/access-protection/vpn/vpn-auto-trigger-profile

    您可以通过 MDM 甚至使用 WMI/Powershell 进行部署

    部署参考

    VPN 2 CSP:https://docs.microsoft.com/en-us/windows/client-management/mdm/vpnv2-csp

    CSP 到 WMI 桥:https://docs.microsoft.com/en-us/windows/client-management/mdm/using-powershell-scripting-with-the-wmi-bridge-provider

    【讨论】:

      【解决方案4】:

      Windows VPN 设置中的“自动连接”复选框对我来说效果很好。但是在配置拆分隧道以连接到锁定到 VPN IP 地址的 VM 后,需要断开/重新连接 VPN 连接才能生效。问题是 rasdial /disconnect 禁用了 AutoTrigger 设置。以下似乎可以重新启用自动触发。

      在此处设置特定的 VPN 配置文件名称或使用从 Get-VpnConnection 返回的第一个:

      $vpnProfileName = Get-VpnConnection | select -first 1 -ExpandProperty Name
      

      显示如何设置拆分隧道的可选示例:

      # Enable split-tunneling to a specific address
      # Name of VM restricted to VPN IP addresses
      $vmName = "myserver.eastus.cloudapp.azure.com"
      $ip = $(Resolve-DnsName -name $vmName  | where section -eq answer).IPAddress
      Add-VpnConnectionRoute -Name $vpnProfileName -DestinationPrefix "$ip/32"
      
      # Rasdial disconnect will turn off AutoTriggering
      rasdial $vpnProfileName /disconnect
      
      # Check VPN status
      Get-VpnConnection | select Name, IsAutoTriggerEnabled, ConnectionStatus
      

      重新启用自动触发并启动 VPN 连接:

      # Remove Disabled Profile
      $disabledProfiles = [string[]](Get-ItemPropertyValue HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggerDisabledProfilesList)
      $disabledProfiles = $disabledProfiles | where { $_ -ne $vpnProfileName }
      Set-ItemProperty HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggerDisabledProfilesList -Type MultiString -Value $disabledProfiles
      
      # Remove AutoTriggeringDisabled
      Remove-ItemProperty HKLM:SYSTEM\CurrentControlSet\Services\RasMan\Config -name AutoTriggeringDisabled 
      
      # Add trigger to a process that is certain to be running. Will trigger on process launch as well as if it is already running.
      # Adding trigger even it already exists seems to be necessary to get it to trigger after rasdial /disconnect
      Add-VpnConnectionTriggerApplication -Name $vpnProfileName –ApplicationID "C:\Windows\explorer.exe" -ErrorAction Ignore 
      
      # Check VPN status
      Get-VpnConnection | select Name, IsAutoTriggerEnabled, ConnectionStatus
      

      【讨论】:

        猜你喜欢
        • 2012-04-22
        • 2012-07-06
        • 2019-12-14
        • 2017-04-13
        • 2015-07-10
        • 2010-10-14
        • 2015-02-20
        • 2011-12-21
        • 2019-10-01
        相关资源
        最近更新 更多