Microsoft Entra ID 服務可啟用許多系統管理工作,例如使用者管理、網域管理和單一登入設定。 此文章描述如何在 Azure 自動化中,使用 Microsoft Entra ID 做為提供者向 Azure 進行驗證。
安裝 Microsoft Entra 模組
您可以透過下列 PowerShell 模組啟用 Microsoft Entra ID:
Azure Active Directory PowerShell for Graph。 Azure 自動化隨附於 Az 模組。 功能包括使用 Microsoft Entra 使用者 (OrgId) 認證型驗證,對 Azure 進行非互動式驗證。 請參閱 Azure AD 2.0.2.182。
Microsoft Entra ID 用於 Windows PowerShell。 此模組可與 Microsoft Online 互動 (包括 Microsoft 365)。
安裝 PSCredential 的支援
Azure Automation uses the PSCredential class to represent a credential asset. 您的指令碼會使用 PSCredential Cmdlet 來擷取 Get-AutomationPSCredential 物件。 如需詳細資訊,請參閱 Azure 自動化中的認證資產。
指派訂用帳戶系統管理員
您必須指派 Azure 訂用帳戶的系統管理員。 此人員有訂用帳戶範圍的擁有者角色。 請參閱 Azure 自動化中的角色型存取控制。
變更 Microsoft Entra 使用者的密碼
若要變更 Microsoft Entra 使用者的密碼:
登出 Azure。
讓系統管理員使用完整的使用者名稱 (包括網域) 和暫時密碼,以剛建立的 Microsoft Entra 使用者身分登入 Azure。
要求系統管理員在出現提示時變更密碼。
設定 Azure 自動化來管理 Azure 訂用帳戶
若要讓 Azure 自動化與 Microsoft Entra ID 通訊,您必須將與 Azure 連線相關聯的認證擷取至 Azure AD。 這些認證的範例包括租用戶識別碼、訂用帳戶識別碼等等。 如需 Azure 與 Microsoft Entra ID 之間的連線詳細資訊,請參閱將組織連線到 Microsoft Entra ID。
建立認證資產
若可以使用適用於 Microsoft Entra 的 Azure 認證,就可以建立 Azure 自動化認證資產,以安全地儲存 Azure AD 認證,讓 Runbook 和期望狀態設定 (DSC) 指令碼可以加以存取。 您可以使用 Azure 入口網站或 PowerShell Cmdlet 來執行此動作。
在 Azure 入口網站中建立認證資產
您可以使用 Azure 入口網站來建立認證資產。 Do this operation from your Automation account using Credentials under Shared Resources. 請參閱 Azure 自動化中的認證資產。
使用 Windows PowerShell 建立認證資產
若要在 Windows PowerShell 中準備新的認證資產,您的指令碼會先使用指派的使用者名稱和密碼來建立 PSCredential 物件。 The script then uses this object to create the asset through a call to the New-AzureAutomationCredential cmdlet. Alternatively, the script can call the Get-Credential cmdlet to prompt the user to type in a name and password. 請參閱 Azure 自動化中的認證資產。
從 Azure 自動化 Runbook 管理 Azure 資源
您可以從使用認證資產的 Azure 自動化 Runbook 來管理 Azure 資源。 以下是 PowerShell Runbook 範例,其會收集用來停止和啟動 Azure 訂用帳戶中虛擬機器的認證資產。 此 Runbook 會先使用 Get-AutomationPSCredential 來擷取要用來向 Azure 進行驗證的認證。 It then calls the Connect-AzAccount cmdlet to connect to Azure using the credential.
Workflow Workflowname
{
Param
(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]
$AzureSubscriptionId,
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
[String]
$AzureVMList="All",
[Parameter(Mandatory=$true)][ValidateSet("Start","Stop")]
[String]
$Action
)
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
# get credential
$credential = Get-AutomationPSCredential -Name "AzureCredential"
# Connect to Azure with credential
$AzureContext = (Connect-AzAccount -Credential $credential -TenantId $AzureContext.Subscription.TenantId).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
-TenantId $AzureContext.Subscription.TenantId `
-DefaultProfile $AzureContext
if($AzureVMList -ne "All")
{
$AzureVMs = $AzureVMList.Split(",")
[System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs
}
else
{
$AzureVMs = (Get-AzVM -DefaultProfile $AzureContext).Name
[System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs
}
foreach($AzureVM in $AzureVMsToHandle)
{
if(!(Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM}))
{
throw " AzureVM : [$AzureVM] - Does not exist! - Check your inputs "
}
}
if($Action -eq "Stop")
{
Write-Output "Stopping VMs";
foreach -parallel ($AzureVM in $AzureVMsToHandle)
{
Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM} | Stop-AzVM -DefaultProfile $AzureContext -Force
}
}
else
{
Write-Output "Starting VMs";
foreach -parallel ($AzureVM in $AzureVMsToHandle)
{
Get-AzVM -DefaultProfile $AzureContext | ? {$_.Name -eq $AzureVM} | Start-AzVM -DefaultProfile $AzureContext
}
}
}
搭配 Powershell 使用 Microsoft Graph
請參閱 開始使用 Microsoft Graph PowerShell SDK
Next steps
- 如需認證使用的詳細資訊,請參閱管理 Azure 自動化中的認證。
- 如需模組的詳細資訊,請參閱在 Azure 自動化中管理模組。
- 如果您需要啟動 Runbook,請參閱在 Azure 自動化中啟動 Runbook。
- For PowerShell details, see PowerShell Docs.