Powershell XenApp Deployment Wizard v1

Ever wanted an easier way to deploy XenApp machines en mass? Well have I got a treat for you.

XenDesktop has an easy way to deploy virtual machines from Citrix Provisioning Server (PVS) but XenApp with PVS is missing this component making deploying virtual machines sometimes a very tedious task. I want to make that easier for myself, I mean the community :). I have began working on a script with another engineer and friend that should ease that pain. This script is only a v1 with future versions to support other hypervisors and remove some of the manual ad nauseum type work on large deployments.

Currently the script is designed to do the following.

Prerequisites:

  1. You will need to create two files currently placed in the root of C: (paths and files can be changed) 
    1. One file will contain a list of servers (servers.txt) and the other the list of ip addresses (ips.txt) Match up the lines in each file so the server and IP match up.
  2. You will need to run this script from the Provisioning Server
  3. Download and configure the Following Powershell Snap Ins
    1. XenServer Powershell Snap-IN
      1. Download XS-PS Windows installer
    2. Configure the PVS Powershell MCLI snap in
      1. The snapin comes with the Provisioning Services Console. To use the snapin, you have to first register it (requires .Net framework). If your Windows is 32bits, use this command: 
        1. “C:WindowsMicrosoft.NETFrameworkv2.0.50727installutil.exe” “C:Program FilesCitrixProvisioning Services ConsoleMcliPSSnapIn.dll” 
      2. For 64bits: “C:WindowsMicrosoft.NETFramework64v2.0.50727installutil.exe” “C:Program FilesCitrixProvisioning Services ConsoleMcliPSSnapIn.dll” 
      3. If you encountered error, make sure that you are running the Command Prompt as administrator. 
      4. Once registered, start a PowerShell console and add the snapin using “add-PSSnapIn mclipssnapin”. The main cmdlets are mcli-run, mcli-get, mcli-set and mcli-delete. To get a detailed help on the cmdlets, use mcli-help.

Once you have completed the prerequisites you can run the script. The script is currently designed to do the following.

  1. Enter variables needed for script to run and confirm settings
  2. Create XenServer VMs based upon servers identified in c:servers.txt from template
  3. Create c:macs.txt listing all Mac addresses for each XenServer VM created from servers.txt
  4. Add IP MAC Reservations to primary Microsoft DHCP Server
  5. Add Devices to Citrix PVS server in appropriate collection and Site
  6. Export IP Mac Reservations from primary Microsoft DHCP server to Secondary DHCP server

As this script is a v1 it is making a lot of assumptions and I plan on building more logic and support for various configurations into the script. If you have any ideas or suggestions, please leave me a comment or contact me.

Upcoming Features

  • VMware Support

##########################################################################
# XenApp PVS Deployment Wizard
# This script is designed to help deploy XenApp machines en masse to a XenApp Farm using XenServer and Microsoft DHCP
# XenApp_Wizard_v1.ps1 script written by Phillip Jones and David Ott
# Version 1.0
# This script is provided as-is, no warrenty is provided or implied.
#
# The author is NOT responsible for any damages or data loss that may occur
# through the use of this script.  Always test, test, test before
# rolling anything into a production environment.
#
# This script is free to use for both personal and business use, however,
# it may not be sold or included as part of a package that is for sale.
#
# A Service Provider may include this script as part of their service
# offering/best practices provided they only charge for their time
# to implement and support.
#
# For distribution and updates go to: http://www.wwwp2vme.com
##########################################################################

add-pssnapin xenserverpssnapin
add-pssnapin mclipssnapin

# Variables Section – This will define the variables that the script requires in order to create the VMs in DHCP, PVS and XenServer

$sitename = Read-Host “Enter the PVS Site Name.”
$collectionname = Read-Host “Enter the PVS collection name.”
$xenserver = Read-Host “Enter the XenServer host name to connect to.”
$XSBase = Read-Host “Enter the base VM to copy. (Case Sensitive!)”
$SR = Read-Host “Enter the storage repository name. (Case Sensitive!)”
$pdhcpip = Read-Host “Enter the IP address of the primary DHCP server.”
$sdhcpip = Read-Host “Enter the IP address of the secondary DHCP server.”
$pdhcpscope = Read-Host “Enter the DHCP scope (ie:10.xxx.xxx.0).”

” “
“Please confirm before continuing.”
” “

