##################################################################### # Scriptname: FixDDCVCHosting.ps1 # Description: Script to fix DDC / vCenter Pool assignments # Author: Michael Rueefli, INSERTO AG # Version: 1.0 # Requirements: VMWare PowerCLI Snap-in, XenDesktop 4 SDK Snap-in # History: ##################################################################### ## General Parameter - to EDIT ############### $vchost = "vchost.domain.com" #the vCenter Server FQDN $vcuser = "vcuser" #The vCenter user account $vcpwd = '' #Fill in your vCenter user pw if you like $vmfilter = 'WinXPVM0' #Include Filter for VM names $clusterfilter = "VDI-Cluster1" #Include filter for ESX Cluster Name ############################################## 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 4 SnapIn Add-PSSnapin XDCommands #Add VMware SnapIn Add-PSSnapin VMware* #Connecting to vCenter Connect-VIServer -Server $vchost -User $vcuser -password $vcpwd #Collect VMs Write-Host "Collecting VMs...please wait..." $VCVms = Get-Cluster | ? {$_.Name -match "$clusterfilter"} | get-vm | ? {$_.Name -match $vmfilter} $DDCVms = Get-XdVirtualDesktop Write-Host "Processing with VMs..." Foreach ($vcobj in $VCVms) { $guestosid = ($vcobj | Get-Annotation -CustomAttribute CTXGuestOsId).Value If (!$guestosid) { $vcobjname = $vcobj.Name $DDCvmobj = $DDCVms | ? {$_.Name -match "$vcobjname"} $DDCvmobjName = $DDCvmobj.Name $DDCvmobjSID = (new-object system.security.principal.NtAccount($DDCvmobjName)).translate([system.security.principal.securityidentifier]) Write-Host "Fixing CTXGuestOsID for VM: $vcobjname" If ($DDCvmobjSID) { Write-Host "Setting CTXGuestOSId to: $DDCvmobjSID for machine: $vcobjname" $vcobj | Set-Annotation -CustomAttribute CTXGuestOsId -value $DDCvmobjSID.Value } Else { write-warning "Could not resolve machine SID for: $DDCvmobjName" } } Elseif ($guestosid) { $DDCvmobjSID = (new-object system.security.principal.NtAccount($DDCvmobjName)).translate([system.security.principal.securityidentifier]) If ($DDCvmobjSID -ne $guestosid) { Write-Host "Fixing CTXGuestOSId to: $DDCvmobjSID for machine: $vcobjname" $vcobj | Set-Annotation -CustomAttribute CTXGuestOsId -value $DDCvmobjSID.Value } } } Get-Service ctxpoolmgr -ea Continue | Stop-Service Get-Service ctxpoolmgr -ea Continue | Start-Service