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