<# .SYNOPSIS Fixes XenDesktop 5.x machine assignment between XD database and vsphere vCenter (usually this fixes BrokerMachines where the powerstate is marked as "unknown" because assignment between the DDC broker machine and the VM on vCenter inventory has been lost) .DESCRIPTION ################################################################################################ # ScriptName: FixXD5VMAssignment.ps1 # Author: Michael Rüefli (www.miru.ch) / @drmiru # Date: 20.05.2012 # Version: 1.0 # # History: # 20.05.2012 / V1.0 / Initial version ################################################################################################ .PARAMETER AutoRestart This switch forces the restart of the broker service on any controller within the XD site Note that PSRemoting has to be enabled on all site DDCs .EXAMPLE FixXD5VMAssignment.ps1 -AutoRestart .NOTES Prerequisites: - XenDesktop 5 Management CMDLETS - vSphere PowerCLI 4.x or higher ################################################################################################ #> param([switch] $AutoRestart) ## General Parameter - to EDIT ############### $vchost = "vCenter.domain.com" #the vCenter Server FQDN $vcuser = "vCenterUser" #The vCenter user account $vcpwd = 'thePassword' #Fill in your vCenter user pw ############################################## trap { # this installs an general simple error handler Write-Host "Error in Script:" $_.Exception.GetType().FullName + $_.Exception.Message continue; # exiting a trap with "continue" says to continue on the next line of the script } Write-Host "-------------------------------------------" -ForegroundColor green Write-Host "FIX DDC / vCenter Hosting assignment Script" -ForegroundColor green Write-Host "-------------------------------------------" -ForegroundColor green $ErrorActionPreference:Continue #Add XDS 5 SnapIn $ctxsnapin = Get-PSSnapin Citrix.Common.Commands -ErrorAction SilentlyContinue If (!$ctxsnapin) { Write-Host "Adding Citrix XD PoSh SnapIn..." Add-PSSnapin Citrix* } #Add VMware SnapIn $vspheresnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue If (!$vspheresnapin) { Write-Host "Adding vSphere PoSh SnapIn..." Add-PSSnapin VMware.VimAutomation.Core } #Connecting to vCenter $vcconnection = Connect-VIServer -Server $vchost -User $vcuser -password $vcpwd If (!$vcconnection) { Write-Warning "Error while connecting to vCenter. Check hostname , username and password in script" } #Collect VMs Write-Host "Collecting VMs...please wait..." $DDCVms = Get-BrokerMachine | ? {$_.Powerstate -eq 'unknown'} Write-Host "Processing with VMs..." Foreach ($DDCobj in $DDCVms) { #Get the VM UUID on vCenter $MachineNameShort = ($DDCobj.MachineName).split('\')[1] $BrokerMachineName = $DDCobj.MachineName $VCVMuuid = (Get-VM $MachineNameShort | %{(Get-View $_.Id).config.uuid}) $DDCVMid = $DDCobj.HostedMachineId #Fixing the Brokermachine ID on DDC database If ($DDCVMid -ne $VCVMuuid) { Write-Host "Fixing HostedMachineID for Machine: $BrokerMachineName" Set-BrokerMachine -MachineName $BrokerMachineName -HostedMachineId "$VCVMuuid" } } #Restart Broker Service on all controllers (requires PSRemoting enabled on all DDCs) If ($AutoRestart) { Write-Host "AutoRestart Switch has been set" Write-Host "Enumerating DDCs" $BrokerControllers = Get-BrokerController | select DNSName Foreach ($objbroker in $BrokerControllers) { $BrokerFQDN = $objbroker.DNSName Write-Host "Restarting Broker Service on Controller: $BrokerFQDN" Enter-PSSession -ComputerName $BrokerFQDN Stop-Service "CitrixBrokerService" Start-Service "CitrixBrokerService" Exit-PSSession } }