Advertisement
henrydenhengst

create a XenDesktop 7.x machine catalog

May 26th, 2015
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Author: Aaron Parker
  3. # Desc:   Using PowerShell to create a XenDesktop 7.x machine catalog
  4. # Date:   Aug 19, 2014
  5. # Site:   http://stealthpuppy.com
  6. #
  7.  
  8. # Set variables for the target infrastructure
  9. # -
  10. $adminAddress = 'xd71.home.stealthpuppy.com' #The XD Controller we're going to execute against
  11. $xdControllers = 'xd71.home.stealthpuppy.com'
  12.  
  13. # Hypervisor and storage resources
  14. # These need to be configured in Studio prior to running this script
  15. # This script is hypervisor and management agnostic - just point to the right infrastructure
  16. $storageResource = "HV1-LocalStorage" #Storage
  17. $hostResource = "Lab SCVMM" #Hypervisor management
  18.  
  19. # Machine catalog properties
  20. $machineCatalogName = "Windows 8 x86"
  21. $machineCatalogDesc = "Windows 8.1 x86 SP1 with Office 2013"
  22. $domain = "home.stealthpuppy.com"
  23. $orgUnit = "OU=MCS Pooled,OU=Workstations,DC=home,DC=stealthpuppy,DC=com"
  24. $namingScheme = "W8-MCS-###" #AD machine account naming conventions
  25. $namingSchemeType = "Numeric" #Also: Alphabetic
  26. $allocType = "Random" #Also: Static
  27. $persistChanges = "Discard" #Also: OnLocal, OnPvD
  28. $provType = "MCS" #Also: Manual, PVS
  29. $sessionSupport = "SingleSession" #Also: MultiSession
  30. $masterImage ="WIN81*"
  31. $vCPUs = 2
  32. $VRAM = 2048
  33. # -
  34.  
  35. # Change to SilentlyContinue to avoid verbose output
  36. $VerbosePreference = "Continue"
  37.  
  38. # Load the Citrix PowerShell modules
  39. Write-Verbose "Loading Citrix XenDesktop modules."
  40. Add-PSSnapin Citrix*
  41.  
  42. # Get information from the hosting environment via the XD Controller
  43. # Get the storage resource
  44. Write-Verbose "Gathering storage and hypervisor connections from the XenDesktop infrastructure."
  45. $hostingUnit = Get-ChildItem -AdminAddress $adminAddress "XDHyp:\HostingUnits" | Where-Object { $_.PSChildName -like $storageResource } | Select-Object PSChildName, PsPath
  46. # Get the hypervisor management resources
  47. $hostConnection = Get-ChildItem -AdminAddress $adminAddress "XDHyp:\Connections" | Where-Object { $_.PSChildName -like $hostResource }
  48. $brokerHypConnection = Get-BrokerHypervisorConnection -AdminAddress $adminAddress -HypHypervisorConnectionUid $hostConnection.HypervisorConnectionUid
  49. $brokerServiceGroup = Get-ConfigServiceGroup -AdminAddress $adminAddress -ServiceType 'Broker' -MaxRecordCount 2147483647
  50.  
  51. # Create a Machine Catalog. In this case a catalog with randomly assigned desktops
  52. Write-Verbose "Creating machine catalog. Name: $machineCatalogName; Description: $machineCatalogDesc; Allocation: $allocType"
  53. $brokerCatalog = New-BrokerCatalog -AdminAddress $adminAddress -AllocationType $allocType -Description $machineCatalogDesc -Name $machineCatalogName -PersistUserChanges $persistChanges -ProvisioningType $provType -SessionSupport $sessionSupport
  54. # The identity pool is used to store AD machine accounts
  55. Write-Verbose "Creating a new identity pool for machine accounts."
  56. $identPool = New-AcctIdentityPool -AdminAddress $adminAddress -Domain $domain -IdentityPoolName $machineCatalogName -NamingScheme $namingScheme -NamingSchemeType $namingSchemeType -OU $orgUnit
  57.  
  58. # Creates/Updates metadata key-value pairs for the catalog (no idea why).
  59. Write-Verbose "Retrieving the newly created machine catalog."
  60. $catalogUid = Get-BrokerCatalog | Where-Object { $_.Name -eq $machineCatalogName } | Select-Object Uid
  61. $guid = [guid]::NewGuid()
  62. Write-Verbose "Updating metadata key-value pairs for the catalog."
  63. Set-BrokerCatalogMetadata -AdminAddress $adminAddress -CatalogId $catalogUid.Uid -Name 'Citrix_DesktopStudio_IdentityPoolUid' -Value $guid
  64.  
  65. # Check to see whether a provisioning scheme is already available
  66. Write-Verbose "Checking whether the provisioning scheme name is unused."
  67. If (Test-ProvSchemeNameAvailable -AdminAddress $adminAddress -ProvisioningSchemeName @($machineCatalogName))
  68. {
  69.   Write-Verbose "Success."
  70.  
  71.   # Get the master VM image from the same storage resource we're going to deploy to. Could pull this from another storage resource available to the host
  72.   Write-Verbose "Getting the master image details for the new catalog: $masterImage"
  73.   $VM = Get-ChildItem -AdminAddress $adminAddress "XDHyp:\HostingUnits\$storageResource" | Where-Object { $_.ObjectType -eq "VM" -and $_.PSChildName -like $masterImage }
  74.   # Get the snapshot details. This code will assume a single snapshot exists - could add additional checking to grab last snapshot or check for no snapshots.
  75.   $VMDetails = Get-ChildItem -AdminAddress $adminAddress $VM.FullPath
  76.  
  77.   # Create a new provisioning scheme - the configuration of VMs to deploy. This will copy the master image to the target datastore.
  78.   Write-Verbose "Creating new provisioning scheme using $VMDetails.FullPath"
  79.   # Provision VMs based on the selected snapshot.
  80.   $provTaskId = New-ProvScheme -AdminAddress $adminAddress -ProvisioningSchemeName $machineCatalogName -HostingUnitName $storageResource -MasterImageVM $VMDetails.FullPath -CleanOnBoot -IdentityPoolName $identPool.IdentityPoolName -VMCpuCount $vCPUs -VMMemoryMB $vRAM -RunAsynchronously
  81.   $provTask = Get-ProvTask -AdminAddress $adminAddress -TaskId $provTaskId
  82.  
  83.   # Track the progress of copying the master image
  84.   Write-Verbose "Tracking progress of provisioning scheme creation task."
  85.   $totalPercent = 0
  86.   While ( $provTask.Active -eq $True ) {
  87.     Try { $totalPercent = If ( $provTask.TaskProgress ) { $provTask.TaskProgress } Else {0} } Catch { }
  88.  
  89.     Write-Progress -Activity "Creating Provisioning Scheme (copying and composing master image):" -Status "$totalPercent% Complete:" -percentcomplete $totalPercent
  90.     Sleep 15
  91.     $provTask = Get-ProvTask -AdminAddress $adminAddress -TaskID $provTaskId
  92.   }
  93.  
  94.   # If provisioning task fails, there's no point in continuing further.
  95.   If ( $provTask.WorkflowStatus -eq "Completed" )
  96.   {
  97.       # Apply the provisioning scheme to the machine catalog
  98.       Write-Verbose "Binding provisioning scheme to the new machine catalog"
  99.       $provScheme = Get-ProvScheme | Where-Object { $_.ProvisioningSchemeName -eq $machineCatalogName }
  100.       Set-BrokerCatalog -AdminAddress $adminAddress -Name $provScheme.ProvisioningSchemeName -ProvisioningSchemeId $provScheme.ProvisioningSchemeUid
  101.  
  102.       # Associate a specific set of controllers to the provisioning scheme. This steps appears to be optional.
  103.       Write-Verbose "Associating controllers $xdControllers to the provisioning scheme."
  104.       Add-ProvSchemeControllerAddress -AdminAddress $adminAddress -ControllerAddress @($xdControllers) -ProvisioningSchemeName $provScheme.ProvisioningSchemeName
  105.  
  106.       # Provisiong the actual machines and map them to AD accounts, track the progress while this is happening
  107.       Write-Verbose "Creating the machine accounts in AD."
  108.       $adAccounts = New-AcctADAccount -AdminAddress $adminAddress -Count 5 -IdentityPoolUid $identPool.IdentityPoolUid
  109.       Write-Verbose "Creating the virtual machines."
  110.       $provTaskId = New-ProvVM -AdminAddress $adminAddress -ADAccountName @($adAccounts.SuccessfulAccounts) -ProvisioningSchemeName $provScheme.ProvisioningSchemeName -RunAsynchronously
  111.       $provTask = Get-ProvTask -AdminAddress $adminAddress -TaskId $provTaskId
  112.  
  113.       Write-Verbose "Tracking progress of the machine creation task."
  114.       $totalPercent = 0
  115.       While ( $provTask.Active -eq $True ) {
  116.         Try { $totalPercent = If ( $provTask.TaskProgress ) { $provTask.TaskProgress } Else {0} } Catch { }
  117.  
  118.         Write-Progress -Activity "Creating Virtual Machines:" -Status "$totalPercent% Complete:" -percentcomplete $totalPercent
  119.         Sleep 15
  120.         $ProvTask = Get-ProvTask -AdminAddress $adminAddress -TaskID $provTaskId
  121.       }
  122.  
  123.       # Assign the newly created virtual machines to the machine catalog
  124.       $provVMs = Get-ProvVM -AdminAddress $adminAddress -ProvisioningSchemeUid $provScheme.ProvisioningSchemeUid
  125.       Write-Verbose "Assigning the virtual machines to the new machine catalog."
  126.       ForEach ( $provVM in $provVMs ) {
  127.         Write-Verbose "Locking VM $provVM.ADAccountName"
  128.         Lock-ProvVM -AdminAddress $adminAddress -ProvisioningSchemeName $provScheme.ProvisioningSchemeName -Tag 'Brokered' -VMID @($provVM.VMId)
  129.         Write-Verbose "Adding VM $provVM.ADAccountName"
  130.         New-BrokerMachine -AdminAddress $adminAddress -CatalogUid $catalogUid.Uid -MachineName $provVM.ADAccountName
  131.       }
  132.       Write-Verbose "Machine catalog creation complete."
  133.  
  134.    } Else {
  135.     # If provisioning task fails, provide error
  136.     # Check that the hypervisor management and storage resources do no have errors. Run 'Test Connection', 'Test Resources' in Citrix Studio
  137.     Write-Error "Provisioning task failed with error: [$provTask.TaskState] $provTask.TerminatingError"
  138.    }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement