Automation

 View Only
  • 1.  Rebuild a virtual machine in a full-clone desktop pool

    Broadcom Employee
    Posted Jan 15, 2021 03:46 AM

    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-BF8E-C204FBE709FA.html

    Thanks.



  • 2.  RE: Rebuild a virtual machine in a full-clone desktop pool

    Posted Jan 15, 2021 07:00 AM

    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/010bcd60-990e-494e-ade7-c65fe01a82b0/vdi.resources.Machine.html#rebuild

    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/010bcd60-990e-494e-ade7-c65fe01a82b0/vdi.resources.Machine.html#rebuildMachines

    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)



  • 3.  RE: Rebuild a virtual machine in a full-clone desktop pool

    Broadcom Employee
    Posted Jan 16, 2021 04:21 AM

    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?



  • 4.  RE: Rebuild a virtual machine in a full-clone desktop pool

    Posted Jan 16, 2021 09:29 PM

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



  • 5.  RE: Rebuild a virtual machine in a full-clone desktop pool

    Broadcom Employee
    Posted Jan 17, 2021 04:18 PM

    Thank you. I'll try it.



  • 6.  RE: Rebuild a virtual machine in a full-clone desktop pool

    Posted Jan 15, 2024 11:09 AM

    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