使用 powershell 创建 l2tp 连接

最近给华为的 AR2220-S 路由器配置 L2TP(参考https://forum.huawei.com/enterprise/zh/thread/580913000242626560)

设备上配置完成后,在Windows上面用如下powershell命令创建vpn连接,并且不会将vpn连接设置为默认路由

Add-VpnConnection -Name "VPNl2tp" -ServerAddress "A.B.C.D" -TunnelType L2tp -RememberCredential -AuthenticationMethod CHAP  -SplitTunneling -Force

拨完vpn之后,如果发现缺路由, 可以通过如下命令添加路由

route add X.Y.Z.0 mask 255.255.255.0 10.1.1.1

 


 

如果不用powershell命令创建vpn连接,就需要进行很多点击操作,比如 https://forum.huawei.com/enterprise/zh/thread/580912995154935808

Add-VpnConnection 帮助

PS C:\Windows\system32> Get-Help Add-VpnConnection

名称
    Add-VpnConnection

摘要
    Adds a VPN connection to the Connection Manager phone book.


语法
    Add-VpnConnection [[-RememberCredential]] [[-UseWinlogonCredential]] [[-EapConfigXmlStream] <XmlDocument>] [-Name]
    <String> [-ServerAddress] <String> [[-TunnelType] {Pptp | L2tp | Sstp | Ikev2 | Automatic}] [[-EncryptionLevel] {No
    Encryption | Optional | Required | Maximum | Custom}] [[-AuthenticationMethod] {Pap | Chap | MSChapv2 | Eap | Machi
    neCertificate}] [[-SplitTunneling]] [[-AllUserConnection]] [[-L2tpPsk] <String>] [-CimSession <CimSession[]>] [-Dns
    Suffix <String>] [-Force] [-IdleDisconnectSeconds <UInt32>] [-MachineCertificateEKUFilter <String[]>] [-MachineCert
    ificateIssuerFilter <X509Certificate2>] [-PassThru] [-ServerList <CimInstance[]>] [-ThrottleLimit <Int32>] [-Confir
    m] [-WhatIf] [<CommonParameters>]

    Add-VpnConnection [[-RememberCredential]] [-PlugInApplicationID] <String> [-Name] <String> [-ServerAddress] <String
    > [[-SplitTunneling]] [-CimSession <CimSession[]>] [-DnsSuffix <String>] [-Force] [-IdleDisconnectSeconds <UInt32>]
     [-PassThru] [-ServerList <CimInstance[]>] [-ThrottleLimit <Int32>] -CustomConfiguration <XmlDocument> [-Confirm] [
    -WhatIf] [<CommonParameters>]


说明
    The Add-VpnConnection cmdlet adds a VPN connection with the specified parameters to the Connection Manager phone bo
    ok. If errors occur when you create the VPN profile, the cmdlet returns the error information.


相关链接
    Get-VpnConnection
    Set-VpnConnection
    Remove-VpnConnection
    New-EapConfiguration

备注
    若要查看示例,请键入: "get-help Add-VpnConnection -examples".
    有关详细信息,请键入: "get-help Add-VpnConnection -detailed".
    若要获取技术信息,请键入: "get-help Add-VpnConnection -full".
    有关在线帮助,请键入: "get-help Add-VpnConnection -online"

Add-VpnConnection 示例

PS C:\Windows\system32> get-help Add-VpnConnection -examples

名称
    Add-VpnConnection

摘要
    Adds a VPN connection to the Connection Manager phone book.


    Example 1: Add a VPN connection

    PS C:\> Add-VpnConnection -Name "Test1" -ServerAddress "10.1.1.1" -PassThru
    Name                  : Test1
    ServerAddress         : 10.1.1.1
    AllUserConnection     : False
    Guid                  : {4B308E9B-C225-42CB-8158-307193429591}
    TunnelType            : Automatic
    AuthenticationMethod  : {MsChapv2}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Certificate
    UseWinlogonCredential : False
    EapConfigXmlStream    :
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : False
    SplitTunneling        : False

    This command adds a VPN connection named Test1 to the server with an IP address 10.1.1.1. By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 2: Add a VPN connection with an alternate authentication method

    PS C:\>Add-VpnConnection -Name "Test3" -ServerAddress "10.1.1.1" -TunnelType "Pptp" -EncryptionLevel "Required" -AuthenticationMethod MSChapv2 -UseWinlogonCredential -SplitTunneling -AllUserConnection -RememberCredential -PassThru
    Name                  : Test3
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {76746D4E-D72A-467D-A11F-3D4D9075F50D}
    TunnelType            : Pptp
    AuthenticationMethod  : {MsChapv2}
    EncryptionLevel       : Required
    L2tpIPsecAuth         :
    UseWinlogonCredential : True
    EapConfigXmlStream    :
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This command adds a VPN connection named Test3 to the server with an IP address 10.1.1.1. This connection uses the MSCHAPv2 authentication method, as specified by the AuthenticationMethod parameter. Additional parameters specify that the connection:

    -- Uses the Windows logon credentials (the UseWinlogonCredential parameter)
    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)

    By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 3: Add a VPN connection that uses EAP authentication

    PS C:\>Add-VpnConnection -Name "Test4" -ServerAddress "10.1.1.1" -TunnelType "L2tp" -EncryptionLevel "Required" -AuthenticationMethod Eap -SplitTunneling -AllUserConnection -L2tpPsk "password" -Force -RememberCredential -PassThru
    Name                  : Test4
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {1D423FF3-E3D4-404A-B052-DB9130656D29}
    TunnelType            : L2tp
    AuthenticationMethod  : {Eap}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Psk
    UseWinlogonCredential : False
    EapConfigXmlStream    : #document
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This command adds a VPN connection named Test4 to the server with an IP address of 10.1.1.1. This connection uses the default EAP authentication method, as specified by the AuthenticationMethod parameter. The pre-shared key for the connection is specified by the L2t
    pPsk parameter Additional parameters specify that the connection:

    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)

    By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 4: Add a VPN connection that uses a custom EAP authentication method

    This command stores the result of New-EapConfiguration into the $A variable.
    PS C:\>$A = New-EapConfiguration


    This command adds a new VPN connection named Test5 to the server with an IP address of 10.1.1.1. This connection is configured to use the custom EAP authentication method specifying the EapConfigXmlStream parameter, and using the EapConfigXmlStream method of the $A
    variable created earlier. Additional parameters specify that the connection:

    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)By specifying the PassThru parameter, you can see the configuration of the VPN connection object.
    PS C:\>Add-VpnConnection -Name "Test5" -ServerAddress "10.1.1.1" -TunnelType "L2tp" -EncryptionLevel "Required" -AuthenticationMethod Eap -SplitTunneling -AllUserConnection -RememberCredential -EapConfigXmlStream $A.EapConfigXmlStream -PassThru
    Name                  : Test5
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {CF9F624F-D7DF-48BA-BD4B-D4E34AE05148}
    TunnelType            : L2tp
    AuthenticationMethod  : {Eap}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Certificate
    UseWinlogonCredential : False
    EapConfigXmlStream    : #document
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This set of commands adds a VPN connection using a custom EAP authentication method. For more information about custom EAP authentication methods, see the New-EapConfiguration cmdlet.