“PVS Site Name: “+$sitename
“PVS Collection Name: “+$collectionname
“XenServer: “+$xenserver
“Base VM: “+$XSBase
“Storage Repository: “+$SR
“Primary DHCP IP: “+$pdhcpip
“Secondary DHCP IP: “+$sdhcpip
“DHCP Scope: “+$pdhcpscope

$n = ([System.Management.Automation.Host.ChoiceDescription]”&No”)
$n.helpmessage = “No, exit script”
$Y = ([System.Management.Automation.Host.ChoiceDescription]”&Yes”)
$y.helpmessage = “Yes, continue script”
$YN= ($Y,$N)

Function Prompt-YesNo ($Caption = “Confirm”, $Message = “Do you want to continue?”,$choices = $YN)
    {
        $host.ui.PromptForChoice($caption,$Message,[System.Management.Automation.Host.ChoiceDescription[]]$choices,1)
    }

$answer = Prompt-YesNo
    if ($answer -eq 0) {“Continue”} else {Exit}
        Connect-XenServer -server $xenserver
        cmd /c if not exist c:csv md c:csv
    if (Test-Path c:macs.txt) {remove-item c:macs.txt}
        $vmnames = get-content c:servers.txt
        $ips = get-content c:ips.txt
        Remove-Item c:csv*.*

# Xenserver – create VMs then pull MAC addresses for each and append c:MACs.txt

foreach ($vmname in $vmnames)
    {
    Invoke-Xenserver:VM.Copy -VM $XSBase -NewName $vmname -SR $SR
        $vifs = Get-XenServer:VM.VIFs -VM $vmname
        $vmname | Out-File c:CSVVMs.csv -append -Encoding ASCII
        $vifs.mac | Out-File c:MACs.txt -append -Encoding ASCII
    }

# MAC Translations – Required for DHCP and PVS as MAC formats are different for each program
# PVS MAC MCLI input format
Get-Content c:MACs.txt | ForEach-Object { $_ -replace “:”, “-” } | Set-Content c:csvMDevice.csv

# DHCP MAC input format
Get-Content c:MACs.txt | ForEach-Object { $_ -replace “:”, “” } | Set-Content c:csvMDHCP.csv

# Obtain IP addresses from ips.txt file
Get-Content c:ips.txt | Set-Content c:csvips.csv
    $num = 0
    $items = get-content c:csvvms.csv

# DHCP and Citrix PVS
foreach ($item in $items)
    {
        $server = get-content C:csvVMs.csv | Select-Object -Index $num
        $mdhcp = get-content C:csvMDHCP.csv | Select-Object -Index $num
        $ip = Get-Content C:csvips.csv | Select-Object -Index $num
        $mdevice = Get-Content C:csvMDevice.csv | Select-Object -Index $num
        “Dhcp Server \”+$pdhcpip+” Scope “+$pdhcpscope+” Add reservedip “+$ip+” “+$mdhcp+” “+”`”$server`””+” “+”`”`””+” “+”`”DHCP`”” | Out-File c:csvprimdhcp.txt -append -Encoding ASCII
        “Dhcp Server \”+$sdhcpip+” Scope “+$pdhcpscope+” Add reservedip “+$ip+” “+$mdhcp+” “+”`”$server`””+” “+”`”`””+” “+”`”DHCP`”” | Out-File c:csvsecdhcp.txt -append -Encoding ASCII
# Citrix PVS add device to Site and Collection
        Mcli-Add Device -r siteName=$siteName, collectionName=$collectionName, deviceName=$server, deviceMac=$mdevice
        $num = $num + 1
    }

“@Echo Off” | out-file c:csvdhcpimport.cmd -encoding ASCII

#DHCP – This will export the settings of the DHCP reservations added above
“netsh exec c:csvprimdhcp.txt” | out-file c:csvdhcpimport.cmd -append -encoding ASCII

#DHCP – This will import the reservations on your secondary Microsoft DHCP server
“netsh exec c:csvsecdhcp.txt” | out-file c:csvdhcpimport.cmd -append -encoding ASCII
“echo Please verify all objects have been created successfully” | out-file C:csvdhcpimport.cmd -append -encoding ASCII
“pause” | out-file C:csvdhcpimport.cmd -append -encoding ASCII
Remove-Item c:csv*.csv
cmd /c C:csvdhcpimport.cmd