Adding RDS Assigned Virtual Desktop User to SCVMM 1


When working with RDS VDI scenarios using VMM, it may be helpful to have a view of which users are assigned to persistent desktops.  Using the extensible power of Virtual Machine manager, you can easily provide this information to all of your virtual machines.  The script below can be run on a schedule to keep this data in sync or as part of an automation workflow.

First, you must prepare the property you will use.

Import-Module VirtualMachineManager

New-SCCustomProperty -Name "RDUser" -AddMember @("VM")

Once this has been done, schedule/run the following script manually

Import-Module RemoteDesktop,VirtualMachineManager
$broker= "RDBroker.Contoso.com"
$VMMServer="SCVMM.Contoso.com"
$Deployment = Get-RDRemoteDesktop -connectionBroker $broker
$RDUserProperty=Get-SCCustomProperty -Name "RDUser"  #We need an object that represents the property "RDUser"

#Get the RD Persistent Assignments
$RDPVDA= Get-RDPersonalVirtualDesktopAssignment -collectionName $Deployment.CollectionName -connectionBroker $broker

foreach ($entry in $RDPVDA)
{
#First we get the VM we want to work with, set variable to null prior to starting work to avoid working on old resources
$vm = $null
$vm = get-VM $entry.VirtualDesktopName -VMMServer $VMMServer
#Now we set it!
Set-SCCustomPropertyValue -inputobject $vm -customproperty $RDUserProperty -value $entry.user
}

 

Once completed, you’ll be able to customize your view to see this property (right click the column headers to add another)

VMM-CustomProperty

 

This can also be done for POOLED desktops, but this version does not remove users once they’ve logged off.  More tinkering is needed to fix that.  One method is to blank all settings in the VMs, but that requires significant filtering.  The downside as well is it generates a lot of jobs, and despite the documentation, -JobGroupID is not supported on the Set-SCCustomPropertyValue cmdlet.

 

Import-Module RemoteDesktop,VirtualMachineManager
$broker= "RDBroker.Contoso.com"
$VMMServer="SCVMM.Contoso.com"
$Deployment = Get-RDRemoteDesktop -connectionBroker $broker
$RDUserProperty=Get-SCCustomProperty -Name "RDUser"  #We need an object that represents the property "RDUser"

#Get the RD Persistent Assignments
$RDPVDA= Get-RDPersonalVirtualDesktopAssignment -collectionName $Deployment.CollectionName -connectionBroker $broker

foreach ($entry in $RDPVDA)
{
#First we get the VM we want to work with, set variable to null prior to starting work to avoid working on old resources
$vm = $null
$vm = get-VM $entry.VirtualDesktopName -VMMServer $VMMServer
#Now we set it!
Set-SCCustomPropertyValue -inputobject $vm -customproperty $RDUserProperty -value $entry.user
}

#Now we move on to pooled
#Get sessions, Filter out Shared sessions, only getting pooled VDI
$Sessions=Get-RDUserSession -connectionBroker $Broker | Where-object {$_.CollectionType -eq "RD_FARM_TEMP_VM" }
foreach ($session in $sessions)
{
$vm = $null
$VMBaseName=$null
#Split the name into a usable string
$VMBaseName=$session.servername.Split(".")[0]
$vm=Get-VM $VMBaseName -VMMServer $VMMServer
#Now we set it!
Set-SCCustomPropertyValue -inputobject $vm -customproperty $RDUserProperty -value $Session.UserName
}

Leave a comment

Your email address will not be published.

One thought on “Adding RDS Assigned Virtual Desktop User to SCVMM