Powercli script to assign a dedicated Horizon machine to multiple users (2024)

By Wouter Api, Horizon, PowerCLI, vExpert, vExpertEUC, VMware

Yesterday Robin Stolpe again reached out that he was having issues assigning multiple accounts to the same dedicated machine. He couldn’t get this running with the vmware.hv.helper and looking that with how it is implemented now it will probably never work. I decided to put together some of the functions I have used for ControlUp script based actions and some of my other work to put together the following script (that can be found on Github here.)

[CmdletBinding()]Param( [Parameter(Mandatory=$False, ParameterSetName="separatecredentials", HelpMessage='Enter a username' )] [ValidateNotNullOrEmpty()] [string] $Username, [Parameter(Mandatory=$false, ParameterSetName="separatecredentials", HelpMessage='Domain i.e. loft.lab' )] [string] $Domain, [Parameter(Mandatory=$false, ParameterSetName="separatecredentials", HelpMessage='Password in plain text' )] [string] $Password, [Parameter(Mandatory=$true, HelpMessage='FQDN of the connectionserver' )] [ValidateNotNullOrEmpty()] [string] $ConnectionServerFQDN, [Parameter(Mandatory=$false, ParameterSetName="credsfile", HelpMessage='Path to credentials xml file' )] [ValidateNotNullOrEmpty()] [string] $Credentialfile, [Parameter(Mandatory=$false, HelpMessage='username of the user to logoff (domain\user i.e. loft.lab\user1')] [ValidateNotNullOrEmpty()] [string[]] $TargetUsers, [Parameter(Mandatory=$false, HelpMessage='Name of the desktop pool the machine belongs to')] [string] $TargetPool, [Parameter(Mandatory=$false, HelpMessage='dns name of the machine the user is on i.d. lp-002.loft.lab')] [string] $TargetMachine, [Parameter(Mandatory=$false, HelpMessage='domain for the target users')] [string] $TargetDomain)if($Credentialfile -and ((test-path $Credentialfile) -eq $true)){ try{ write-host "Using credentialsfile" $credentials=Import-Clixml $Credentialfile $username=($credentials.username).split("\")[1] $domain=($credentials.username).split("\")[0] $secpw=$credentials.password $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secpw) $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) } catch{ write-error -Message "Error importing credentials" break }}elseif($Credentials -and ((test-path $credentials) -eq $false)){ write-error "Invalid Path to credentials file" break}elseif($username -and $Domain -and $Password){ write-host "Using separate credentials"}function Get-HVDesktopPool { param ( [parameter(Mandatory = $true, HelpMessage = "Displayname of the Desktop Pool.")] [string]$HVPoolName, [parameter(Mandatory = $true, HelpMessage = "The Horizon View Connection server object.")] [VMware.VimAutomation.HorizonView.Impl.V1.ViewObjectImpl]$HVConnectionServer ) # Try to get the Desktop pools in this pod try { # create the service object first [VMware.Hv.QueryServiceService]$queryService = New-Object VMware.Hv.QueryServiceService # Create the object with the definiton of what to query [VMware.Hv.QueryDefinition]$defn = New-Object VMware.Hv.QueryDefinition # entity type to query $defn.queryEntityType = 'DesktopSummaryView' # Filter on the correct displayname $defn.Filter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='desktopSummaryData.displayName'; 'value' = "$HVPoolname"} # Perform the actual query [array]$queryResults= ($queryService.queryService_create($HVConnectionServer.extensionData, $defn)).results # Remove the query $queryService.QueryService_DeleteAll($HVConnectionServer.extensionData) # Return the results if (!$queryResults){ write-host "Can't find $HVPoolName, exiting." exit } else { return $queryResults } } catch { write-host 'There was a problem retreiving the Horizon View Desktop Pool.' }}function Get-HVDesktopMachine { param ( [parameter(Mandatory = $true, HelpMessage = "ID of the Desktop Pool.")] [VMware.Hv.DesktopId]$HVPoolID, [parameter(Mandatory = $true, HelpMessage = "Name of the Desktop machine.")] [string]$HVMachineName, [parameter(Mandatory = $true, HelpMessage = "The Horizon View Connection server object.")] [VMware.VimAutomation.HorizonView.Impl.V1.ViewObjectImpl]$HVConnectionServer ) try { # create the service object first [VMware.Hv.QueryServiceService]$queryService = New-Object VMware.Hv.QueryServiceService # Create the object with the definiton of what to query [VMware.Hv.QueryDefinition]$defn = New-Object VMware.Hv.QueryDefinition # entity type to query $defn.queryEntityType = 'MachineDetailsView' # Filter so we get the correct machine in the correct pool $poolfilter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='desktopData.id'; 'value' = $HVPoolID} $machinefilter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='data.name'; 'value' = "$HVMachineName"} $filterlist = @() $filterlist += $poolfilter $filterlist += $machinefilter $filterAnd = New-Object VMware.Hv.QueryFilterAnd $filterAnd.Filters = $filterlist $defn.Filter = $filterAnd # Perform the actual query [array]$queryResults= ($queryService.queryService_create($HVConnectionServer.extensionData, $defn)).results # Remove the query $queryService.QueryService_DeleteAll($HVConnectionServer.extensionData) # Return the results if (!$queryResults){ write-host "Can't find $HVPoolName, exiting." exit } else{ return $queryResults } } catch { write-host 'There was a problem retreiving the Horizon View Desktop Pool.' }}function Get-HVUser { param ( [parameter(Mandatory = $true, HelpMessage = "User loginname..")] [string]$HVUserLoginName, [parameter(Mandatory = $true, HelpMessage = "Name of the Domain.")] [string]$HVDomain, [parameter(Mandatory = $true, HelpMessage = "The Horizon View Connection server object.")] [VMware.VimAutomation.HorizonView.Impl.V1.ViewObjectImpl]$HVConnectionServer ) try { # create the service object first [VMware.Hv.QueryServiceService]$queryService = New-Object VMware.Hv.QueryServiceService # Create the object with the definiton of what to query [VMware.Hv.QueryDefinition]$defn = New-Object VMware.Hv.QueryDefinition # entity type to query $defn.queryEntityType = 'ADUserOrGroupSummaryView' # Filter to get the correct user $userloginnamefilter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='base.loginName'; 'value' = $HVUserLoginName} $domainfilter = New-Object VMware.Hv.QueryFilterEquals -property @{'memberName'='base.domain'; 'value' = "$HVDomain"} $filterlist = @() $filterlist += $userloginnamefilter $filterlist += $domainfilter $filterAnd = New-Object VMware.Hv.QueryFilterAnd $filterAnd.Filters = $filterlist $defn.Filter = $filterAnd # Perform the actual query [array]$queryResults= ($queryService.queryService_create($HVConnectionServer.extensionData, $defn)).results # Remove the query $queryService.QueryService_DeleteAll($HVConnectionServer.extensionData) # Return the results if (!$queryResults){ write-host "Can't find user $HVUserLoginName in domain $HVDomain, exiting." exit } else { return $queryResults } } catch { write-host 'There was a problem retreiving the user.' }}$hvserver1=connect-hvserver $ConnectionServerFQDN -user $username -domain $domain -password $password$Services1= $hvServer1.ExtensionData$desktop_pool=Get-HVDesktopPool -hvpoolname $TargetPool -HVConnectionServer $hvserver1$poolid=$desktop_pool.id$machine = get-hvdesktopmachine -HVConnectionServer $hvserver1 -HVMachineName $TargetMachine -HVPoolID $poolid$machineid = $machine.id$useridlist=@()foreach ($targetuser in $TargetUsers){ $user = Get-HVUser -HVConnectionServer $hvserver1 -hvdomain $TargetDomain -HVUserLoginName $targetUser $useridlist+=$user.id}$Services1.Machine.Machine_assignUsers($machineid, $useridlist)

So first I have 3 functions to get the Pool, the machine and users. With a foreach on the $Targetusers list I create a list of the userid’s that is required to use for the Machine_assignUsers function of the machine service.

Tagged api, horizon, powercli, SOAP, VMware.

Bookmark the permalink.

Comments are closed.

Powercli script to assign a dedicated Horizon machine to multiple users (2024)
Top Articles
Winston-Salem Journal from Winston-Salem, North Carolina
Jackson County, NC News | NewsBreak Jackson County, NC
Byrn Funeral Home Mayfield Kentucky Obituaries
Puretalkusa.com/Amac
craigslist: south coast jobs, apartments, for sale, services, community, and events
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Moe Gangat Age
Craigslist Boats For Sale Seattle
Lax Arrivals Volaris
Hoe kom ik bij mijn medische gegevens van de huisarts? - HKN Huisartsen
Playgirl Magazine Cover Template Free
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
6813472639
Uc Santa Cruz Events
50 Shades Darker Movie 123Movies
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Barber Gym Quantico Hours
PCM.daily - Discussion Forum: Classique du Grand Duché
Apartments / Housing For Rent near Lake Placid, FL - craigslist
The Banshees Of Inisherin Showtimes Near Broadway Metro
Speedstepper
Miles City Montana Craigslist
Firefly Festival Logan Iowa
Weather October 15
Craftybase Coupon
Login.castlebranch.com
Guinness World Record For Longest Imessage
Nikki Catsouras: The Tragic Story Behind The Face And Body Images
Dairy Queen Lobby Hours
Redbox Walmart Near Me
How to Use Craigslist (with Pictures) - wikiHow
RFK Jr., in Glendale, says he's under investigation for 'collecting a whale specimen'
Ixl Lausd Northwest
Nsu Occupational Therapy Prerequisites
Scanning the Airwaves
KM to M (Kilometer to Meter) Converter, 1 km is 1000 m
Elizaveta Viktorovna Bout
Busted Newspaper Campbell County KY Arrests
World Social Protection Report 2024-26: Universal social protection for climate action and a just transition
Subdomain Finder
VDJdb in 2019: database extension, new analysis infrastructure and a T-cell receptor motif compendium
Promo Code Blackout Bingo 2023
Expendables 4 Showtimes Near Malco Tupelo Commons Cinema Grill
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
About Us
Darkglass Electronics The Exponent 500 Test
Greg Steube Height
The Quiet Girl Showtimes Near Landmark Plaza Frontenac
Craigslist Anc Ak
Bones And All Showtimes Near Emagine Canton
Asisn Massage Near Me
Die 10 wichtigsten Sehenswürdigkeiten in NYC, die Sie kennen sollten
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6307

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.