Scripting Citrix Provisioning Services (PVS) with PowerShell and Command Line
In the Ultimate Golden Image Automation Guide I wrote about creating a new Citrix Provisioning vDisk with MCLI command line. But you can of course also do this with PowerShell and there are a lot more commands to use. So in this blog I will show the most popular Citrix Provisioning commands in MCLI command line and PowerShell. I hope this will help you to script / automate your deployments and workflow. Because as always, automate EVERYTHING !!
*To use the PowerShell commands you first need to load the PVS PowerShell module: Import-Module “C:\Program Files\Citrix\Provisioning Services Console\Citrix.PVS.SnapIn.dll”
Jump to command:
- Create a new vDisk
- Add an existing vDisk to PVS Store
- Change vDisk mode
- Add vDisk infromation
- Create new vDisk version
- Promote vDisk version
- Revert vDisk version
- Merge vDisk version
- Merge vDisk versions to new base
- Change vDisk licensing type
- Create a new device collection
- Create a new device
- Assign vDisk to a collection
- Assign vDisk to a device
Create a new vDisk:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” RunWithReturn CreateDisk -p Name=DiskName Size=SizeInMB Storename=YourStoreName ServerName=YourPVSServerName Sitename=YourSiteName Format=1 Type=1 |
Format 1 is VHDX 0 is VHD | Type 1 is Dynamic 0 is Fixed
PowerShell
Start-PVSCreateDisk -Name ““YourDiskName” -Size “SizeInMB” -StoreName “YourStoreName” -ServerName “YourPVSServerName” -SiteName “YourSiteName” -VHDX |
Add an existing vDisk to PVS Store:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” add disklocator -r disklocatorNameYourDiskName sitename=YourSiteName storename=YourStoreName serverName=YourPVSServerName format=1 |
Format 1 is VHDX 0 is VHD
PowerShell
New-PvsDiskLocator -Name “YourDiskName” -StoreName “YourStoreName” -ServerName “YourPVSServerName” -SiteName “YourSiteName” -VHDX |
Change vDisk mode:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” set disk -p diskLocatorName=YourDiskName siteName=YourSiteName storeName=YourStoreName -r writeCacheType=9 writeCacheSize=SizeInMB |
PowerShell
Set-PvsDisk -Name ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -WriteCacheType “9” -WriteCacheSize “SizeInMB” |
WriteCacheType: 0 (Private), (other values are standard image) 1 (Cache on Server), 3 (Cache in Device RAM), 4 (Cache on Device Hard Disk), 6 (Device RAM Disk), 7 (Cache on Server, Persistent), or 9 (Cache in Device RAM with Overflow on Hard Disk). Min=0, Max=9, Default=0
Add vDisk information:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” SET DISK -p disklocatorName=YourDiskName sitename=YourSiteName storename=YourStoreName -r longDescription=Description date=DATE author=Author title=Title company=Company internalName=InternalName originalFile=OriganalFile |
PowerShell
Set-PvsDisk -Name ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -longDescription “Description” -date “DATE” -author “Author” -title “Title” -company “Company” -internalName “InternalName” -originalFile “OriganalFile” |
Create a new vDisk version:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” RunWithReturn CreateMaintenanceVersion -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName |
PowerShell
New-PvsDiskMaintenanceVersion -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” |
Promote vDisk version:
To production version
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run PromoteDiskVersion -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName |
PowerShell
Invoke-PvsPromoteDiskVersion -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” |
To test version
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run PromoteDiskVersion -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName test=1 |
PowerShell
Invoke-PvsPromoteDiskVersion -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -Test |
Revert vDisk version:
To test
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run RevertDiskVersion -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName test=1 |
PowerShell
Invoke-PvsRevertDiskVersion -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -test |
To maintenance
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run RevertDiskVersion -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName |
PowerShell
Invoke-PvsRevertDiskVersion -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” |
Merge vDisk version:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run MergeDisk -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName |
PowerShell
Merge-PvsDisk -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” |
Merge vDisk versions to new base:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Run MergeDisk -p diskLocatorName=YourDiskName siteName=YourSiteName StoreName=YourStoreName base=1 |
PowerShell
Merge-PvsDisk -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -NewBase |
Change vDisk licensing type:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” SET DISK -p disklocatorName=YourDiskName sitename=YourSiteName storename=YourStoreName -r licenseMode=2 |
PowerShell
Set-PvsDisk -DiskLocatorName ““YourDiskName” -StoreName “YourStoreName” -SiteName “YourSiteName” -LicenseMode “2” |
1 is MAK 2 is KMS
Create a new device collection:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Add Collection -r collectionName=NewCollectionName siteName=YourSiteName |
PowerShell
New-PvsCollection -SiteName “YourSiteName” – CollectionName “NewCollectionName” |
Create a new device:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” Add Device -r collectionName=NewCollectionName siteName=YourSiteName deviceName=NewDevice deviceMAC=00-00-00-00-00-00 |
PowerShell
New-PvsDevice -SiteName “YourSiteName” -CollectionName “NewCollectionName” -DeviceName “NewDevice” -DeviceMac “00-00-00-00-00-00” |
Assign vDisk to a collection:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” run assigndisklocator -p collectionName=NewCollectionName removeexisting=1 disklocatorname=YourDiskName sitename=YourSiteName storename=YourStoreName |
PowerShell
Add-PvsDiskLocatorToDevice -SiteName “YourSiteName” -StoreName “YourStoreName” -DiskLocatorName ““YourDiskName” -CollectionName “NewCollectionName” -RemoveExisting |
Assign vDisk to a device:
MCLI.exe Command Line
“C:\Program Files\Citrix\Provisioning Services Console\MCLI.exe” run assigndisklocator -p deviceName=DeviceName removeexisting=1 disklocatorname=YourDiskName sitename=YourSiteName storename=YourStoreName |
PowerShell
Add-PvsDiskLocatorToDevice -SiteName “YourSiteName” -StoreName “YourStoreName” -DiskLocatorName ““YourDiskName” -DeviceName “DeviceName” -RemoveExisting |
The best way to image your target device to a vDisk is of course to use the Base Image Script Framework (BIS-F). This script will seal and image your device. Check it out here.
I hope this was informative. For questions or comments you can always give a reaction in the comment section or contact me:
Hi
I have some PVS script that i use for several things. I have just created a merge script that are going on the weekend, but there is something that might bo handy. In the gui you can see how many procent that is done in the progress. Is there a way to see this progress somewhere else, i mean som cmd-lets? I have looked everywhere and just cant find anything. Since you can see it, it must be a way to find it. Could it be direct in the database maybe? If you run merge cmd-lets you can see that this will get an unique task id, this task must be found somewhere.
In xendesktop i use this progress in a script that it is find in get-provtask
Thats a good one!
Does this work: Get-PvsDiskUpdateStatus -DiskUpdateTaskId or -UpdateTaskId ?
Oh and here are all commands but the document is not so easy to read:
https://docs.citrix.com/content/dam/docs/en-us/provisioning-services/7-7/downloads/PowerShellObjectProgramGuide7-7.pdf
still no percent from the cmd-lets you mentioned
A that’s a bummer, have you tried looking trough the guide ?
seems that the snapins from 7.15.1 reveals the answer for this question if you run get-help Start-PVSCreateDisk -showWindow
within example number 1 you should get the information how to achieve this.
get-pvstask | Where-Object {$_.Command -eq “MergeDisk” -and $_.State -eq 0}|Get-PvsTaskStatus
This will give you percentage complete.
Thanks for the guide Chris. Hopefully some of these can come in handy to complete changes out of hours.
For example I have some production servers that all use a vDisk. I want them to boot from a new vDisk in a few days time when I am out of the office.
Can I just create a scheduled task to run the powershell script, will it remove the link to the existing vDisk (I’m thinking yes). Would I have to shut these devices down first before the script is run ?
Add-PvsDiskLocatorToDevice -SiteName “YourSiteName” -StoreName “YourStoreName” -DiskLocatorName ““YourDiskName” -DeviceName “DeviceName” -RemoveExisting
Thanks in advance
Hi Paul,
You’re welcome! You can indeed save the script to a ps1 file and schedule it with task shedulair. With the command above the device don’t have to be shutdown when performaning the command. They will just keep running with the “old” disk until reboot. So in your use case you should create a script which changes the disk with above command and after that perform a remote reboot 🙂
Does anyone have a Powershell command to Change the Load Balancing of the vdisk to a specific PVS server?
There is none at the moment. You can only set a Pvs server when you create or add a disk. So you could remove the disk from the Pvs server but keep the vhdx and then add it again on the right Pvs server.
Here is one I use as a function I add to James Kindon PVSImageMangementTool PS script.
Usage:
# Will set load balance to PVS server connection that is currently in use when running Set-PVSConnection function from PVS PS module
PS> SetLoadBalance ‘Disable’
# Will set default load balance if more than one PVS server is in PVS Site
PS> SetLoadBalance ‘Enable’
function SetLoadBalance ($LB) {
# pops up a window to select vDisk to modify
$DiskInfo = @(Get-PvsDiskInfo -Verbose -StoreName “” -SiteName “” | Select-Object Name, Description, SiteName, StoreName, Enabled, DiskLocatorId | Out-GridView -Title ‘Select a vDisk’ -PassThru)
[string]$LBdisk = Get-PvsDiskLocator -DiskLocatorId $DiskInfo.disklocatorid
$LBprimary = Get-PvsConnection | select -expandProperty server
# Trimming primary server in case FQDN is used from PVS connection info
[string]$LBServerTrim = $LBprimary.substring(“”, 15)
if ($LB -eq “Enable”) {
try {
$Info = “Attempting to enable loadbalancing for $($DiskInfo.Name)”
Write-Host $Info -ForegroundColor Green
# portion for actual change. Setting to server to blank “” defaults to load balance re-enabling
Get-PvsDiskLocator -DiskLocatorId $($DiskInfo.disklocatorid) -Fields ServerName | foreach { $o = $_; $o.ServerName = “”; $o } | Set-PvsDiskLocator
# portion for actual change. Setting to server to the PVS connection we are using on this script to setup load balancing to one server
#Get-PvsDiskLocator -DiskLocatorId $DiskInfo -Fields ServerName | foreach { $o = $_; $o.ServerName = $LBServerTrim; $o } | Set-PvsDiskLocator
$Info = “Successfully enabled $($DiskInfo.Name) to load balance to $($LBServerTrim) ”
Write-Host $Info -ForegroundColor Green
}
catch {
[string]$ErrorText = $Error[0].CategoryInfo.Reason
[string]$Errordetail = $Error[0].Exception.Message
Write-Warning $Errordetail
Write-Output $ErrorText
Write-Warning “Please Check Device maintenance manually”
#Write-Error $Errordetail
Write-Output “Error: $($_.Exception.Message) -Line Number: $($_.Invocationinfo.ScriptLineNumber)”
}
}
elseif ($LB -eq “Disable”) {
try {
$Info = “Attempting to disable loadbalancing for $($DiskInfo.Name)”
Write-Host $Info -ForegroundColor Green
# portion for actual change. Setting to a specific PVS server to the current powershell PVS connection we are using on this script to setup load balancing to one server
Get-PvsDiskLocator -DiskLocatorId $($DiskInfo.disklocatorid) -Fields ServerName | foreach { $o = $_; $o.ServerName = $LBServerTrim; $o } | Set-PvsDiskLocator
$Info = “Successfully enabled $($DiskInfo.Name) to load balance to $($LBServerTrim) ”
Write-Host $Info -ForegroundColor Green
}
catch {
[string]$ErrorText = $Error[0].CategoryInfo.Reason
[string]$Errordetail = $Error[0].Exception.Message
Write-Warning $Errordetail
Write-Output $ErrorText
Write-Warning “Please Check load balancing manually”
#Write-Error $Errordetail
Write-Host “Error: $($_.Exception.Message) -Line Number: $($_.Invocationinfo.ScriptLineNumber)”
}
}
else {
Write-Warning “Not able to set $($LB)”
Write-Host “Not able to set $($LB)”
exit
}
}
How did you get this to work? I get “ServerName is a Read-Only Property”
Hi Guys,
Is there a Powershell script that can tell me how many clients are connected to a PVS server? or even more – if the streaming service is displaying as “Down”?
You can check from Citrix studio / Appcenter / Director? for status of streaming service it can be done at the OS level
Get-service should get the status of all the services of the OS
How can i create a new maintenance version of our prod version when u already have an newer vdisk (test access mode) version up for a testing scenario. In gui console i only get a new vdisk versioin of the newest one but i wanna new vdisk from our older prod vdisk.
I dont think it is possible yet, you can only create a version from the latest vdisk be it prod or test
how do you add a description/properties to a maintenance version with PVS PS API?
Thank you, it helps me alot
i need to delete multiple devices in device collection of pvs
is there any script for it
New-PvsCollection command gives Internal Server Error..Any suggestions on this??
Hi Guys, has anyone automated the following tasks:
Create a new version of the vDisk in maintenance mode
Launch a VM using that version
Apply latest windows patches + reboot if required
Run clean up scripts on the VM and shut it down
Promote vDisk to production
Start restarting target servers