🕌

AVD RdAgent とかを再インストールする PowerShell Cmdlet

2022/11/08に公開

更新した

ので、こっちを見てね♪

https://zenn.dev/skmkzyk/articles/avd-rdagent-reinstall-202404

TL;DR

  • AVD の RdAgent は腐ることがある
  • その RdAgent を再インストールする PowerShell Cmdlet を作りました
  • 使い方は簡単で、ReInstall-AzWvdSessionHostAgent に Resource Group、Location、Host Pool、Session Host VM、Domain を渡すだけ

Update log

  • 全体的に # (見出し1) から ## (見出し2) に変更 - 2024/04/09
  • 更新版を作成したのでリンクの追加 - 2024/04/09
  • そのほか細かい修正など - 2024/04/09

はじめに

RdAgent という service があり、これが Azure Virtual Desktop (AVD) の中で大事な役割を占めています。

RdAgent
Session host に install された RdAgent service

が、残念ながらためにこれが腐ります。。
問題の解消は別の話題として、とりあえず困るのでわーくあらうんどとして再インストールすることがあります。
意外と面倒なので PowerShell Cmdlet にしました。
なお、動くとは思うのですが、サポートされるかは何とも言えません、ごめんなさい。

使い方

先に使い方だけ書いておきます。

ReInstall-AzWvdSessionHostAgent -ResourceGroupName avd-20221026 -Location japaneast -HostPoolName hp20221103 -SessionHostVMName sh01-1 -DomainName contoso.local

RdAgent 再インストール用 PowerShell Cmdlet

本体はこちらです。
解説は後ろの方に書いてあります。

reinstall-azwvdsessionhostagent.ps1
Function ReInstall-AzWvdSessionHostAgent() {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)] [string] $ResourceGroupName,
        [Parameter(Mandatory = $true)] [string] $Location,
        [Parameter(Mandatory = $true)] [string] $HostPoolName,
        [Parameter(Mandatory = $true)] [string] $SessionHostVMName,
        [Parameter(Mandatory = $true)] [string] $DomainName
    )

    Write-Debug "$(Get-Date -Format "h:MM:ss tt") - Deleting session host `"${SessionHostVMName}`" from session pool `"${HostPoolName}`"."
    Remove-AzWvdSessionHost -HostPoolName $HostPoolName -Name "${SessionHostVMName}.${DomainName}" -ResourceGroupName $ResourceGroupName

    Write-Debug "$(Get-Date -Format "h:MM:ss tt") - Deleting DSC extension from session host `"${SessionHostVMName}`"."
    Remove-AzVMExtension -ResourceGroupName $ResourceGroupName -Name Microsoft.PowerShell.DSC -VMName $SessionHostVMName -Force | Out-Null

    Write-Debug "$(Get-Date -Format "h:MM:ss tt") - Deleting RdAgent and AVD-related softwares from session host `"${SessionHostVMName}`"."
    Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $SessionHostVMName -CommandId RunPowerShellScript -ScriptPath .\cim.ps1 | Out-Null

    Write-Debug "$(Get-Date -Format "h:MM:ss tt") - Get AVD registration key for the host pool `"${HostPoolName}`"."
    $HostpoolToken = (Get-AzWvdRegistrationInfo -ResourceGroupName $ResourceGroupName -HostPoolName $HostPoolName).Token

    if ($HostpoolToken -eq $nul) {
        $HostpoolToken = (New-AzWvdRegistrationInfo -ResourceGroupName $ResourceGroupName -HostPoolName $HostPoolName -ExpirationTime $((get-date).ToUniversalTime().AddHours(3).ToString('yyyy-MM-ddTHH:mm:ss.fffffffZ'))).Token
    }

    Write-Debug "$(Get-Date -Format "h:MM:ss tt") - Re-installing RdAgent to session host `"${SessionHostVMName}`" for binding to host pool `"${HostPoolName}`"."
    New-AzResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile template.json -vmName $SessionHostVMName -location $Location -hostpoolToken $HostpoolToken -hostpoolName $HostPoolName | Out-Null
}