Add-VpnConnection 详细信息

PS C:\Windows\system32> get-help Add-VpnConnection -detailed

名称
    Add-VpnConnection

摘要
    Adds a VPN connection to the Connection Manager phone book.


语法
    Add-VpnConnection [[-RememberCredential]] [[-UseWinlogonCredential]] [[-EapConfigXmlStream] <XmlDocument>] [-Name] <String> [-ServerAddress] <String> [[-TunnelType] {Pptp | L2tp | Sstp | Ikev2 | Automatic}] [[-EncryptionLevel] {NoEncryption | Optional | Required | M
    aximum | Custom}] [[-AuthenticationMethod] {Pap | Chap | MSChapv2 | Eap | MachineCertificate}] [[-SplitTunneling]] [[-AllUserConnection]] [[-L2tpPsk] <String>] [-CimSession <CimSession[]>] [-DnsSuffix <String>] [-Force] [-IdleDisconnectSeconds <UInt32>] [-MachineCer
    tificateEKUFilter <String[]>] [-MachineCertificateIssuerFilter <X509Certificate2>] [-PassThru] [-ServerList <CimInstance[]>] [-ThrottleLimit <Int32>] [-Confirm] [-WhatIf] [<CommonParameters>]

    Add-VpnConnection [[-RememberCredential]] [-PlugInApplicationID] <String> [-Name] <String> [-ServerAddress] <String> [[-SplitTunneling]] [-CimSession <CimSession[]>] [-DnsSuffix <String>] [-Force] [-IdleDisconnectSeconds <UInt32>] [-PassThru] [-ServerList <CimInstan
    ce[]>] [-ThrottleLimit <Int32>] -CustomConfiguration <XmlDocument> [-Confirm] [-WhatIf] [<CommonParameters>]


