Adding bulk users to Azure Windows Virtual Desktop using PowerShell

Adding bulk users to Azure Windows Virtual Desktop using PowerShell

Since the release of Azure Windows Virtual Desktop, organizations are finding it a bit difficult to manage the environment. It is because Azure Portal does not provide a management pane that could be used to execute daily operational tasks such as adding users, logging off users, removing users, and so on. There are many other tasks that a Windows Virtual Desktop admin would like to perform every day, such as adding and removing bulk users from the WVD tenant. For example, you might have given temporary permissions to some users in the WVD environment and would like to remove these users on a daily or weekly basis. This is where this article comes handy. In this article, we are going to talk about some examples of using Azure Windows Virtual Desktop PowerShell modules to add a single user or bulk users.

Before you start to use the PowerShell commands explained in this article, install WVD PowerShell modules by executing the command below. Note that you will be required to execute the PowerShell command from an elevated PowerShell prompt.

Install-Module -Name RDInfra.RDPowershell

Once the PowerShell modules have been installed, run Import-Module Microsoft.RDInfra.RDPowershell so all WVD related PowerShell commands can be imported in the current PowerShell session.

You need to connect to your WVD tenant before executing PowerShell commands to add a single user or bulk users by running this command:

Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com"

Use of Add-RdsAppGroupUser PowerShell cmdlet

In this article, we are explaining the use of the Add-RDSAppGroupUser PowerShell cmdlet to add a single user or bulk users. Note that you will be required to provide the inputs below to the Add-RdsAppGroupUser PowerShell cmdlet:

  • Tenant name: The WVD tenant name.
  • Host pool name: The WVD post pool name to which the user will be added.
  • User principal name: The name of the UPN that is present on the WVD tenant.

For example, executing the PowerShell command below will add user [email protected] to the host pool named: TechGenixPool in the WVD tenant named TechGenixWVD

Add-RdsAppGroupUser -TenantName "TechGenixWVD" -HostPoolName "TechGenixPool" -AppGroupName "Desktop Application Group" -UserPrincipalName "[email protected]"

Note that the above command processes the request and does not show the result. Usually, PowerShell cmdlets return information to let users know that a command has been successful, but this is not the case with the Add-RDSAppGroupUser cmdlet. To ensure the user was added to the WVD tenant, executing this PowerShell command will help you get the status:

Get-RdsAppGroupUser -TenantName "TechGenixWVD" -HostPoolName "TechGenixPool" -AppGroupName "Desktop Application Group" -UserPrincipalName "[email protected] "

Adding bulk users to WVD tenant

If you would like to add bulk users to WVD tenant from a CSV file, we can use the same PowerShell commands explained in this article, but make use of ForEach loop. The ForEach loop will help us process all entries in a CSV file. Let’s assume we create a file called UPNs.CSV file. The UPNs.CSV file contains user principal names on each line. If we need to add all users from a CSV file to the WVD tenant, we can use this PowerShell script:

$UPNFile = "C:TempUPNs.CSV"
$ResultFile = "C:TempResult.CSV"
Remove-item $ResultFile -ErrorAction SilentlyContinue
$STR = "User Principal Name, Status"
$CSV = Import-CSV $UPNFile
$TenantName = "TechGenixWVD"
$HostPool = "TechGenixHostPool"
ForEach ($Item in $CSV)
{
$ThisUser = $Item.UPN
Write-Host "Processing User: " $ThisUser
$Error.Clear()
Add-RdsAppGroupUser -TenantName $TenantName -HostPoolName $HostPool -AppGroupName "Desktop Application Group" -UserPrincipalName $ThisUser
IF ($Error.Count -eq 0)
{
$STR = $ThisUser+", Added"
Add-Content $ResultFile $STR
}
else
{
$STR = $ThisUser+", Error Adding User"
Add-Content $ResultFile $STR
}
}
Write-Host "Finished Processing all users..."

Before you execute the PowerShell script, you are required to make these necessary changes:

  • Create a CSV file named C:TempUPNs.CSV and mention all User Principal Names that you wish to add.
  • Change $TenantName and $HostPool variables accordingly.

Once you have executed the PowerShell script, you will see a report generated in the C:Temp folder. The report file name will be Result.CSV. The Result.CSV contains the status for each user, for example, whether the addition was successful or not.

Tip: As you can see, as part of the above script, I am using $Error variable to see if any issues were reported when executing the Add-RDSAppGroupUser command. The error will always report ZERO ($Error.Count -eq 0) if there were no issues in running the command.

The PowerShell script was retrieved from Wintellisys WVD Manager, which can be used to add and remove bulk users easily from within the application as seen in the screenshot below:

The Wintellisys WVD Manager console allows you to add and remove a single user or bulk users. You just need to provide the path to CSV file, which contains the list of users to added. As you can see in the screenshot above, it can also report on users that were not added successfully in the “Failed” section and users that were added successfully in the “Added” section.

Removing users from Azure Windows Virtual Desktop tenant

When it comes to removing the users from the WVD tenant, you can use Remove-RDSAppGroupUser PowerShell cmdlet. For example, to remove a single user from WVD Tenant, executing below PowerShell command will do:

Remove-RdsAppGroupUser -TenantName "TechGenixWVD" -HostPoolName "TechGenixPool" -AppGroupName "Desktop Application Group" -UserPrincipalName "[email protected] "

And if you would like to remove multiple users, you can always make use of the PowerShell script explained to add the users. You will just be required to replace “Add-RDSAppGroupUser” with “Remove-RDSAppGroupUser” PowerShell cmdlet.

Featured image: Shutterstock


Post Views:
27



report this ad

msz991

Leave a Reply

Your email address will not be published. Required fields are marked *