Microsoft Entra プライベート ネットワーク コネクタ用の無人インストール スクリプトの作成

この記事は、Microsoft Entra プライベート ネットワーク コネクタを無人でインストールして登録できる Windows PowerShell スクリプトを作成するときに役に立ちます。

無人インストールは、次の場合に役立ちます。

  • ユーザー インターフェイスが有効でない、または Remote Desktop でアクセスできない Windows サーバーにコネクタをインストールする。
  • 一度に多数のコネクタをインストールし、登録する。
  • コネクタのインストールと登録を別の手順の一部として統合する。
  • コネクタのビットを含むが未登録の標準的なサーバー イメージを作成する。

プライベート ネットワーク コネクタを機能させるには、Microsoft Entra ID に登録する必要があります。 登録はコネクタをインストールするときにユーザー インターフェイスで行われますが、PowerShell を使ってそのプロセスを自動化できます。

無人インストールには 2 つの手順があります。 最初に、コネクタをインストールします。 次に、コネクタを Microsoft Entra ID に登録します。

重要

Microsoft Azure Government クラウドのコネクタをインストールする場合は、前提条件インストール手順を確認してください。 Microsoft Azure Government クラウドでインストールを実行するには、別の URL セットへのアクセスと追加のパラメータを有効にする必要があります。

コネクタをインストールする

次の手順に従って、コネクタを登録なしでインストールします。

  1. コマンド プロンプトを開きます。

  2. 次のコマンドを実行します。/q は、サイレント インストールを意味します。 サイレント インストールでは、使用許諾契約書への同意を求めるメッセージは表示されません。

    MicrosoftEntraPrivateNetworkConnectorInstaller.exe REGISTERCONNECTOR="false" /q
    

コネクタを Microsoft Entra ID に登録する

コネクタを登録するために使用できる方法は 2 つあります。

  • 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
    

オフラインで作成したトークンを使用してコネクタを登録する

  1. このコード スニペットの値または PowerShell コマンドレットを使い、AuthenticationContext クラスを使ってオフライン トークンを作成します。

    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. <tenant GUID> を実際のディレクトリ ID に置き換えて、次の Windows PowerShell コマンドを実行します。

    .\RegisterConnector.ps1 -modulePath "C:\Program Files\Microsoft Entra private network connector\Modules\" -moduleName "MicrosoftEntraPrivateNetworkConnectorPSModule" -Authenticationmode Token -Token $SecureToken -TenantId <tenant GUID> -Feature ApplicationProxy
    

次のステップ