说明
    The Add-VpnConnection cmdlet adds a VPN connection with the specified parameters to the Connection Manager phone book. If errors occur when you create the VPN profile, the cmdlet returns the error information.


参数
    -AllUserConnection [<SwitchParameter>]
        Indicates that the cmldet adds the VPN connection to the global phone book entries.

    -AuthenticationMethod [<String[]>]
        Specifies the authentication method to use for the VPN connection. The acceptable values for this parameter are:

        -- PAP
        -- CHAP
        -- MSCHAPv2
        -- EAP

    -CimSession [<CimSession[]>]
        Runs the cmdlet in a remote session or on a remote computer. Enter a computer name or a session object, such as the output of a New-CimSession or Get-CimSession cmdlet. The default is the current session on the local computer.

    -CustomConfiguration <XmlDocument>
        Specifies an XML document that allows vendors to specify custom configuration information.

    -DnsSuffix [<String>]
        Specifies the DNS suffix of the VPN connection.

    -EapConfigXmlStream [<XmlDocument>]
        Specifies the contents of the EAP XML configuration file, which includes the EAP method ID.

    -EncryptionLevel [<String>]
        Specifies the encryption level for the VPN connection. The acceptable values for this parameter are:

        -- NoEncryption
        -- Optional
        -- Required
        -- Maximum

    -Force [<SwitchParameter>]
        Indicates that the pre-shared key (PSK) value is supplied over an insecure channel, if L2TP is used.

    -IdleDisconnectSeconds [<UInt32>]
        Specifies the time, in seconds, before an idle connection is closed.

    -L2tpPsk [<String>]
        Specifies the value of the PSK to be used for L2TP authentication. If this parameter is not specified, a certificate is used for L2TP.

    -MachineCertificateEKUFilter [<String[]>]
        Specifies an array of enhanced key usage (EKU) filters for Internet Key Exchange version 2 (IKEv2) machine certificate selection.

    -MachineCertificateIssuerFilter [<X509Certificate2>]
        Specifies the X509 certificate of the issuer filter for IKEv2 machine certificate selection.

    -Name <String>
        Specifies the name of this VPN connection profile.

    -PassThru [<SwitchParameter>]
        Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.

    -PlugInApplicationID <String>
        Specifies the identifier for a third party application.

    -RememberCredential [<SwitchParameter>]
        Indicates that the credentials supplied at the time of first successful connection are stored in the cache.

    -ServerAddress <String>
        Specifies the address of the remote VPN server to which the client connects. You can specify the address as a URL, an IPv4 address, or an IPv6 address.

    -ServerList [<CimInstance[]>]
        Specifies an array of VPN servers. The VPN client can connect to these servers.

    -SplitTunneling [<SwitchParameter>]
        Indicates that the cmdlet enables split tunneling for this VPN connection profile. When you enable split tunneling, traffic to destinations outside the intranet does not flow through the VPN tunnel. If you do not specify this parameter, split tunneling is disabl
        ed.

    -ThrottleLimit [<Int32>]
        Specifies the maximum number of concurrent operations that can be established to run the cmdlet. If this parameter is omitted or a value of 0 is entered, then Windows PowerShell® calculates an optimum throttle limit for the cmdlet based on the number of CIM cmdl
        ets that are running on the computer. The throttle limit applies only to the current cmdlet, not to the session or to the computer.

    -TunnelType [<String>]
        Specifies the type of tunnel used for the VPN connection. The acceptable values for this parameter are:

        -- PPTP
        -- L2TP
        -- SSTP
        -- IKEv2
        -- Automatic

    -UseWinlogonCredential [<SwitchParameter>]
        Indicates that MSCHAPv2 or EAP MSCHAPv2 is used as the authentication method, and that Windows logon credentials are used automatically when connecting with this VPN connection profile.

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before running the cmdlet.Prompts you for confirmation before running the cmdlet.

    -WhatIf [<SwitchParameter>]
        Shows what would happen if the cmdlet runs. The cmdlet is not run.Shows what would happen if the cmdlet runs. The cmdlet is not run.

    <CommonParameters>
        此 Cmdlet 支持常见参数: Verbose、Debug、
        ErrorAction、ErrorVariable、WarningAction、WarningVariable、
        OutBuffer、PipelineVariable 和 OutVariable。有关详细信息,请参阅
        about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216)。

    Example 1: Add a VPN connection

    PS C:\> Add-VpnConnection -Name "Test1" -ServerAddress "10.1.1.1" -PassThru
    Name                  : Test1
    ServerAddress         : 10.1.1.1
    AllUserConnection     : False
    Guid                  : {4B308E9B-C225-42CB-8158-307193429591}
    TunnelType            : Automatic
    AuthenticationMethod  : {MsChapv2}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Certificate
    UseWinlogonCredential : False
    EapConfigXmlStream    :
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : False
    SplitTunneling        : False

    This command adds a VPN connection named Test1 to the server with an IP address 10.1.1.1. By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 2: Add a VPN connection with an alternate authentication method

    PS C:\>Add-VpnConnection -Name "Test3" -ServerAddress "10.1.1.1" -TunnelType "Pptp" -EncryptionLevel "Required" -AuthenticationMethod MSChapv2 -UseWinlogonCredential -SplitTunneling -AllUserConnection -RememberCredential -PassThru
    Name                  : Test3
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {76746D4E-D72A-467D-A11F-3D4D9075F50D}
    TunnelType            : Pptp
    AuthenticationMethod  : {MsChapv2}
    EncryptionLevel       : Required
    L2tpIPsecAuth         :
    UseWinlogonCredential : True
    EapConfigXmlStream    :
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This command adds a VPN connection named Test3 to the server with an IP address 10.1.1.1. This connection uses the MSCHAPv2 authentication method, as specified by the AuthenticationMethod parameter. Additional parameters specify that the connection:

    -- Uses the Windows logon credentials (the UseWinlogonCredential parameter)
    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)

    By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 3: Add a VPN connection that uses EAP authentication

    PS C:\>Add-VpnConnection -Name "Test4" -ServerAddress "10.1.1.1" -TunnelType "L2tp" -EncryptionLevel "Required" -AuthenticationMethod Eap -SplitTunneling -AllUserConnection -L2tpPsk "password" -Force -RememberCredential -PassThru
    Name                  : Test4
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {1D423FF3-E3D4-404A-B052-DB9130656D29}
    TunnelType            : L2tp
    AuthenticationMethod  : {Eap}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Psk
    UseWinlogonCredential : False
    EapConfigXmlStream    : #document
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This command adds a VPN connection named Test4 to the server with an IP address of 10.1.1.1. This connection uses the default EAP authentication method, as specified by the AuthenticationMethod parameter. The pre-shared key for the connection is specified by the L2t
    pPsk parameter Additional parameters specify that the connection:

    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)

    By specifying the PassThru parameter, you can see the configuration of the VPN connection object.


    Example 4: Add a VPN connection that uses a custom EAP authentication method

    This command stores the result of New-EapConfiguration into the $A variable.
    PS C:\>$A = New-EapConfiguration


    This command adds a new VPN connection named Test5 to the server with an IP address of 10.1.1.1. This connection is configured to use the custom EAP authentication method specifying the EapConfigXmlStream parameter, and using the EapConfigXmlStream method of the $A
    variable created earlier. Additional parameters specify that the connection:

    -- Uses split tunneling (the SplitTunneling parameter)
    -- Is stored in the global phone book (the AllUserConnection parameter)
    -- Caches the credentials used for the first successful connection (the RememberCredential parameter)By specifying the PassThru parameter, you can see the configuration of the VPN connection object.
    PS C:\>Add-VpnConnection -Name "Test5" -ServerAddress "10.1.1.1" -TunnelType "L2tp" -EncryptionLevel "Required" -AuthenticationMethod Eap -SplitTunneling -AllUserConnection -RememberCredential -EapConfigXmlStream $A.EapConfigXmlStream -PassThru
    Name                  : Test5
    ServerAddress         : 10.1.1.1
    AllUserConnection     : True
    Guid                  : {CF9F624F-D7DF-48BA-BD4B-D4E34AE05148}
    TunnelType            : L2tp
    AuthenticationMethod  : {Eap}
    EncryptionLevel       : Required
    L2tpIPsecAuth         : Certificate
    UseWinlogonCredential : False
    EapConfigXmlStream    : #document
    ConnectionStatus      : Disconnected
    NapState              : NotConnected
    RememberCredential    : True
    SplitTunneling        : True

    This set of commands adds a VPN connection using a custom EAP authentication method. For more information about custom EAP authentication methods, see the New-EapConfiguration cmdlet.


备注
    若要查看示例,请键入: "get-help Add-VpnConnection -examples".
    有关详细信息,请键入: "get-help Add-VpnConnection -detailed".
    若要获取技术信息,请键入: "get-help Add-VpnConnection -full".
    有关在线帮助,请键入: "get-help Add-VpnConnection -online"

 

posted @ 2023-12-08 23:42  三角形  阅读(112)  评论(0编辑  收藏  举报