Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Horizon Cloud DAAS REST API - VM to User mapping PowerShell Script
<#
.Synopsis
VM to User mapping Powershell Script
.Details required to run the script
1) Enter the Portal URL for $tenanturl
2) Enter the Domain Name for $Domain
3) Change the Path of CSV file in $csvfile
4) Enter Credentials information in $ADCredentials section which need below inputs
SuperAdmin User , SuperAdmin Password & Domain
5) Create a csv file with two columns ,
Enter the Names of colums as VDIName , User
Under VDIName column mention the Newly provisioned VM Names that needs to assigned
Under User column metion the user ID's
6) You will get a prompt to input pool id of the vm's
.Limitations
1) This script can run against one pool at a time
hence the csv file should contain the VDINames from one pool only
if you need this for multiple pools then create multiple csv files and run one at a time
2) This script is for static pool assignments only, not to be used for floating since floating pools we will assign the group
#>
#input the PORTAL URL & Domain
$tenanturl= "xxx.horizon.vmware.com"
$domain = "xxx"
$csvfile = Import-Csv "C:\temp\usermappings.csv"
$taaddress = "https://" + $tenanturl + "/"
$dtlogin = "dt-rest/v100/system/authenticate/credentials"
$auth = @()
$ADCredentials = @()
#provide AD Creadentials Plain text
$ADCredentials ='
{
"type":"CREDENTIALS",
"username":"SuperAdminUser",
"password":"SuperAdminUserPassword",
"domain":"Domain",
"usernameModifiable":false
}
'
$taauthadderessuri= $taaddress+$dtlogin
try
{
#Create Initial auth request and this will used run further api calls
$auth = Invoke-WebRequest –Uri $taauthadderessuri –Method Post -Headers @{"Content-Type" ="application/json"} –Body $ADCredentials –ErrorAction stop
}
catch
{
Write-Host -ForegroundColor Red -BackgroundColor Black "Error with Active Directory login, please check credentials."
return
}
#Get the auth headers for future API calls
if($auth.headers.Authorization)
{
Write-Host -ForegroundColor Green "Active Directory login successful."
$loginauth = $auth.headers.Authorization
$csrfheader = $auth.headers.'x-dt-csrf-header'
}
#Pull Assignments information
$poolsuri = ($taaddress + "dt-rest/v100/pool/manager/pools")
$poolsinfojson = Invoke-RestMethod -Uri $poolsuri -Method Get -Headers @{"Accept"="application/json";"Authorization"="$loginauth"}
foreach ($poolinfo in $poolsinfojson)
{
foreach ($pooldetails in $poolinfo) { foreach ($poolformat in $pooldetails) {write-host -ForegroundColor Cyan $poolformat.id --> $poolformat.name}}
}
write-host -ForegroundColor Cyan "======================================================================================"
#Enter the pool id in which we have to proceed with user mappings
$pool_id= Read-Host "Enter the pool id"
write-host -ForegroundColor Cyan "======================================================================================"
#import the csv file and run one mapping at time
$csvfile | ForEach-Object -process {
$computer=($_.VDIName)
$username=($_.User)
Write-Host -ForegroundColor Yellow "$computer --> $username"
$checkpatternjson = @()
#We need the patternid for assigning the user
$patterninfouri=($taaddress + "dt-rest/v100/infrastructure/pool/desktop/" + $pool_id + "/vms?name=" + $computer)
$useriduri=($taaddress + "dt-rest/v100/security/domain/pilot/user?name=" + $username)
#find the pattern id for the VM
$Vminfojson=Invoke-RestMethod -Uri $patterninfouri -Method Get -Headers @{"Accept"="application/json";"Authorization"="$loginauth"}
$patternid= foreach ($info in $Vminfojson) {if ($info.name -eq $computer) { $info.patternid}}
#find user id
$useridjson=Invoke-RestMethod -Uri $useriduri -Method Get -Headers @{"Accept"="application/json";"Authorization"="$loginauth"}
$userid=$useridjson.id
write-host -ForegroundColor Yellow "patternid & userid values are as below"
Write-Host -ForegroundColor White "Patternid of $computer --> $patternid"
Write-Host -ForegroundColor White "UserID of $username --> $userid"
try
{
#check user has any patterns i.e desktops already assigned
$checkpatternuri = ($taaddress + "dt-rest/v100/security/domain/" + $domain + "/user/" + $userid + "/patterns")
$checkpatternjson = Invoke-RestMethod -Uri $checkpatternuri -Method Get -Headers @{"Accept"="application/json";"Authorization"="$loginauth"}
Write-Host $checkpatternjson
#assign only when he/she not having any desktops assigned on the pool id that was mentioned
if ($checkpatternjson.desktopPoolId -notcontains $pool_id -or $checkpatternjson.id -eq $null)
{
#Assign the pattern to the user
$assignuri=($taaddress + "dt-rest/v100/infrastructure/pattern/static/" + $patternid + "/assign/user/" + $userid)
Invoke-RestMethod -Uri $assignuri -Method PUT -Headers @{"Accept"="application/json";"Authorization"="$loginauth";"x-dt-csrf-header"="$csrfheader"}
write-host -ForegroundColor Green "$username assignment to $computer Succeded"
}
else
{
$assignedvmname= $checkpatternjson.name
Write-Host -ForegroundColor DarkYellow "$username already have an assignment to $assignedvmname in the pool $pool_id"
}
}
catch {
write-host -ForegroundColor RED "$username assignment to $computer failed, possibly user name doesn't exist in the AD"
}
write-host -ForegroundColor Cyan "======================================================================================"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment