Wilco van Bragt - LinkeIn Wilco van Bragt - Twitter rssa 

Citrix Provisioning Services PowerShell scripts

In the article series Unattended Installation Citrix Provisioning Services I already touched the PowerShell possibilities of PVS showing how to configure farm and server settings via PowerShell. In the same project I already wrote some additional PowerShell scripts to partly automate some tasks within the PVS infrastructure, so no mistakes could be made. Although they are not rocket science I decided to share them, as some commands are not fully obvious when reading the PowerShell Guide.

Creating a vDisk

The first I would like to share is creating a vDisk that is ready for creating the base image via the Master Target Device. Within the script you need to define the Site and the PVS Store where the vDisk should be defined. The person who start the script should provide the name of the vDisk that is being created. When those information is available the disk will be created via Mcli-RunWithReturn CreateDisk command. In this case we were using fixed values for the size of the disk (43250) and use the local server to host the vDisk with a blocksize of 16 MB. As we would like to use the disk for writing the image to, the disk should be in private mode which is accomplished via type=1.

#---------------------------------------------------------------------
# Script: CreatevDisk.ps1
# Unattended Creation of vDisk
# Creator: Wilco van Bragt
# Creation Date: 11-10-2013
#---------------------------------------------------------------------

# Import Modules
#---------------------------------

Import-Module -Force Function

#Define Variables
#---------------------------------

$PVSSiteValue=”VanBragtSite”
$PVSStoreValue = “vDisksPVS”

#User Defined Variables
#---------------------------------

$vDiskName=Read-Host "Please enter the name of the vDisk"

#Create vDisk
#---------------------------------

Mcli-RunWithReturn CreateDisk -p name="$vDiskName",size="43520",storename="$PVSStoreValue",SiteName="$PVSSiteValue",servername="$env:computername",type="1",vhdBlockSize="16384"




Change vDisk to Shared Mode

The second script is to convert the disk into a shared image after the image has been written to the vDisk. Again the PVS Site and PSV Store are required and defined as a variable in the beginning of the script. Again the person running the script should provide the name of the vDisk that should be converted into the shared mode. The conversion is based on two PowerShell script. The first part based on mcli-Set Disk converts the disk into the shared mode. In this case we are using RAM for the write cache (writeCacheType=3), with a write cache of 20 GB (writeCacheType=3) and Windows/Office licensing is based on KMS (licenseMode=2).

The second part via the command Mcli-Set DiskLocator arranges that the disk is load balanced over the available PVS servers. This is arranged by specifying the parameter ServerName without a value, but with double quotes (ServerName=""). We are using in this case Load Balancing based on Best Effort (subnetAffinity=1) and rebalancing enabled (rebalanceEnabled=1).

#---------------------------------------------------------------------
# Script: ChangevDisktoSharedXenApp65.ps1
# Unattended Changing vDisk to Shared including XenApp 6.5 PVS settings
# Creator: Wilco van Bragt
# Creation Date: 21-08-2013

# Import Modules
#---------------------------------

add-PSSnapIn mclipssnapin

#Define Variables
#---------------------------------

$PVSSiteValue=”VanBragtSite”
$PVSStoreValue = “vDisksPVS”

#User Defined Variables
#---------------------------------

$vDiskName=Read-Host "Please enter the name of the vDisk"

#Change vDisk Settings
#---------------------------------

mcli-Set Disk -p diskLocatorName="$vDiskName",siteName="$PVSSiteValue",storeName="$PVSStoreValue" -r writeCacheSize=20480, writeCacheType=3,licenseMode=2

Mcli-Set DiskLocator -p diskLocatorName="$vDiskName",siteName="$PVSSiteValue",storeName="$PVSStoreValue" -r rebalanceEnabled=1,subnetAffinity=1,ServerName=""

Create Template Target Device

I used a Template device that is being used to set-up additional Target Device with the correct settings. I added this part of the script to the unattended installation of the first PVS server (I created it later, so that’s why it’s not mentioned in the article series Unattended Installation of PVS.

Creating a Template is done in two phases. The first step is to create a device. A device has some required fields like collection name and site name , which are available as parameters in an earlier phase of the installation scripts. Also a Mac Address is required, but as it is a template you can fill it with dummy information (for example all zeros). We decided that we would like to log error message of the Target Devices, so we change the Loglevel to Error (logLevel="2") of the template so Target Devices created based on the template are inheriting the log level.

The second step is to “convert” the created device into a template. As a template belongs to a Device Collection it’s done via PowerShell command mcli-Set Collection. It took me a while to find that out, I expected this to be a device parameter.

add-PSSnapin -Name mclipssnapin

deviceName="__Template",collectionName="$PVSCollectionValue",siteName="$PVSSiteValue",deviceMac="000000000000",logLevel="2"

mcli-Set Collection -p CollectionName="$PVSCollectionValue",siteName="$PVSSiteValue" -r templatedevicename="__Template"

Add Target Devices

Adding a Taret Device manually is a time consuming activity and although there are some tools available, I prefer to add the target devices using a script. The script requires a file with the computername and the MAC address defined per line. In our situation we have a script that fills this file automatically when creating the Virtual Machines.

To keep the script flexible I added a component to specify the file name in which the Target Devices are entered. Secondly creating a Target Device requires a Site and Collection where the Target Device should be added. The script reads out the Target Devices per line and add the Target Device via the Mcli-Add Device to the PVS Farm using the MAC address and Name specified in the file. By specifying copyTemplate="1" the settings of the template devices are inherited.

Secondly the Target Device is also added to Active Directory as a computer object. Besides the device name you need to specify the OU where the object should be created. As this was a fixed location we just entered the location into the script, but you can create some logic around that if required.

#---------------------------------------------------------------------
# Script: AddTargetDeviceXenApp65.ps1
# Add Target Devices
# Creator: Wilco van Bragt
# Creation Date: 22-08-2013
#---------------------------------------------------------------------
# The file that will be used need to be formatted devicename,MaCaddress per line
#---------------------------------------------------------------------

# Import Modules
#---------------------------------

add-PSSnapIn mclipssnapin 

#Define Variables
#---------------------------------

$PVSSiteValue=”VanBragtSite”
$PVSCollectionValue= “VanBragtCollection”

#User Defined Variables
#---------------------------------

$targetdevices=Read-Host "Please enter the path and name of the file containing the target devices information"

#Read Target Devices and perform actions
#---------------------------------

$TDFile=Get-Content $targetdevices

foreach ($line in $TDFile)

                {              $line = $line -split(',')

                               $DeviceName = $Line[0]

                               write-host "$DeviceName"

                               $MAC = $Line[1]

                               write-host "$MAC"

                               #Add Target Devices

                               Mcli-Add Device -r deviceName="$DeviceName",collectionName="$PVSCollectionValue",siteName="$PVSSiteValue",deviceMac="$MAC",copyTemplate="1"

                               #Create AD Account

                               Mcli-Run AddDeviceToDomain -p deviceName="$DeviceName",organizationUnit="VanBragt/Servers/XenApp65SH"

                }

Conclusion

With this script I automated some time consuming and/or actions that are executed often eliminating the risk that settings are not configured correctly. Most parts are not rocket science, but it gives an insight in the possibilities and the way the PowerShell command of Citrix Provisioning Services works.