为 Microsoft Entra 专用网络连接器创建无人参与安装脚本

本项目可帮助你创建 Windows PowerShell 脚本,来在无人参与的情况下安装和注册 Microsoft Entra 专用网络连接器。

如果想要执行以下操作,则无人参与安装很有用:

  • 在未启用用户界面或无法使用远程桌面进行访问的 Windows 服务器上安装连接器。
  • 一次性安装并注册多个连接器。
  • 将连接器安装与注册集成为另一个过程的一部分。
  • 创建包含连接器代码但未注册的标准服务器映像。

要使专用网络连接器正常工作,必须将其注册到 Microsoft Entra ID。 安装连接器时,会在用户界面中完成注册,但你可以使用 PowerShell 自动执行该过程。

无人参与安装包括两个步骤。 第一步,安装连接器。 其次,将连接器注册到 Microsoft Entra ID。

重要

如果要为 Microsoft Azure 政府云安装连接器,请查看先决条件安装步骤。 Microsoft Azure 政府云需要能够访问一组不同的 URL 和一个额外的参数来运行安装。

安装连接器

使用以下步骤免注册安装连接器:

  1. 打开命令提示符。

  2. 运行以下命令,/q 表示静默安装。 静默安装不会提示接受最终用户许可协议。

    MicrosoftEntraPrivateNetworkConnectorInstaller.exe REGISTERCONNECTOR="false" /q
    

将连接器注册到 Microsoft Entra ID

可以使用两种方法注册连接器:

  • 使用 Windows PowerShell 凭据对象注册连接器。
  • 使用离线创建的令牌注册连接器。

使用 Windows PowerShell 凭据对象注册连接器

  1. 创建包含目录管理用户名和密码的 Windows PowerShell 凭据对象 $cred。 运行以下命令,替换 <username><password><tenantid>

    $User = "<username>"
    $PlainPassword = '<password>'
    $TenantId = '<tenantid>'
    $SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
    $cred = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $SecurePassword
    
  2. 转到 C:\Program Files\Microsoft Entra private network connector 并使用创建的 $cred 对象运行以下脚本:

    .\RegisterConnector.ps1 -modulePath "C:\Program Files\Microsoft Entra private network connector\Modules\" -moduleName "MicrosoftEntraPrivateNetworkConnectorPSModule" -Authenticationmode Credentials -Usercredentials $cred -Feature ApplicationProxy -TenantId $TenantId
    
  3. 该脚本包含敏感的凭据信息。 请将该脚本存储在一个安全的位置。

使用离线创建的令牌注册连接器

  1. 通过 AuthenticationContext 类使用此代码片段或 PowerShell cmdlet 中的值创建离线令牌:

    使用 C#:

    using System;
    using System.Linq;
    using System.Collections.Generic;
    using Microsoft.Identity.Client;
    
    class Program
    {
       #region constants
       /// <summary>
       /// The AAD authentication endpoint uri
       /// </summary>
       static readonly string AadAuthenticationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize";
    
       /// <summary>
       /// The application ID of the connector in AAD
       /// </summary>
       static readonly string ConnectorAppId = "55747057-9b5d-4bd4-b387-abf52a8bd489";
    
       /// <summary>
       /// The AppIdUri of the registration service in AAD
       /// </summary>
       static readonly string RegistrationServiceAppIdUri = "https://proxy.cloudwebappproxy.net/registerapp/user_impersonation";
    
       #endregion
    
       #region private members
       private string token;
       private string tenantID;
       #endregion
    
       public void GetAuthenticationToken()
       {
          IPublicClientApplication clientApp = PublicClientApplicationBuilder
             .Create(ConnectorAppId)
             .WithDefaultRedirectUri() // will automatically use the default Uri for native app
             .WithAuthority(AadAuthenticationEndpoint)
             .Build();
    
          AuthenticationResult authResult = null;
    
          IAccount account = null;
    
          IEnumerable<string> scopes = new string[] { RegistrationServiceAppIdUri };
    
          try
          {
          authResult = await clientApp.AcquireTokenSilent(scopes, account).ExecuteAsync();
          }
          catch (MsalUiRequiredException ex)
          {
          authResult = await clientApp.AcquireTokenInteractive(scopes).ExecuteAsync();
          }
    
          if (authResult == null || string.IsNullOrEmpty(authResult.AccessToken) || string.IsNullOrEmpty(authResult.TenantId))
          {
          Trace.TraceError("Authentication result, token or tenant id returned are null");
          throw new InvalidOperationException("Authentication result, token or tenant id returned are null");
          }
    
          token = authResult.AccessToken;
          tenantID = authResult.TenantId;
       }
    }
    

    使用 PowerShell:

    # Load MSAL (Tested with version 4.7.1) 
    
    Add-Type -Path "..\MSAL\Microsoft.Identity.Client.dll"
    
    # The AAD authentication endpoint uri
    
    $authority = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
    
    #The application ID of the connector in AAD
    
    $connectorAppId = "55747057-9b5d-4bd4-b387-abf52a8bd489";
    
    #The AppIdUri of the registration service in AAD
    $registrationServiceAppIdUri = "https://proxy.cloudwebappproxy.net/registerapp/user_impersonation"
    
    # Define the resources and scopes you want to call
    
    $scopes = New-Object System.Collections.ObjectModel.Collection["string"] 
    
    $scopes.Add($registrationServiceAppIdUri)
    
    $app = [Microsoft.Identity.Client.PublicClientApplicationBuilder]::Create($connectorAppId).WithAuthority($authority).WithDefaultRedirectUri().Build()
    
    [Microsoft.Identity.Client.IAccount] $account = $null
    
    # Acquiring the token
    
    $authResult = $null
    
    $authResult = $app.AcquireTokenInteractive($scopes).WithAccount($account).ExecuteAsync().ConfigureAwait($false).GetAwaiter().GetResult()
    
    # Check AuthN result
    If (($authResult) -and ($authResult.AccessToken) -and ($authResult.TenantId)) {
    
       $token = $authResult.AccessToken
       $tenantId = $authResult.TenantId
    
       Write-Output "Success: Authentication result returned."
    }
    Else {
    
       Write-Output "Error: Authentication result, token or tenant id returned with null."
    
    } 
    
  2. 拥有令牌后,使用令牌创建 SecureString

    $SecureToken = $Token | ConvertTo-SecureString -AsPlainText -Force
    
  3. 运行以下 Windows PowerShell 命令,将 <tenant GUID> 替换为目录 ID:

    .\RegisterConnector.ps1 -modulePath "C:\Program Files\Microsoft Entra private network connector\Modules\" -moduleName "MicrosoftEntraPrivateNetworkConnectorPSModule" -Authenticationmode Token -Token $SecureToken -TenantId <tenant GUID> -Feature ApplicationProxy
    
  4. 请将该脚本或代码存储在一个安全的位置,因为它包含敏感的凭据信息。

后续步骤