Using PowerShell with Citrix Policies
PowerShell has quickly become one of my favorite scripting interfaces. One of the cool things about PowerShell is its object oriented design and use. This makes it easy to blend command line scripting with Citrix objects exposed via MFCOM. Brandon Shell has posted a nice example of manipulating Citrix policies using PowerShell. Check out Brandon’s Citrix policy PowerShell functions:
Script To Get Citrix Policy
# Get-CitrixPolicy.ps1
Param($Server,$PolicyName = ".*")
# Enums in Use
$MetaFrameUnknownObject = 0
$MetaFrameWinFarmObject = 1
# Getting Farm Object
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize($MetaFrameWinFarmObject)
# Getting Policies that Match Name and Loading Data
$pol = $mfarm.policies($MetaFrameUnknownObject) | ?{$_.Name -match $PolicyName}
$pol | %{$_.LoadData($true)}
$pol
Script To Create a New Citrix Policy
# New-CitrixPolicy.ps1
Param($Server,$PolicyName,$PolicyDescription)
if(!$PolicyDescription){$PolicyDescription=$PolicyName)
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize(1)
$NewPolicy = $mfarm.CreatePolicy(19,$PolicyName,$PolicyDescription)
Script To Remove a Citrix Policy
# Remove-CitrixPolicy.ps1
Param($Server,$PolicyName = $(throw '$PolicyName is Required'),[switch]$whatif)
# Enums in Use
$MetaFrameUnknownObject = 0
$MetaFrameWinFarmObject = 1
# Getting Farm Object
$type = [System.Type]::GetTypeFromProgID("MetaframeCOM.MetaframeFarm",$Server)
$mfarm = [system.Activator]::CreateInstance($type)
$mfarm.Initialize($MetaFrameWinFarmObject)
# Getting Policies that Match Name and Loading Data
$policies = $mfarm.policies($MetaFrameUnknownObject) | ?{$_.Name -eq $PolicyName}
foreach($pol in $policies)
{
if($whatif){Write-Host " What if: Performing operation `"Delete`" on Target `"$($pol.Name)`". " -foreground yellow}
else{Write-Host " - Deleting $($pol.Name)";$pol.Delete()}
}
Be sure to check out some of the other PowerShell resources available on MSTerminalServices.org:
Using PowerShell to Manage Terminal Services Attributes
Using PowerShell in Your Environment
Technorati : Administration, Citrix, Policies, PowerShell, XenApp
Del.icio.us : Administration, Citrix, Policies, PowerShell, XenApp
Ice Rocket : Administration, Citrix, Policies, PowerShell, XenApp

Brandon Says:
March 30th, 2008 at 9:45 am
Thanks for the cross post
I truly love Powershell as well. It makes complex task in Batch and Vbscript trivial.
Note: It looks like you have the Get and New mixed up in your post.
Jason Says:
March 30th, 2008 at 7:21 pm
Thanks for pointing out the mix up. I corrected the post.