AzureVDIAddUser

Published on 4 June 2021 at 11:48

Reason

The IT manager requested to add a user to the VDI group so they could connect with the Azure WVD.

Found a guide to give access through powershell so I made the following script.
When you log in you should get a table to choose the correct settings and find the account to give access.

some information was altered to hide company info.

Please use parts of this script with caution, I am not responsible for any damage.

 

Script

#Get Azure admin credentials
Write-Host "Getting Azure credentials... "
$Credentials = Get-Credential -credential <O365 admin account>
#logfile
$date = (Get-Date).tostring("yyyyMMdd")
$file = "\\<servername>\Software\General\Log$\VDI$\$env:username $date.log"
echo "Script started, credentials requested (O365 admin)" > $file
#[5]adduserfunction
function adduser{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to add a user to the VDI group?", 0,"Add User to VDI",4)
If ($intAnswer -eq 6) {
$User = (Get-ADUser -Filter * -SearchBase "OU=Users,OU=<companyname>,DC=<companyname>,DC=local") | Out-GridView -Title "Please select the user" -OutputMode Single -ErrorAction Stop
try {
Add-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" -UserPrincipalName $User.UserPrincipalName
#Display current RDS users in the selected hostpool
Write-Host "Getting WVD users information for $RDSHostPoolName in $RDSTenantName..."
$RDSUsers = Get-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" | Select UserPrincipalName
Write-Host $RDSUsers.UserPrincipalName
}
Catch {
Write-Host "Failed to add user to the hostpool." -ForegroundColor Red
echo "[5]Could not add user to the VDI group" >> $file
$User=""
Break
}
Write-Host "Succesfully added the user $User to hostpool $RDSHostPoolName" -ForegroundColor Green
echo "[5]User $User added to the VDI group" >> $file
$User=""
adduser
} else {
echo "[5]Clicked no to add user" >> $file
}
}
#[6]removeuserfunction
function removeuser{
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to remove a user from the VDI group?", 0,"Remove User from VDI",4)
If ($intAnswer -eq 6) {
$User = Get-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" | Select UserPrincipalName | Out-GridView -Title "Please select the user" -OutputMode Single -ErrorAction Stop
try {
Remove-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" -UserPrincipalName $User.UserPrincipalName
#Display current RDS users in the selected hostpool
Write-Host "Getting WVD users information for $RDSHostPoolName in $RDSTenantName..."
$RDSUsers = Get-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" | Select UserPrincipalName
Write-Host $RDSUsers.UserPrincipalName
}
Catch {
Write-Host "Failed to remove user from the hostpool." -ForegroundColor Red
echo "[6]Could not remove user $User from the VDI group" >> $file
$User=""
Break
}
Write-Host "Succesfully removed $UserToRemove from hostpool $RDSHostPoolName" -ForegroundColor Green
echo "[6]User $User removed from the VDI group" >> $file
$User=""
removeuser
} else {
echo "[6]Clicked no to remove user" >> $file
}
}
#[1]Add RDS Account in order to be able to change WVD configuration
$BrokerURL = "https://rdbroker.wvd.microsoft.com"
Write-Host "Adding the RDS account... " -NoNewline
Try {
Add-RdsAccount -DeploymentUrl $BrokerURL -Credential $Credentials -ErrorAction Stop | Out-Null
echo "[1]Added RDS account" >> $file
}
Catch {
Write-Host "Failed to add the RDS account. Aborting." -ForegroundColor Red
echo "[1]Failed to add RDS account, check credentials" >> $file
Break
}
Write-Host "Done." -ForegroundColor Green
#[2]Select WVD tenant
Write-Host "Getting WVD tenants information... " -NoNewline
Try {
$RDSTenantName = (Get-RdsTenant).TenantName | Out-GridView -Title "Please select one WVD tenant from the list above" -OutputMode Single -ErrorAction Stop
echo "[2]Choosing Tenant: $RDSTenantName" >> $file
}
Catch {
Write-Host "Failed to get WVD tenant information. Aborting." -ForegroundColor Red
echo "[2]Failed to get Tenant" >> $file
Break
}
Write-Host "Done." -ForegroundColor Green
#[3]Select WVD hostpool within the tenant
Write-Host "Getting WVD hostpools information... " -NoNewline
Try {
$RDSHostPoolName = (Get-RdsHostPool -TenantName $RDSTenantName).HostPoolName | Out-GridView -Title "Please select one hostpool from the list above" -OutputMode Single -ErrorAction Stop
echo "[3]Choosing Hostpool: $RDSHostPoolName" >> $file
}
Catch {
Write-Host "Failed to get WVD hostpools information. Aborting." -ForegroundColor Red
echo "[3]Failed to get Hostpool" >> $file
Break
}
Write-Host "Done." -ForegroundColor Green
#[4]Display current RDS users in the selected hostpool
Write-Host "Getting WVD users information for $RDSHostPoolName in $RDSTenantName..."
$RDSUsers = Get-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" | Select UserPrincipalName
echo "[4]Show Usergroup: " >> $file
echo $RDSUsers.UserPrincipalName >> $file
Write-Host $RDSUsers.UserPrincipalName
#Add additional RDS users
Write-Host "###################################" -ForegroundColor Yellow
#Adduser function
adduser
#Removeuser function
removeuser
#show final group
$RDSUsers = Get-RdsAppGroupUser -TenantName $RDSTenantName -HostPoolName $RDSHostPoolName -AppGroupName "Desktop Application Group" | Select UserPrincipalName
"Final group: " >> $file
echo $RDSUsers.UserPrincipalName >> $file
echo "------------End of Script------------" >> $file
Write-Host 'Thank you for using my script!';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');

Add comment

Comments

There are no comments yet.