Tag AVD Personal Desktops With Assigned User

Here’s how you can use a scheduled GitHub Actions workflow to automatically tag virtual machines when users get assigned. This script will check all resource groups for HostPoolType equal to Personal and then tag each virtual machine with the assigned user. Tag Name= “AssignedUser” Value = “UPN”.

name: Create Tag for Assigned Users
'on':
  workflow_dispatch:
    inputs:
      environment:
        description: Deploy to Subscription
        required: true
        default: NONPROD
        type: choice
        options:
          - NONPROD
          - PROD-WUS2
          - PROD-WUS
          - PROD-EUS
          - PROD-WEU
          - PROD-IND
  schedule:
   # - cron: 0 14 * * 1,3,5  # 2:00 PM UTC, Monday, Wednesday, Friday. Commented out. Run manual or uncom.
jobs:
  HostPools:
    runs-on: ubuntu-latest
    environment: '${{ github.event.inputs.environment}}'
    name: Start Process
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Login to Azure
        uses: azure/login@v1
        with:
          creds: '${{secrets.A}}' #Modify to your own secret name.
          enable-AzPSSession: true
      - name: Get Assigned Users and Tag Virtual Machines
        id: task-get-Assigned-Users-and-Tag-Virtual-Machines
        uses: azure/powershell@v1
        with:
          azPSVersion: latest
          errorActionPreference: Continue
          inlineScript: |
            # Create an empty array to store the virtual machine objects
            $vms = @()

            # Get all resource groups in the subscription
            $resourceGroups = Get-AzResourceGroup

            # Loop through each resource group
            foreach ($resourceGroup in $resourceGroups) {
                $hostPools = Get-AzWvdHostPool -ResourceGroupName $resourceGroup.ResourceGroupName
                Write-Output "Checking $($hostPools.Count) host pools in resource group 
                $($resourceGroup.ResourceGroupName)"

                # Loop through each host pool in the resource group
                foreach ($hostPool in $hostPools) {
                    # Check if the hostpool is personal
                    if($hostpool.HostPoolType -eq 'Personal'){
                        Write-Output "Checking host pool $($hostPool.Name) in resource group 
                        $($resourceGroup.ResourceGroupName)"
                        $sessionHosts = Get-AzWvdSessionHost -HostPoolName $hostPool.Name - 
                        ResourceGroupName $resourceGroup.ResourceGroupName
                        Write-Output "Checking $($sessionHosts.Count) session hosts in host pool 
                        $($hostPool.Name)"
                        foreach ($sessionHost in $sessionHosts) {
                            Write-Output "Checking session host $($sessionHost.name) in host pool 
                            $($hostPool.Name)"
                            # Get the assigned user for the session host virtual machine
                            $assignedUser = $sessionHost.AssignedUser
                            # Get the virtual machine object
                            $vm = Get-AzResource -ResourceId $sessionHost.ResourceId
                            # Add the user principal name, pool name and resource group name as properties 
                            of the virtual machine object
                            $vm | Add-Member -MemberType NoteProperty -Name "UserPrincipalName" -Value 
                            $assignedUser
                            $vm | Add-Member -MemberType NoteProperty -Name "PoolName" -Value 
                            $hostPool.Name
                            $vm | Add-Member -MemberType NoteProperty -Name "ResourceGroupNameNew" -Value 
                            $resourceGroup.ResourceGroupName
                            # Check if the "AssignedUser" tag has the correct value
                            $tags = (Get-AzResource -ResourceId $sessionHost.ResourceId).Tags
                            if ($tags.'resourcecontact' -ne $assignedUser) {
                                # Update the "AssignedUser" tag with the assigned user
                                $tags.'AssignedUser' = $assignedUser
                                Set-AzResource -ResourceId $sessionHost.ResourceId -Tag $tags -Force -AsJob
                            }
                            $vms += $vm
                        }
                    }
                }
            }
Share or Save this:
Share