by Martin Therkelsen, CTA
Today’s post is about Citrix PVS (PVS) and PowerShell. This combination has been a pretty awful experience for a long time, but now things are looking up. Since PVS version 7.1x, where Citrix released a PowerShell module instead of MCLI commands, we have been able to use some real PowerShell commands and logic. In this article, I will show you how to do common PVS task with PowerShell.
The tasks I want to show in this article are the following:
- Create device collection
- Create device
- Import vdisk
- Set vdisk properties
- Assign vdisk
- Create a new vdisk version
- Promote vdisk
- Delete vDisk from store
- Delete a device
- Delete a device collection
Information used throughout the article
Device collection: CitrixLab
Device name: Citrixlab-XA01
Mac address: 00:11:22:33:44:55
Site name: Skanderborg
vDisk name: CitrixlabvDisk
Store name: CitrixlabStore
Create PVS device collection
So lets us start from the top and work our way through this. The first thing we need to do is import the module into our PowerShell session, this is done with the following command:
Import-Module "C:\Program Files\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll"
When you have it loaded, you can run this command to view what options you have:
Get-Command -Module Citrix.PVS.SnapIn
So, to create a new device collection we first run:
Get-Help New-PvsCollection -Examples
This will provide us with the information we need to create the new device collection. From the help, we can see that the minimum information we need to provide is a name for the collection along with the siteID. So, the name is something we can decide, but the siteID is something we need to find. Finding the SiteID is easy, we just use the command
In my lab, this looks like this:
To create a new device collection called Citrixlab, we can use this command with the information we just found:
New-PvsCollection -CollectionName "Citrixlab" -SiteName "Skanderborg"
Create a PVS target device
Going through the same process with creating a new device, we get the following from the help:
New-PvsDevice -Name theDevice -DeviceMac "00-11-22-33-44-55" -SiteName theSite -CollectionName theCollection
So, if we have a device with a Mac address of 00:11:22:33:44:55, we can add it to the PVS configuration with this command:
New-PvsDevice -DeviceName "Citrixlab-xa01" -DeviceMac "00-11-22-33-44-55" -CollectionName "Citrixlab" -SiteName "Skanderborg"
Do notice the format that the Mac address needs to be in.
Create PVS Store
To create a new PVS store, we can use the command below, this will create a new store called “CitrixlabStore” and it has the path of “D:\Stores\Citrixlab”
New-PvsStore -StoreName "CitrixlabStore" -Path "D:\Stores\CitrixlabStore"
Next up is importing a vDisk into PVS.
To import a new vDisk into PVS, I have created a small PowerShell module because, I didn’t think the built-in options were clear to use. I have put my module into the PowerShell gallery, so, to install it, you only need to run this command: “Install-Module CitrixPVSModule” in your PowerShell. If you want it only for your own user only, you can use this command instead: “Install-Module CitrixPVSModule -Scope CurrentUser” When you have the module installed, you can use the command below to import a vDisk into PVS.
Import-PVSVDisk -NewVDiskName "CitrixlabvDisk" -PVSStore "CitrixlabStore" -PVSServerName "PVS01" -PVSSiteName "Skanderborg" -VHDXPath "D:\Stores\CitrixlabStore"
To explain what happens in the code above, I will go through the flow that is needed to execute the code. The first thing I needed to find was the information about the vhdx I wanted to import. The information I need is: disk size, creation date, name and last write date. With this information, I can go ahead and create a new XML document that allows me to import the disk into PVS, this is the step I feel is missing from the built-in commands for PVS, because they assume that the XML file is present already which they never are when I import vDisks. Now that we have the XML document we need, I can use the built-in command for importing, but to make it easy for everyone who wants to use my module I put that into my module as well. Executing the command I showed will do all the work needed.
I have added some documentation to the PowerShell help part, so if you use these two commands, you can get more information on how to use the module
“Get-Help Import-PVSVDisk -Full”
Setting vDisk properties
In the same PowerShell module that I have used to import vDisks into PVS, I have also added a Set-PVSVdiskProperties command that, for me, makes it easier to set the properties needed for the vDisk. These properties are for instance, licensing and cache type. Below is an example of how to use the command:
Set-PVSVdiskProperties -PVSStore "CitrixlabStore" -PVSSite "Skanderborg" -VdiskName "CitrixlabvDisk" -CacheType CacheInRAMOverflowToDisk -LicenseType KMS -CacheSizeInMB 2048
To get more information about how to use the Set-PVSVdiskProperties command, use the help that I have provided with the module. You can view the help by typing this command:
“Get-Help Set-PVSVdiskProperties -Full”
Assign vDisk to the device collection
To add a vDisk to a device collection, we can use this simple command:
Add-PvsDiskLocatorToDevice -SiteName "Skanderborg" -StoreName "CitrixlabStore" -DiskLocatorName "CitrixlabvDisk" -CollectionName "Citrixlab" -RemoveExisting
Create a new vDisk version
To create a new version of the vDisk to install patches and software on, you can use the command below.
New-PvsDiskMaintenanceVersion -DiskLocatorName "CitrixlabvDisk" -SiteName "Skanderborg" -StoreName "CitrixlabStore"
Promote vDisk version
When you have installed your software and patches on the new vDisk version, you can use the following command to put it into test mode.
Invoke-PvsPromoteDiskVersion -DiskLocatorName "CitrixlabvDisk" -StoreName "CitrixlabStore" -SiteName "Skanderborg" -Test
When you have tested your new vDisk version and want to put it into production, you can use the following command. Notice the only difference is the “-Test” that is on the command to put the vDisk into test mode.
Invoke-PvsPromoteDiskVersion -DiskLocatorName "CitrixlabvDisk" -StoreName "CitrixlabStore" -SiteName "Skanderborg"
Delete vDisk from store
If you want to delete a vDisk from your store, you need to find the disk locator id and then you can remove it. The code to obtain that is shown below.
$LocaterID = (Get-PvsDisk -DiskLocatorName "CitrixlabvDisk" -SiteName "Skanderborg" -StoreName "CitrixlabStore").DiskLocatorId Remove-PvsDiskLocator -DiskLocatorId $LocaterID -DeleteDiskFile
Note that if you do NOT want to delete the VHDX from your store, just remove the “-DeleteDiskFile” from the command line.
Delete a device
To delete a device is really simple as well, again I have used the help to find the parameters, so if we want to delete the device we just created we can do it like this:
Remove-PvsDevice -DeviceName "Citrixlab-XA01"
Delete a device collection
To delete a device collection, you need to specify the collection name or id along with either the siteID or the site name. An example of this is shown below:
Remove-PvsCollection -CollectionName "Citrixlab" -SiteName "Skanderborg"
I hope that this article is useful for you and please provide any feedback you might have.