VMware Cloud Community
sayaho
VMware Employee
VMware Employee

Rebuild a virtual machine in a full-clone desktop pool

I'd like to know if it is possible to create a script with PowerCLI to rebuild a virtual machine in a full-clone desktop pool, as in the following document:
https://docs.vmware.com/en/VMware-Horizon-7/7.13/horizon-virtual-desktops/GUID-7B6737E8-DD82-482F-BF...

Thanks.

Reply
0 Kudos
5 Replies
Magneet
Hot Shot
Hot Shot

That is certainly possible, first perform a query to get the machine or machine's that you need to rebuild and after that use the machine.machine(rebuild(machineid) api method

https://vdc-download.vmware.com/vmwb-repository/dcr-public/a2b06b81-997f-45cb-a52c-39c44f7a819e/010b...

f there are more machines use machine.machine_rebuildmachines([machineids]) make sure it's an array if the machineid's only.

https://vdc-download.vmware.com/vmwb-repository/dcr-public/a2b06b81-997f-45cb-a52c-39c44f7a819e/010b...

to build the query use this on how to build it:

https://www.retouw.nl/2018/10/19/horizon-view-apis-back-to-basics-part-2-queries/

and for the

after that use either of the methods to rebuild the machine(s)

sayaho
VMware Employee
VMware Employee

Thank you very much. I have learned a lot from the sites you have provided.
Is it possible to use Hv.Helper to implement the Horizon View API in an environment where it is not subscribed?

Reply
0 Kudos
Magneet
Hot Shot
Hot Shot

you can copy the vmware.hv.helper to any machine that has powercli installed and use it. 

Reply
0 Kudos
sayaho
VMware Employee
VMware Employee

Thank you. I'll try it.

Reply
0 Kudos
MaorZoh
Contributor
Contributor

There is a simple way to achieve this:

# Reading the machine list:
$machinelist = gc "C:\Temp\RebuildTest.txt"
 
$HVUsername = "hv_user"
$HVPassword = 'hv_password'
$HVServer = 'server_fqdn'
 
# Connect to VMware Horizon server:
Write-Host "Connecting to $HVServer Horizon server...`r`n" -ForegroundColor Cyan
$HV = Connect-HVServer -Server $HVServer -User $HVUsername -Password $HVPassword -Domain ntnet
 
$Services1 = $HV.ExtensionData
$domain = "domain.name.com"
 
# For each machine on the list - get its name and id, rebuild machine and re-assign the user on the computer it belongs to:
foreach ($machine in $machinelist)
{
    $machinename = Get-HVMachineSummary | where {$_.Base.Name -eq "$machine"}
    $id = $machinename.Id
 
    Write-Host "Rebuilding $machine..." -ForegroundColor Green
    $Services1.Machine.Machine_Rebuild($id)
 
    Write-Host "Waiting 90 seconds..." -ForegroundColor Green
    Start-Sleep -Seconds 90
    
    # On this case, the machine consists of the username and a 3 characters suffix, so to get the user, we will remove the last 3 characters from the string. of course, you can get the username in multiple other ways:
    $username = $machine.Substring(0, $machine.Length - 3)
    Write-Host "Re-assigning $username to $machine machine..." -ForegroundColor Green
    Get-HVMachine -MachineName $machine | Set-HVMachine -User "$username@$domain"
    Write-Host "$username was assigned to $machine" -ForegroundColor Green
}
 
# Disconnect from EMEA VMware Horizon server
$HV = Disconnect-HVServer -Confirm:$false -Force
Write-Host "Disconnected from $HVServer Horizon server." -ForegroundColor Gray
Reply
0 Kudos