共用方式為


教學課程:在 Windows 用戶端裝置上建立 Always On VPN 連線

本教學課程說明如何在 Windows 用戶端裝置上建立 Always On VPN 連線。 它繼續系列,以在範例環境中部署 Always On VPN。 先前在系列中,您已 部署範例基礎結構設定證書頒發機構單位範本

在本教學課程中,您會:

  • 建立及測試手動使用者 VPN 連線,以做為 AlwaysOn VPN 連線的範本。
  • 使用 PowerShell 將手動 VPN 連線轉換為 Always On VPN 連線。

AlwaysOn VPN 連線可以設定為裝置通道或使用者通道:

  • 裝置通道:在使用者登入裝置之前連線到指定的 VPN 伺服器。 裝置通道用於登入前連線案例和裝置管理用途。

  • 使用者通道:只有在使用者登入裝置後才連線。 使用者通道可讓使用者透過 VPN 伺服器存取組織資源。

如需裝置通道與使用者通道之間差異的詳細資訊,請參閱 在 Windows 用戶端中設定 VPN 裝置通道

Always On VPN 連線,無論是裝置通道還是使用者通道,都是使用 VPNv2 設定服務提供者 (CSP) 中的 ProfileXML 節點來設定。 本教學課程中的步驟示範如何在單一 Windows 用戶端裝置上設定使用者通道。 您可以針對小型環境或測試目的使用這些步驟。

針對較大的環境,您應該使用 Microsoft Intune 或 Microsoft Configuration Manager,將 Always On VPN 配置檔部署至 Windows 用戶端裝置。 如需詳細資訊,請參閱 使用 Microsoft Intune 將 Always On VPN 配置檔部署到具有 Microsoft Intune 的 Windows 用戶端 ,以及 使用 Microsoft Configuration Manager 將 Always On VPN 配置檔部署至 Windows 用戶端

Prerequisites

若要完成本教學課程中的步驟,您需要:

建立及測試手動使用者 VPN 連線

首先,您必須建立 VPN 用戶端連線,以確認 VPN 測試用戶端可以建立成功的 VPN 連線。 此連線也可讓您建立可延伸驗證通訊協定 (EAP) 設定,以用於 AlwaysOn VPN 連線。 如需 EAP 設定的詳細資訊,請參閱 EAP 設定

  1. 以您先前在 建立 Active Directory 測試使用者中建立的 VPN 使用者身分登入已加入網域的 Windows 用戶端裝置。

  2. 在 [開始] 功能表上,輸入 VPN 以選取 [VPN 設定]。 按 ENTER。

  3. 在詳細資料窗格中,選取 [新增 VPN 連線]。

  4. 針對 VPN 提供者,選取 Windows (內建)。

  5. 針對 [連線名稱],輸入 Contoso VPN

  6. 針對 伺服器名稱或位址,輸入 VPN 伺服器的外部完整域名(例如vpn.contoso.com)。

  7. 針對 VPN 類型,選取 [IKEv2]。

  8. 針對 [登入資訊類型],選取 [憑證]。

  9. 選取 [儲存]。

  10. [相關設定] 底下,選取 [ 變更配接器選項]。

  11. 以滑鼠右鍵按一下 [Contoso VPN],然後選取 [屬性]。

  12. 在 [ 安全性 ] 索引標籤上,針對 [資料加密],選取 [最大強度加密]。

  13. 選取 [使用可延伸的驗證通訊協定 (EAP)]。 然後,針對 [使用可延伸的驗證通訊協定 (EAP)],選取 [Microsoft:受保護的 EAP (PEAP) (已啟用加密)]。

  14. 選取 [屬性] 以開啟 [受保護的 EAP 屬性],然後完成下列步驟:

    1. 針對 [連線到這些伺服器],輸入 NPS 伺服器的主機名。

    2. 針對 受信任的根憑證授權單位,選取核發 NPS 伺服器憑證的 CA(例如contoso-CA)。

    3. 針對 [連線前的通知],選取 [不要要求使用者授權新伺服器或受信任的 CA]。

    4. 針對 [選取驗證方法],選取 [智慧卡或其他憑證]。

    5. 選取設定

      1. 選取 [使用這台電腦上的憑證]。

      2. 針對 [連線到這些伺服器],輸入 NPS 伺服器的主機名。

      3. 針對 [受信任的根憑證授權單位] 中,選擇發出 NPS 伺服器憑證的 CA。

      4. 選取 [不提示使用者授權新伺服器或受信任的憑證授權單位]。

      5. 選取 [ 確定 ] 以關閉 智慧卡或其他憑證屬性

      6. 選取 [ 確定 ] 以關閉 受保護的 EAP 屬性

    6. 選取 [ 確定 ] 以關閉 Contoso VPN 屬性

  15. 關閉 「網路連線」 視窗。

  16. 在 [設定] 中,選取 [Contoso VPN],然後選取 [連線]。 應成功建立 VPN 連線。 您可以檢查「 網路連線」 視窗來驗證連線。 Contoso VPN 連線應該會顯示為 [已連線]。 測試您可以連線到 VPN 通道另一端的資源,例如檔案共用或網頁伺服器。

  17. 確認 VPN 連線成功之後,請中斷與 VPN 連線的連線。

