在本文中,您將瞭解使用 Azure 自動化 Runbook 和 Azure 邏輯應用程式在 Azure 虛擬桌面環境中自動調整會話主機 VM 的調整工具。 若要深入瞭解調整工具,請參閱使用 Azure 自動化 和 Azure Logic Apps 調整會話主機。
注意事項
Azure 虛擬桌面的原生自動調整解決方案通常適用於集區和個人主機集區 (的) ,並會根據調整排程自動相應縮小或相應放大會話主機 VM。 建議您使用自動調整以更輕鬆地進行設定。 如需詳細資訊,請 參閱自動調整規模調整計劃。
您無法使用 Azure 自動化 和 Azure Logic Apps 以及相同主機集區上的自動調整來調整會話主機。 您必須使用其中一個。
必要條件
開始設定調整工具之前,請確定您已備妥下列專案:
- Azure 虛擬桌面主機集區。
- 已設定並註冊 Azure 虛擬桌面服務的會話主機集區 VM。
- 在 Azure 訂用帳戶上指派 參與者角色型 訪問控制 (RBAC) 角色來建立資源的使用者。 您也需要應用程式 系統管理員 和/或 擁有者 RBAC 角色來建立受控識別。
- Log Analytics 工作區 (選擇性) 。
您用來部署工具的電腦必須具有:
- PowerShell 5.1 或更新版本
- Azure Az PowerShell 模組
如果您已準備就緒,讓我們開始吧。
建立或更新 Azure 自動化 帳戶
注意事項
如果您已經有執行舊版調整腳本之 Runbook 的 Azure 自動化 帳戶,您只需要依照下列指示確定已更新。
首先,您需要 Azure 自動化 帳戶來執行 PowerShell Runbook。 即使您有想要用來設定 PowerShell Runbook 的現有 Azure 自動化 帳戶,本節描述的程式仍有效。 以下是設定方式:
開啟 PowerShell。
執行下列 Cmdlet 以登入您的 Azure 帳戶。
Login-AzAccount
注意事項
您的帳戶必須擁有您想要部署調整工具之 Azure 訂用帳戶的參與者許可權。
執行下列 Cmdlet 以下載用來建立 Azure 自動化 帳戶的腳本:
New-Item -ItemType Directory -Path "C:\Temp" -Force Set-Location -Path "C:\Temp" $Uri = "https://raw.githubusercontent.com/Azure/RDS-Templates/master/wvd-templates/wvd-scaling-script/CreateOrUpdateAzAutoAccount.ps1" # Download the script Invoke-WebRequest -Uri $Uri -OutFile ".\CreateOrUpdateAzAutoAccount.ps1"
執行下列 Cmdlet 來執行腳本,並建立 Azure 自動化 帳戶。 您可以填入參數的值,或將它們加上批註以使用其預設值。
$Params = @{ "AADTenantId" = "<Azure_Active_Directory_tenant_ID>" # Optional. If not specified, it will use the current Azure context "SubscriptionId" = "<Azure_subscription_ID>" # Optional. If not specified, it will use the current Azure context "UseARMAPI" = $true "ResourceGroupName" = "<Resource_group_name>" # Optional. Default: "WVDAutoScaleResourceGroup" "AutomationAccountName" = "<Automation_account_name>" # Optional. Default: "WVDAutoScaleAutomationAccount" "Location" = "<Azure_region_for_deployment>" "WorkspaceName" = "<Log_analytics_workspace_name>" # Optional. If specified, Log Analytics will be used to configure the custom log table that the runbook PowerShell script can send logs to } .\CreateOrUpdateAzAutoAccount.ps1 @Params
注意事項
如果您的原則不允許您在特定區域中建立調整腳本資源,請更新原則指派,並將您要的區域新增至允許的區域清單。
如果您之前尚未建立自動化帳戶,Cmdlet 的輸出會在自動化帳戶變數中包含加密的 Webhook URI。 請務必保留 URI 的記錄,因為當您設定 Azure 邏輯應用程式的執行排程時,會使用它作為參數。 如果您要更新現有的自動化帳戶,您可以使用 PowerShell 擷取 Webhook URI 來存取變數。
如果您為 Log Analytics 指定了 WorkspaceName 參數,Cmdlet 的輸出也會包含 Log Analytics 工作區識別碼及其主鍵。 請記下工作區標識碼和主鍵,因為當您設定 Azure 邏輯應用程式的執行排程時,稍後必須再次搭配參數使用它們。
設定 Azure 自動化 帳戶之後,請登入您的 Azure 訂用帳戶,並檢查以確定您的 Azure 自動化 帳戶和相關的 Runbook 已出現在指定的資源群組中,如下圖所示:
若要檢查 Webhook 是否應位於何處,請選取 Runbook 的名稱。 接下來,移至 Runbook 的 [資源] 區段,然後選取 [Webhook]。
建立受控識別
既然您有 Azure 自動化 帳戶,如果您尚未設定受控識別,也必須設定受控識別。 受控識別可協助您的 Runbook 存取其他 Microsoft Entra 相關資源,以及驗證重要的自動化程式。
若要設定受控識別,請遵循針對 Azure 自動化 帳戶使用系統指派的受控識別中的指示。 建立受控識別之後,請將適當的參與者許可權指派給 Azure 虛擬桌面資源,例如主機集區、VM 等。完成之後,請返回本文和 建立 Azure 邏輯應用程式和執行排程 ,以完成初始設定程式。
建立 Azure 邏輯應用程式和執行排程
最後,您必須建立 Azure 邏輯應用程式,並為新的調整工具設定執行排程。 首先,如果您還沒有,請下載並匯入要在PowerShell工作階段中使用的 桌面虛擬化 PowerShell 模組 。
開啟 PowerShell。
執行下列 Cmdlet 以登入您的 Azure 帳戶。
Login-AzAccount
執行下列 Cmdlet 以下載用來建立 Azure 邏輯應用程式的腳本。
New-Item -ItemType Directory -Path "C:\Temp" -Force Set-Location -Path "C:\Temp" $Uri = "https://raw.githubusercontent.com/Azure/RDS-Templates/master/wvd-templates/wvd-scaling-script/CreateOrUpdateAzLogicApp.ps1" # Download the script Invoke-WebRequest -Uri $Uri -OutFile ".\CreateOrUpdateAzLogicApp.ps1"
執行下列 PowerShell 腳本,為您的主機集區建立 Azure 邏輯應用程式和執行排程
注意事項
您必須針對想要自動調整的每個主機集區執行此腳本,但您只需要一個 Azure 自動化 帳戶。
$AADTenantId = (Get-AzContext).Tenant.Id $AzSubscription = Get-AzSubscription | Out-GridView -OutputMode:Single -Title "Select your Azure Subscription" Select-AzSubscription -Subscription $AzSubscription.Id $ResourceGroup = Get-AzResourceGroup | Out-GridView -OutputMode:Single -Title "Select the resource group for the new Azure Logic App" $WVDHostPool = Get-AzResource -ResourceType "Microsoft.DesktopVirtualization/hostpools" | Out-GridView -OutputMode:Single -Title "Select the host pool you'd like to scale" $LogAnalyticsWorkspaceId = Read-Host -Prompt "If you want to use Log Analytics, enter the Log Analytics Workspace ID returned by when you created the Azure Automation account, otherwise leave it blank" $LogAnalyticsPrimaryKey = Read-Host -Prompt "If you want to use Log Analytics, enter the Log Analytics Primary Key returned by when you created the Azure Automation account, otherwise leave it blank" $RecurrenceInterval = Read-Host -Prompt "Enter how often you'd like the job to run in minutes, e.g. '15'" $BeginPeakTime = Read-Host -Prompt "Enter the start time for peak hours in local time, e.g. 9:00" $EndPeakTime = Read-Host -Prompt "Enter the end time for peak hours in local time, e.g. 18:00" $TimeDifference = Read-Host -Prompt "Enter the time difference between local time and UTC in hours, e.g. +5:30" $SessionThresholdPerCPU = Read-Host -Prompt "Enter the maximum number of sessions per CPU that will be used as a threshold to determine when new session host VMs need to be started during peak hours" $MinimumNumberOfRDSH = Read-Host -Prompt "Enter the minimum number of session host VMs to keep running during off-peak hours" $MaintenanceTagName = Read-Host -Prompt "Enter the name of the Tag associated with VMs you don't want to be managed by this scaling tool" $LimitSecondsToForceLogOffUser = Read-Host -Prompt "Enter the number of seconds to wait before automatically signing out users. If set to 0, any session host VM that has user sessions, will be left untouched" $LogOffMessageTitle = Read-Host -Prompt "Enter the title of the message sent to the user before they are forced to sign out" $LogOffMessageBody = Read-Host -Prompt "Enter the body of the message sent to the user before they are forced to sign out" $WebhookURI = Read-Host -Prompt "Enter the webhook URI that has already been generated for this Azure Automation account. The URI is stored as encrypted in the above Automation Account variable. To retrieve the value, see https://learn.microsoft.com/azure/automation/shared-resources/variables?tabs=azure-powershell#powershell-cmdlets-to-access-variables" $Params = @{ "AADTenantId" = $AADTenantId # Optional. If not specified, it will use the current Azure context "SubscriptionID" = $AzSubscription.Id # Optional. If not specified, it will use the current Azure context "ResourceGroupName" = $ResourceGroup.ResourceGroupName # Optional. Default: "WVDAutoScaleResourceGroup" "Location" = $ResourceGroup.Location # Optional. Default: "West US2" "UseARMAPI" = $true "HostPoolName" = $WVDHostPool.Name "HostPoolResourceGroupName" = $WVDHostPool.ResourceGroupName # Optional. Default: same as ResourceGroupName param value "LogAnalyticsWorkspaceId" = $LogAnalyticsWorkspaceId # Optional. If not specified, script will not log to the Log Analytics "LogAnalyticsPrimaryKey" = $LogAnalyticsPrimaryKey # Optional. If not specified, script will not log to the Log Analytics "RecurrenceInterval" = $RecurrenceInterval # Optional. Default: 15 "BeginPeakTime" = $BeginPeakTime # Optional. Default: "09:00" "EndPeakTime" = $EndPeakTime # Optional. Default: "17:00" "TimeDifference" = $TimeDifference # Optional. Default: "-7:00" "SessionThresholdPerCPU" = $SessionThresholdPerCPU # Optional. Default: 1 "MinimumNumberOfRDSH" = $MinimumNumberOfRDSH # Optional. Default: 1 "MaintenanceTagName" = $MaintenanceTagName # Optional. "LimitSecondsToForceLogOffUser" = $LimitSecondsToForceLogOffUser # Optional. Default: 1 "LogOffMessageTitle" = $LogOffMessageTitle # Optional. Default: "Machine is about to shutdown." "LogOffMessageBody" = $LogOffMessageBody # Optional. Default: "Your session will be logged off. Please save and close everything." "WebhookURI" = $WebhookURI } .\CreateOrUpdateAzLogicApp.ps1 @Params
執行腳本之後,Azure 邏輯應用程式應該會出現在資源群組中,如下圖所示。
若要變更執行排程,例如變更周期間隔或時區,請移至 Azure 邏輯應用程式自動調整排程器,然後選取 [編輯] 以移至 Azure 邏輯應用程式 Designer。
管理調整工具
現在您已建立調整工具,可以存取其輸出。 本節說明您可能會覺得有用的一些功能。
檢視作業狀態
您可以檢視所有 Runbook 作業的摘要狀態,或檢視 Azure 入口網站 中特定 Runbook 作業的更深入狀態。
在您選取的 Azure 自動化 帳戶右側的 [作業統計數據] 底下,您可以檢視所有 Runbook 作業的摘要清單。 開啟視窗左側的 [ 作業 ] 頁面會顯示目前的作業狀態、開始時間和完成時間。
檢視記錄和調整工具輸出
您可以開啟 Runbook 並選取作業,以檢視相應放大和相應縮小作業的記錄。
流覽至裝載 Azure 自動化 帳戶之資源群組中的 Runbook,然後選取 [概觀]。 在 [概觀] 頁面上,選取 [ 最近使用的作業 ] 底下的作業,以檢視其調整工具輸出,如下圖所示。
檢查 Runbook 文稿版本號碼
您可以在 Azure 自動化 帳戶中開啟 Runbook 檔案,然後選取 [檢視],以檢查您使用的 Runbook 腳本版本。 Runbook 的腳本會出現在畫面右側。 在文稿中,您會在 區段底下看到格式 v#.#.#
的 SYNOPSIS
版本號碼。 您可以 在這裡找到最新的版本號碼。 如果您在 Runbook 命令稿中看不到版本號碼,這表示您正在執行舊版的腳本,您應該立即更新它。 如果您需要更新 Runbook 腳本,請遵循建立或更新 Azure 自動化 帳戶中的指示。
報告問題
當您回報問題時,您必須提供下列資訊來協助我們進行疑難解答:
作業中 [ 所有記錄 ] 索引標籤中造成問題的完整記錄。 若要瞭解如何取得記錄,請遵循 檢視記錄和調整工具輸出中的指示。 如果記錄中有任何機密或私人資訊,您可以在將問題提交給我們之前移除它。
您使用的 Runbook 命令稿版本。 若要瞭解如何取得版本號碼,請 參閱檢查 Runbook 腳本版本號碼
安裝在您 Azure 自動化 帳戶中下列每個 PowerShell 模組的版本號碼。 若要尋找這些模組,請開啟 Azure 自動化 帳戶,選取視窗左側窗格中 [共用資源] 區段下的 [模組],然後搜尋模組的名稱。
- Az.Accounts
- Az.Compute
- Az.Resources
- Az.Automation
- OMSIngestionAPI
- Az.DesktopVirtualization
Log Analytics
如果您決定使用 Log Analytics,您可以在 Log Analytics 工作區的 [記錄] 檢視中,檢視名為 WVDTenantScale_CL 自定義記錄檔中的所有記錄數據。 我們列出了一些您可能會覺得有用的範例查詢。
若要查看主機集區的所有記錄,請輸入下列查詢:
WVDTenantScale_CL | where hostpoolName_s == "<host_pool_name>" | project TimeStampUTC = TimeGenerated, TimeStampLocal = TimeStamp_s, HostPool = hostpoolName_s, LineNumAndMessage = logmessage_s, AADTenantId = TenantId
若要檢視主機集區中目前執行中的會話主機 VM 和作用中用戶會話總數,請輸入下列查詢:
WVDTenantScale_CL | where logmessage_s contains "Number of running session hosts:" or logmessage_s contains "Number of user sessions:" or logmessage_s contains "Number of user sessions per Core:" | where hostpoolName_s == "<host_pool_name>" | project TimeStampUTC = TimeGenerated, TimeStampLocal = TimeStamp_s, HostPool = hostpoolName_s, LineNumAndMessage = logmessage_s, AADTenantId = TenantId
若要檢視主機集區中所有會話主機 VM 的狀態,請輸入下列查詢:
WVDTenantScale_CL | where logmessage_s contains "Session host:" | where hostpoolName_s == "<host_pool_name>" | project TimeStampUTC = TimeGenerated, TimeStampLocal = TimeStamp_s, HostPool = hostpoolName_s, LineNumAndMessage = logmessage_s, AADTenantId = TenantId
若要檢視任何錯誤和警告,請輸入下列查詢:
WVDTenantScale_CL | where logmessage_s contains "ERROR:" or logmessage_s contains "WARN:" | project TimeStampUTC = TimeGenerated, TimeStampLocal = TimeStamp_s, HostPool = hostpoolName_s, LineNumAndMessage = logmessage_s, AADTenantId = TenantId
限制
以下是使用此調整文本調整會話主機 VM 的一些限制:
- 調整腳本不會考慮標準與日光節約之間的時間變更。