建立 Microsoft Entra 私人網路連接器的無操作安裝指令碼
本文可協助您建立 Windows PowerShell 指令碼,以啟用無操作安裝和註冊 Microsoft Entra 私人網路連接器。
當您要進行下列動作時,無操作安裝會很有用:
- 在未啟用使用者介面或您無法使用遠端桌面存取的 Windows 伺服器上安裝連接器。
- 一次安裝並註冊許多連接器。
- 將連接器安裝與註冊整合成另一個程序的一部分。
- 建立一個包含連接器位元但未註冊的標準伺服器映像。
若要讓私人網路連接器能夠運作,您必須向 Microsoft Entra ID 註冊。 當您安裝連接器時,會在使用者介面中完成註冊,但您可以使用 PowerShell 將程序自動化。
自動安裝有兩個步驟。 首先,安裝連接器。 其次,使用 Microsoft Entra ID 註冊連接器。
重要
如果您要安裝適用於 Microsoft Azure Government 雲端的連接器,請檢閱必要條件和安裝步驟。 Microsoft Azure Government 雲端需要啟用對不同組 URL 的存取權,以及執行安裝的其他參數。
安裝連接器
使用下列步驟安裝連接器而不註冊連接器:
開啟命令提示字元。
執行下列命令,
/q
旗標代表無訊息安裝。 無訊息安裝不會提示您接受《使用者授權合約》。MicrosoftEntraPrivateNetworkConnectorInstaller.exe REGISTERCONNECTOR="false" /q
使用 Microsoft Entra ID 註冊連接器
有兩種方法可用來註冊連接器︰
- 使用 Windows PowerShell 認證物件註冊連接器。
- 使用離線時建立的權杖註冊連接器。
使用 Windows PowerShell 認證物件註冊連接器
建立 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
使用您所建立的
$cred
物件,移至C:\Program Files\Microsoft Entra private network connector
並執行下列指令碼:.\RegisterConnector.ps1 -modulePath "C:\Program Files\Microsoft Entra private network connector\Modules\" -moduleName "MicrosoftEntraPrivateNetworkConnectorPSModule" -Authenticationmode Credentials -Usercredentials $cred -Feature ApplicationProxy -TenantId $TenantId
指令碼包含敏感性認證資訊。 將指令碼存放在安全的位置。
使用離線時建立的權杖註冊連接器
使用此程式碼片段中的值或 PowerShell Cmdlet,建立使用
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." }
擁有權杖之後,請使用該權杖建立一個
SecureString
:$SecureToken = $Token | ConvertTo-SecureString -AsPlainText -Force
執行下列 Windows PowerShell 命令,將
<tenant GUID>
取代為您的目錄識別碼:.\RegisterConnector.ps1 -modulePath "C:\Program Files\Microsoft Entra private network connector\Modules\" -moduleName "MicrosoftEntraPrivateNetworkConnectorPSModule" -Authenticationmode Token -Token $SecureToken -TenantId <tenant GUID> -Feature ApplicationProxy
將指令碼或程式碼存放在安全的位置,因為其包含敏感性認證資訊。