Important

請確定範本 VPN 已成功連線至 VPN 伺服器。 這麼做可確保 EAP 設定正確無誤,再在下一節中使用它們。 您至少必須連線一次,才能繼續;否則,配置檔不包含連線到 VPN 所需的所有資訊。

將手動 VPN 連線轉換為 AlwaysOn VPN 連線

接下來,您會使用 PowerShell 腳本,將手動 VPN 連線轉換成 Always On VPN 連線。

  1. 與 Windows 用戶端裝置上的相同使用者,以系統管理員身分開啟 Windows PowerShell ISE

  2. 將下列 PowerShell 腳本複製並貼到 Windows PowerShell ISE 編輯器視窗中,然後請務必變更您自己的八個變數值。

    展開本節以顯示 PowerShell 腳本。
    # Set the variables for the VPN profile.
    $domain = 'corp' # Name of the domain.
    $templateName = 'Contoso VPN' # Name of the test VPN connection you created in the tutorial. 
    $profileName = 'Contoso AlwaysOn VPN' # Name of the profile we are going to create.
    $servers = 'aov-vpn.contoso.com' # Public or routable IP address or DNS name for the VPN gateway.
    $dnsSuffix = 'corp.contoso.com' # Specifies one or more commas separated DNS suffixes. 
    $domainName = '.corp.contoso.com' # Used to indicate the namespace to which the policy applies. Contains `.` prefix.
    $dnsServers = '10.10.0.6' # List of comma-separated DNS Server IP addresses to use for the namespace.
    $trustedNetwork = 'corp.contoso.com' # Comma-separated string to identify the trusted network.
    
    # Get the EAP settings for the current profile called $templateName
    $connection = Get-VpnConnection -Name $templateName
    
    if(!$connection)
    {
        $message = "Unable to get $templateName connection profile: $_"
        Write-Host "$message"
        exit
    }
    
    $EAPSettings= $connection.EapConfigXmlStream.InnerXml
    
    $profileNameEscaped = $profileName -replace ' ', '%20'
    
    # Define ProfileXML
    $profileXML = @("
    <VPNProfile>
      <DnsSuffix>$dnsSuffix</DnsSuffix>
      <NativeProfile>
    <Servers>$servers</Servers>
    <NativeProtocolType>IKEv2</NativeProtocolType>
    <Authentication>
      <UserMethod>Eap</UserMethod>
      <Eap>
        <Configuration>
        $EAPSettings
        </Configuration>
      </Eap>
    </Authentication>
    <RoutingPolicyType>SplitTunnel</RoutingPolicyType>
      </NativeProfile>
    <AlwaysOn>true</AlwaysOn>
    <RememberCredentials>true</RememberCredentials>
    <TrustedNetworkDetection>$trustedNetwork</TrustedNetworkDetection>
      <DomainNameInformation>
    <DomainName>$domainName</DomainName>
    <DnsServers>$dnsServers</DnsServers>
    </DomainNameInformation>
    </VPNProfile>
    ")
    
    # Output the XML for possible use in Intune
    $profileXML | Out-File -FilePath ($env:USERPROFILE + '\desktop\VPN_Profile.xml')
    
    # Escape special characters in the profile (<,>,")
    $profileXML = $profileXML -replace '<', '&lt;'
    $profileXML = $profileXML -replace '>', '&gt;'
    $profileXML = $profileXML -replace '"', '&quot;'
    
    # Define WMI-to-CSP Bridge properties
    $nodeCSPURI = "./Vendor/MSFT/VPNv2"
    $namespaceName = "root\cimv2\mdm\dmmap"
    $className = "MDM_VPNv2_01"
    
    try
    {
    
        # Determine user SID for VPN profile.
        $WmiLoggedOnUsers = (Get-WmiObject Win32_LoggedOnUser).Antecedent
        If($WmiLoggedOnUsers.Count -gt 1) { 
            $WmiLoggedOnUsers = $WmiLoggedOnUsers -match "Domain=""$domain"""
        }
    
        $WmiUserValid = ($WmiLoggedOnUsers | Select-Object -Unique -First 1) -match 'Domain="([^"]+)",Name="([^"]+)"'
    
        If(-not $WmiUserValid){
            Throw "Returned object is not a valid WMI string"
        }
    
        $UserName = "$($Matches[1])\$($Matches[2])"
    
        $ObjUser = New-Object System.Security.Principal.NTAccount($UserName)
        $Sid = $ObjUser.Translate([System.Security.Principal.SecurityIdentifier])
        $SidValue = $Sid.Value
        $message = "User SID is $SidValue."
    
        Write-Host "$message"
    
    }
    catch [Exception] 
    {
    
        $message = "Unable to get user SID. $_"
        Write-Host "$message" 
        exit
    }
    
    try 
    {
    
        # Define WMI session.
        $session = New-CimSession
        $options = New-Object Microsoft.Management.Infrastructure.Options.CimOperationOptions
        $options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Type", "PolicyPlatform_UserContext", $false)
        $options.SetCustomOption("PolicyPlatformContext_PrincipalContext_Id", "$SidValue", $false)
    
    }
    catch {
    
        $message = "Unable to create new session for $profileName profile: $_"
        Write-Host $message
        exit
    }
    
    try
    {
    
        # Detect and delete previous VPN profile.
        $deleteInstances = $session.EnumerateInstances($namespaceName, $className, $options)
    
        foreach ($deleteInstance in $deleteInstances)
        {
            $InstanceId = $deleteInstance.InstanceID
            if ("$InstanceId" -eq "$profileNameEscaped")
            {
                $session.DeleteInstance($namespaceName, $deleteInstance, $options)
                $message = "Removed $profileName profile $InstanceId" 
                Write-Host "$message"
            }
            else 
            {
                $message = "Ignoring existing VPN profile $InstanceId"
                Write-Host "$message"
            }
        }
    }
    catch [Exception]
    {
        $message = "Unable to remove existing outdated instance(s) of $profileName profile: $_"
        Write-Host $message
        exit
    
    }
    
    try
    {
    
        # Create the VPN profile.
        $newInstance = New-Object Microsoft.Management.Infrastructure.CimInstance $className, $namespaceName
        $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ParentID", "$nodeCSPURI", "String", "Key")
        $newInstance.CimInstanceProperties.Add($property)
        $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("InstanceID", "$profileNameEscaped", "String", "Key")
        $newInstance.CimInstanceProperties.Add($property)
        $property = [Microsoft.Management.Infrastructure.CimProperty]::Create("ProfileXML", "$profileXML", "String", "Property")
        $newInstance.CimInstanceProperties.Add($property)
        $session.CreateInstance($namespaceName, $newInstance, $options)
    
        $message = "Created $profileName profile."
        Write-Host "$message"
    
    }
    catch [Exception]
    {
    
        $message = "Unable to create $profileName profile: $_"
        Write-Host "$message"
        exit
    }
    
    $message = "Always On VPN connection created successfully."
    Write-Host "$message"
    
  3. 執行腳本。

  4. 在編輯器視窗中執行下列命令 Windows PowerShell ISE ,確認文稿是否成功:

    Get-CimInstance -Namespace root\cimv2\mdm\dmmap -ClassName MDM_VPNv2_01
    

    輸出應該類似下列範例( ProfileXML 此值會截斷以供可讀性):

    AlwaysOn                : True
    ByPassForLocal          : 
    DeviceTunnel            : 
    DnsSuffix               : corp.contoso.com
    EdpModeId               : 
    InstanceID              : Contoso%20AlwaysOn%20VPN
    LockDown                : 
    ParentID                : ./Vendor/MSFT/VPNv2
    ProfileXML              : <VPNProfile>...</VPNProfile>
    RegisterDNS             : 
    RememberCredentials     : True
    TrustedNetworkDetection : corp.contoso.com
    PSComputerName          : 
    
  5. 應成功建立 Always On VPN 連線。 您可以檢查「 網路連線」 視窗來驗證連線。 Contoso Always On VPN 連線應該會顯示為 [已連線]。 測試您可以連線到 VPN 通道另一端的資源,例如檔案共用或網頁伺服器。

現在您使用 Always On VPN 連線連線到 VPN 伺服器,以下是一些額外的資源,可協助您進行 AlwaysOn VPN 部署: