更改 VPN 连接的服务器地址

Change ServerAddress of VPN connection

我在 30 多台客户端计算机上配置了点到站点 VPN 连接,我只需要更改 VPN 网关的地址。当然,这意味着我必须重新配置所有客户端机器。我希望我可以创建某种可以 运行 自动更新内容的程序或脚本,而不是手动执行。 唯一我需要改变的是服务器地址,其他一切都应该保持不变。

我遇到了这些 PowerShell 命令 Get-VpnConnection and Set-VpnConnection。我可以使用此命令成功检索创建的 VPN 连接:

Get-VpnConnection MyConnectionName -AllUserConnection

所以我尝试使用 Set 变体:

Set-VpnConnection -Name MyConnectionName -ServerAddress NewServerAddress -AllUserConnection

但这只是 returns 并且什么都不做。没有错误,没有效果。用rasphone查看服务器地址,发现老地址还在使用

我也可以这样做:

$connection = Get-VpnConnection MyConnectionName -AllUserConnection
$connection.ServerName = NewServerAddress 

这也没有做任何事情,因为我很确定我只是在更新一个变量而不是 "committing" 它。

那么如何更新服务器地址呢?它甚至不必是 PowerShell,那只是我能找到的最佳选择。

在我这边,当我使用

时一切正常
Set-VpnConnection -Name MyConnectionName -ServerAddress = "x.x.x.x"

或许你可以试试去掉旧的再换一个新的

$Connection = Get-VpnConnection -Name MyConnectionName

Remove-VpnConnection -Name MyConnectionName -Force

$Connection.ServerAddress = "x.x.x.x"

$Connection | Add-VpnConnection

我最终使用 DotRas.

编写了一个 .NET 应用程序

如果有人感兴趣,这里是代码:

Public Const EntryName As String = "VPNEntryNAme"
Public Const NewAddress As String = "NewVPNAddress"

Private Sub B_Update_Click(sender As Object, e As EventArgs) Handles B_Update.Click
    Using pbk As New RasPhoneBook()
        pbk.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers))

        Dim VPN = pbk.Entries.Where(Function(Entry) Entry.Name = EntryName).FirstOrDefault

        If VPN Is Nothing Then
            MsgBox("VPN not found!", MsgBoxStyle.Critical)
            Exit Sub
        End If

        VPN.PhoneNumber = NewAddress

        VPN.Update()
    End Using

    Dim cn = RasConnection.GetActiveConnections.Where(Function(c) c.EntryName = EntryName).FirstOrDefault

    If cn IsNot Nothing Then
        cn.HangUp()
    End If

    MsgBox("The VPN has now been successfully updated")
End Sub