途中で実行される cim.ps1 はこちらです。
AVD にかかわるソフトウェアを uninstall しています。

cim.ps1
Get-CimInstance -Class Win32_Product -Filter "Name='Remote Desktop Agent Boot Loader'" | ForEach-Object {$_ | Invoke-CimMethod -MethodName Uninstall}
Get-CimInstance -Class Win32_Product -Filter "Name='Remote Desktop Services Infrastructure Agent'" | ForEach-Object {$_ | Invoke-CimMethod -MethodName Uninstall}
Get-CimInstance -Class Win32_Product -Filter "Name='Remote Desktop Services Infrastructure Geneva Agent'" | ForEach-Object {$_ | Invoke-CimMethod -MethodName Uninstall}

最後の部分で利用される template.json はこちらです。
Azure Portal から通常どおり deploy する際の ARM template を一部もってきたものです。
artifactsLocation の部分で日付が含まれるファイル名の zip ファイルを参照するので、たまーに更新しないといけないかなという気もします。

template.json
{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "artifactsLocation": {
            "defaultValue": "https://wvdportalstorageblob.blob.core.windows.net/galleryartifacts/Configuration_09-08-2022.zip",
            "type": "String",
            "metadata": {
                "description": "The base URI where artifacts required by this template are located."
            }
        },
        "vmName": {
            "type": "String"
        },
        "location": {
            "type": "String",
            "metadata": {
                "description": "Location for all resources to be created in."
            }
        },
        "hostpoolToken": {
            "type": "String",
            "metadata": {
                "description": "The token for adding VMs to the hostpool"
            }
        },
        "hostpoolName": {
            "type": "String",
            "metadata": {
                "description": "The name of the hostpool"
            }
        },
        "aadJoin": {
            "defaultValue": false,
            "type": "Bool",
            "metadata": {
                "description": "IMPORTANT: You can use this parameter for the test purpose only as AAD Join is public preview. True if AAD Join, false if AD join"
            }
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "apiVersion": "2018-10-01",
            "name": "[concat(parameters('vmName'), '/', 'Microsoft.PowerShell.DSC')]",
            "location": "[parameters('location')]",
            "properties": {
                "publisher": "Microsoft.Powershell",
                "type": "DSC",
                "typeHandlerVersion": "2.73",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "modulesUrl": "[parameters('artifactsLocation')]",
                    "configurationFunction": "Configuration.ps1\\AddSessionHost",
                    "properties": {
                        "hostPoolName": "[parameters('hostpoolName')]",
                        "registrationInfoToken": "[parameters('hostpoolToken')]",
                        "aadJoin": "[parameters('aadJoin')]",
                        "UseAgentDownloadEndpoint": true
                    }
                }
            }
        }
    ],
    "outputs": {}
}

PowerShell Cmdlet を少し解説

  1. Remove-AzWvdSessionHost
    AVD の Host pool から該当の Azure VM を削除します

  2. Remove-AzVMExtension
    RdAgent などはもともと DSC により install されているため、この Azure VM Extension を削除します

  3. Invoke-AzVMRunCommand
    Azure VM Extension を削除しただけでは RdAgent などは uninstall されないため、実際に OS 側で command を実行させ uninstall します

  4. Get-AzWvdRegistrationInfoNew-AzWvdRegistrationInfo
    Host pool へぶら下げるためには Registration key が必要となるため、Get- します
    期限が切れているなどで Get- できない場合には New- により再発行します

  5. New-AzResourceGroupDeployment
    Azure Portal から Session host を作成するときと同様に、DSC により RdAgent 一式を install します

参考

  • AVD RdAgent とかを再インストールする PowerShell Cmdlet (2024/04 版)

https://zenn.dev/skmkzyk/articles/avd-rdagent-reinstall-202404

  • Azure Virtual Desktop エージェントの概要

https://learn.microsoft.com/azure/virtual-desktop/agent-overview

Microsoft (有志)

Discussion