Sample Provisioning Server Powershell Scripts : Add / Rename / Delete a Device
Aug
16
Written by:
8/16/2009 2:11 PM
I'm currently working for a customer on automating Citrix Provisioning Server through Powershell Scripts.
For those who have had a look on how to automate PVS, there are three ways on how to achieve it :
- The provided Mcli.exe command line tool
- The PowerShell SnapIn
- The PVS SOAP API
The PowerShell SnapIn was more suited to my needs then I've started writing some scripts to achieve typical configuration and Admin tasks.
The first scripts I'll publish are dedicated to Add / Rename / Delete a Device.
As PowerShell does not support spaces within Command Line Arguments, the only limitation is not to add spaces for item names.
Please also remember that PVS Devices Name should be limited to 15 cars.
Create a PVS Device
#============================================================================================
# Script Name : CreateDevice.ps1
# Script Purpose : Create PVS Device
# Script Revision : 1.00
# Script Author : pierre.marmignon@citrixtools.net
#============================================================================================
#
#============================================================================================
# Script PreRequisites :
# 1) Register Provisioning Services SnapIn with "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" "C:\Program Files\Citrix\Provisioning Services Console\McliPSSnapIn.dll"
# 2) Allow PowerShell Scripts Execution => Open Powershell and Set Policy Execution Rights : Set-ExecutionPolicy unrestricted
#============================================================================================
#
#============================================================================================
# Script Parameters List
# Argument(0) : Full LogFile Path
# Argument(1) : Site Name
# Argument(2) : Collection Name
# Argument(3) : Device Name
# Argument(4) : Device Mac Address
# Argument(5) : Device Description
#=============================================================================================
#
#=============================================================================================
# Sample Command Line Execution : powershell.exe MyScriptName.ps1 Argument(0) Argument(1) Argument(2) Argument(3)
#=============================================================================================
#
#=============================================================================================
# Exit Codes Definition List
# Exit Code 0 : Script Execution OK
# Exit Code 1602 : Invalid Arguments Count
# Exit Code 1603 : No Log File Specified
# Exit Code 1604 : Item Already Existing
# Exit Code 1605 : Error Occured when processing task
# Exit Code 1606 : Item does not exists
#=============================================================================================
#
# Initialization Variables
Add-PSSnapin -Name McliPSSnapIn # Important : Imports PVS CmdLet
$ArgsNeeded = 6 # Important : Sets the number of expected Arguments
$strArg = "Argument"
$strArgsCount = "Arguments Counts : "
$TimeLog = "" # Init for this variable not to be nothing.
Write-Output ""
$strlogfile = $args[0] # LogFile is bound to Script Argument 1
$strSiteName = $args[1] # Site Name is bound to Script Argument 2
$strCollectionName = $args[2] # Collection Name is bound to Script Argument 3
$strDeviceName = $args[3] # Device Name is bound to Script Argument 4
$strDeviceMac = $args[4] # Device Mac Address is bound to Script Argument 5
$strDeviceDesc = $args[5] # Device Description is bound to Script Argument 6
# Script Functions
# Function Get TimeStamp | Used for Log Purposes : Returns The Formatted TimeStamp
Function GetTimeStamp()
{
Write-Output (Get-Date).ToString("yyyy-MM-dd hh:mm:ss")
}
# Function StartLogging | Used for Log Purposes
Function StartLogging()
{param ($LogFile)
Write-Output "$Logfile"
if($logfile -ne $Null)
{
Write-Output ""
Start-Transcript -append -force -path $logfile
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Started"
Write-Output ""
Write-Output ""
}
Else
{
Write-Output ""
Write-Output "Exiting : No Log File Specified"
Write-Output ""
$host.SetShouldExit(1603)
break
}
}
# Function StopLogging | Used for Log Purposes
Function StopLogging()
{
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Stopped"
Write-Output ""
Stop-Transcript
}
# Main Script Runtime
# Script Begin : Start Logging
StartLogging($strlogfile)
# Arguments Control
If (!($args.count -ieq $ArgsNeeded))
{
Write-Output ""
Write-Output "Arguments Count Not Valid : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1602)
# Stop Logging
StopLogging
#Exit
break
}
# Arguments Display
$strLogArgsMsg = $strArgsCount + $args.count
Write-Output $strLogArgsMsg
Write-Output ""
Write-Output "Detected Arguments"
Write-Output ""
$int = 0
foreach ($i in $args)
{
write-Output "$strArg($int) : $i"
$int++
}
# Main Task execution
$error.Clear()
# Check Item Existence
$Exists = Mcli-RunWithReturn Exists -p DeviceName=$strDeviceName | where-object {$_ –match “ReturnValue”} | foreach-object{$_.Split(“=”)[1].Trim()}
If ($Exists -eq 0) {
Write-Output ""
Write-Output "Creating Device $strDeviceName in Collection $strCollectionName (in Site $strSiteName)"
Write-Output ""
$error.Clear()
$Result = Mcli-Add Device -r SiteName=$strSiteName,CollectionName=$strCollectionName,DeviceName=$strDeviceName,DeviceMac=$strDeviceMac,Description=$strCollectionDesc | where-object {$_ –match “resultId”} | foreach-object{$_.Split(“=”)[1].Trim()}
If ($Result.length -gt 2) {
Write-Output ""
Write-Output "Device Created Successfully with GUID : $Result"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(0)
}
Else
{
Write-Output ""
Write-Output "Error While Creating the Device"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1605)
}
}
Else
{
Write-Output ""
Write-Output "Device Already Existing : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1604)
# Stop Logging
StopLogging
#Exit
break
}
# Script End : Stop Logging
StopLogging
#==== END Script ====================================================
Rename a PVS Device
#=============================================================================================
# Script Name : RenameDevice.ps1
# Script Purpose : Rename PVS Device
# Script Revision : 1.00
# Script Author : pierre.marmignon@citrixtools.net
#=============================================================================================
#
#=============================================================================================
# Script PreRequisites :
# 1) Register Provisioning Services SnapIn with "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" "C:\Program Files\Citrix\Provisioning Services Console\McliPSSnapIn.dll"
# 2) Allow PowerShell Scripts Execution => Open Powershell and Set Policy Execution Rights : Set-ExecutionPolicy unrestricted
#=============================================================================================
#
#=============================================================================================
# Script Parameters List
# Argument(0) : Full LogFile Path
# Argument(1) : Actual Device Name
# Argument(2) : New Device Name
#=============================================================================================
#
#=============================================================================================
# Sample Command Line Execution : powershell.exe MyScriptName.ps1 Argument(0) Argument(1) Argument(2) Argument(3)
#=============================================================================================
#
#=============================================================================================
# Exit Codes Definition List
# Exit Code 0 : Script Execution OK
# Exit Code 1602 : Invalid Arguments Count
# Exit Code 1603 : No Log File Specified
# Exit Code 1604 : Item Not Existing
# Exit Code 1605 : Error Occured when processing task
# Exit Code 1606 : Item does not exists
#=============================================================================================
#
# Initialization Variables
Add-PSSnapin -Name McliPSSnapIn # Important : Imports PVS CmdLet
$ArgsNeeded = 3 # Important : Sets the number of expected Arguments
$strArg = "Argument"
$strArgsCount = "Arguments Counts : "
$TimeLog = "" # Init for this variable not to be nothing.
Write-Output ""
$strlogfile = $args[0] # LogFile is bound to Script Argument 1
$strDeviceName = $args[1] # Actual Device Name is bound to Script Argument 2
$strNewDeviceName = $args[2] # New Device Name is bound to Script Argument 3
# Script Functions
# Function Get TimeStamp | Used for Log Purposes : Returns The Formatted TimeStamp
Function GetTimeStamp()
{
Write-Output (Get-Date).ToString("yyyy-MM-dd hh:mm:ss")
}
# Function StartLogging | Used for Log Purposes
Function StartLogging()
{param ($LogFile)
Write-Output "$Logfile"
if($logfile -ne $Null)
{
Write-Output ""
Start-Transcript -append -force -path $logfile
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Started"
Write-Output ""
Write-Output ""
}
Else
{
Write-Output ""
Write-Output "Exiting : No Log File Specified"
Write-Output ""
$host.SetShouldExit(1603)
break
}
}
# Function StopLogging | Used for Log Purposes
Function StopLogging()
{
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Stopped"
Write-Output ""
Stop-Transcript
}
# Main Script Runtime
# Script Begin : Start Logging
StartLogging($strlogfile)
# Arguments Control
If (!($args.count -ieq $ArgsNeeded))
{
Write-Output ""
Write-Output "Arguments Count Not Valid : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1602)
# Stop Logging
StopLogging
#Exit
break
}
# Arguments Display
$strLogArgsMsg = $strArgsCount + $args.count
Write-Output $strLogArgsMsg
Write-Output ""
Write-Output "Detected Arguments"
Write-Output ""
$int = 0
foreach ($i in $args)
{
write-Output "$strArg($int) : $i"
$int++
}
# Main Task execution
$error.Clear()
# Check Item Existence
$Exists = Mcli-RunWithReturn Exists -p DeviceName=$strDeviceName | where-object {$_ –match “ReturnValue”} | foreach-object{$_.Split(“=”)[1].Trim()}
If ($Exists -ne 0) {
Write-Output ""
Write-Output "Renaming Device $strDeviceName"
Write-Output ""
$error.Clear()
$Result = Mcli-Set Device -p DeviceName=$strDeviceName -r DeviceName=$strNewDeviceName
$Result = "$Result"
If ($Result.contains("succeeded")) {
Write-Output ""
Write-Output "Device Renamed Successfully"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(0)
}
Else
{
Write-Output ""
Write-Output "Error While Renaming the Device"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1605)
}
}
Else
{
Write-Output ""
Write-Output "Device does not exists : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1606)
# Stop Logging
StopLogging
#Exit
break
}
# Script End : Stop Logging
StopLogging
#==== END Script ====================================================
Delete a PVS Device
#============================================================================================
# Script Name : DeleteDevice.ps1
# Script Purpose : Delete PVS Device
# Script Revision : 1.00
# Script Author : pierre.marmignon@citrixtools.net
#============================================================================================
#============================================================================================
# Script PreRequisites :
# 1) Register Provisioning Services SnapIn with "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe" "C:\Program Files\Citrix\Provisioning Services Console\McliPSSnapIn.dll"
# 2) Allow PowerShell Scripts Execution => Open Powershell and Set Policy Execution Rights : Set-ExecutionPolicy unrestricted
#============================================================================================
#
#============================================================================================
# Script Parameters List
# Argument(0) : Full LogFile Path
# Argument(1) : Device Name
#============================================================================================
#============================================================================================
# Sample Command Line Execution : powershell.exe MyScriptName.ps1 Argument(0) Argument(1) Argument(2) Argument(3)
#===========================================================================================
#
#===========================================================================================
# Exit Codes Definition List
# Exit Code 0 : Script Execution OK
# Exit Code 1602 : Invalid Arguments Count
# Exit Code 1603 : No Log File Specified
# Exit Code 1604 : Item Not Existing
# Exit Code 1605 : Error Occured when processing task
# Exit Code 1606 : Item does not exists
#============================================================================================
#
# Initialization Variables
Add-PSSnapin -Name McliPSSnapIn # Important : Imports PVS CmdLet
$ArgsNeeded = 2 # Important : Sets the number of expected Arguments
$strArg = "Argument"
$strArgsCount = "Arguments Counts : "
$TimeLog = "" # Init for this variable not to be nothing.
Write-Output ""
$strlogfile = $args[0] # LogFile is bound to Script Argument 1
$strDeviceName = $args[1] # Actual Device Name is bound to Script Argument 2
# Script Functions
# Function Get TimeStamp | Used for Log Purposes : Returns The Formatted TimeStamp
Function GetTimeStamp()
{
Write-Output (Get-Date).ToString("yyyy-MM-dd hh:mm:ss")
}
# Function StartLogging | Used for Log Purposes
Function StartLogging()
{param ($LogFile)
Write-Output "$Logfile"
if($logfile -ne $Null)
{
Write-Output ""
Start-Transcript -append -force -path $logfile
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Started"
Write-Output ""
Write-Output ""
}
Else
{
Write-Output ""
Write-Output "Exiting : No Log File Specified"
Write-Output ""
$host.SetShouldExit(1603)
break
}
}
# Function StopLogging | Used for Log Purposes
Function StopLogging()
{
Write-Output ""
$TimeLog = GetTimeStamp
Write-Output "$TimeLog Log Stopped"
Write-Output ""
Stop-Transcript
}
# Main Script Runtime
# Script Begin : Start Logging
StartLogging($strlogfile)
# Arguments Control
If (!($args.count -ieq $ArgsNeeded))
{
Write-Output ""
Write-Output "Arguments Count Not Valid : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1602)
# Stop Logging
StopLogging
#Exit
break
}
# Arguments Display
$strLogArgsMsg = $strArgsCount + $args.count
Write-Output $strLogArgsMsg
Write-Output ""
Write-Output "Detected Arguments"
Write-Output ""
$int = 0
foreach ($i in $args)
{
write-Output "$strArg($int) : $i"
$int++
}
# Main Task execution
$error.Clear()
# Check Item Existence
$Exists = Mcli-RunWithReturn Exists -p DeviceName=$strDeviceName | where-object {$_ –match “ReturnValue”} | foreach-object{$_.Split(“=”)[1].Trim()}
If ($Exists -ne 0) {
Write-Output ""
Write-Output "Deleting Device $strDeviceName"
Write-Output ""
$error.Clear()
$Result = Mcli-Delete Device -p DeviceName=$strDeviceName
$Result = "$Result"
If ($Result.contains("succeeded")) {
Write-Output ""
Write-Output "Device Deleted Successfully"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(0)
}
Else
{
Write-Output ""
Write-Output "Error While Deleting the Device"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1605)
}
}
Else
{
Write-Output ""
Write-Output "Device does not exists : Exiting"
Write-Output ""
# Set ExitCode
$host.SetShouldExit(1606)
# Stop Logging
StopLogging
#Exit
break
}
# Script End : Stop Logging
StopLogging
#==== END Script ====================================================
I'll publish more scripts (included Import from .csv file for example) in the following weeks so stay tuned !