2 minute read

The Mystery of the Assigned AVD Host

At some point in your Azure career you will come across a project that has a great naming scheme that they stick too but you almost need a dictonary to find abreviations for each part.

I’ve encountered this problem time and time again and more recently after deploying Azure Virtual Desktop. All of our hosts have a prefix such as CATEAZ followed by the VM number so the virtual machine name in Azure looks like CATEAZ-01. This makes it great for quickly finding all your AVD hosts looking for virtual machines with the prefix CATEAZ but difficult to determine whos virtual machines is whos when using Azure Virtual Desktop with a Personal HostPool without digging into the AVD Host Pool resource.

I solved this problem by writing some handy PowerShell that runs hourly via Azure Automation that will go through every host in our Azure Virtual Desktop pool and ensure it has a Tag with the Assigned User on the resource.

Lets get started I’m assuming you have the entire AZ Module installed with all sub-modules.

Getting the AVD Hosts

$SubID = "xxxx-xxx-xxx-xxx"
$AVD_HOST_RGP = "AVD-HOST-RGP"
$AVD_HOSTPOOL = "AVD-HOSTPOOL"
$AVD_HOST_VMS_RGP = "AVD-HOST-VMS-RGP"
$AVD_APP_GROUP = "AVD-DESKTOP-GROUP"

#Start by setting the AZ Context to the Subscription where your AVD Resources are
Set-AzContext -Subscription $SubID

#Get your AVD Host Pool by providing the Resource Group Name and the Name of the HostPool
$HostPool = Get-AzWvdHostPool -ResourceGroupName $AVD_HOST_RGP -Name $AVD_HOSTPOOL

#Get your AVD Desktop Group 
$AVD_DesktopGroup = Get-AzWvdDesktop -ResourceGroupName $AVD_HOST_RGP -ApplicationGroupName $AVD_APP_GROUP

#Get All AVD Hosts *Use Resource Group where AVD Virtual Machines are*
$AVD_VMS = Get-AzWvdSessionHost -ResourceGroupName $AVD_HOST_VMS_RGP -HostPoolName $AVD_HOSTPOOL

Now you have a PowerShell Object with all the Virtual Machines in your AVD Host Pool.

Tagging the AVD Hosts

#Iterating through each AVD Host in our PowerShell Object
foreach ($VM in $AVD_VMS){

    ###############################################
    #           User Tag                          #
    ###############################################

      #Saving the assigned User to a Variable
      $VMUser = ($VM.AssignedUser)
     
      #Ensuring the Assigned User isn't null (This could happen if host is unassigned)
      if ($null -ne $VMUser){
      
      #Saving Virtual Machine Host Name
      $VMName = ($VM.Name)

      #Trimming off the fluff from the name (My VMS had the domain in the name. I'm removing the uncessary text to get exactly what the name is in Azure Portal.)

      #Replacing 'bad text' with an empty character ''
      $VMName = $VMName.Replace('bad text','')
      
      #Get Azure AD User Object by filtering the UPN for UPNS that match the VM Assigned User
      $ADObject = Get-AzureADUser -Filter "userPrincipalName eq '$VMUser'"

      #Saving the Display Name and Email (Either could be used in the tag)
      $DisplayName = $ADObject.DisplayName
      $UserEmail = $ADObject.Mail

      #Saving the VM Object and saving ResourceID
      $VMObject = (Get-AzResource -ResourceGroupName "ResourceGroupName" -Name $VMName)
      $ResourceIDForVM = $VM.ResourceId
 
      #Storing Tags of VM resource to a variable
      $tags = $VMObject.Tags

      #If the User Tag is missing enter this if statement and add a new User Tag with User Email as the value
      if (!$tags.ContainsKey("User")){
      $emailTag = @{"User"="$UserEmail"}

      #Updating Azure Resource with new tag using Merge Operation
      Update-AzTag -ResourceId $ResourceIDForVM -Tag $emailTag -Operation Merge
      
      }
  }

Conclusion

Now all of your AVD Hosts will have a User Tag with the Assigned User. You can throw this script into Azure Automation with a Service Principal with Contributor access over your Subscription with AVDS and have tags deployed hourly for new Azure Virtual Desktops.

I hope this script helps you keep your Azure Virtual Desktop Pools a little more organized.

-Alex