Let me start off this blog post by saying that as much as I want to be a 'developer', I just can't get my mind moving in that direction.  But the beauty with PowerShell is that it can take not even a weekend warrior of development like myself, and actually become useful with SDK's.  

Citrix has done an excellent job in the past few years really developing and providing a very useful policy engine and structure for not only XenApp, but also XenDesktop.  Personally, I have always love the Citrix policy engine, making it very easy to get to the end results I wanted - the look, feel and usefulness of a XenApp environment.  To me, storing the policies within the IMA database always left something to be desired.  Sure, it's always been there, and is a core component of the XenApp infrastructure.  Let's take for example a farm that consists of 20 various policies - printing, Flash management, TimeZone, etc, and consider the following scenarios.

A) You have a separate test farm, and you need to bring over 2 new policies to the production farm - how do you do it?
B) You have purchased a new company who currently runs a XenApp farm, and need to merge their policies into the main farm - how do you do it?
C) You want to migrate all of the XenApp IMA polices into Active Directory, for a single point of policy management - how do you do it?

The simple answer is the XenApp SDK.  The XenApp <a href="http://community.citrix.com/display/xa/XenApp+6.5+Powershell+SDK">SDK</a> allows for complete PowerShell management of a XenApp farm.  This blog post isn't intended as an overview of the full XA SDK, but will focus on the 3 scenarios from above, managing policies with the SDK.  

<h2>
Brief Overview of XenApp SDK
</h2>
The XenApp SDK was released initially with XenApp 5.0, as a replacement for the MFCOM commands that were included with earlier editions of XenApp.  To be honest, MFCOM never seemed super useful with XenApp management, but with SDK and the usage of PowerShell, I started to see where the SDK was proving to give that little extra bit we were missing.  So how do we use it?  Well, every XenApp server v6.x and above includes PowerShell, so all that needs to be installed is the SDK itself (I wish Citrix would just install this as part of the XenApp install).  There are a ton of resources on the web that provide a greater background on the SDK usage, including some very excellent Farm Reports that will print out the entire farm configuration with best practices - Google is your friend…

Let's start the SDK to see what we've got available:

<img src="http://egroup.jasonmangone.com/wp-content/uploads/2013/08/Image-8-26-13-at-10.13-AM.png" alt="Image 8 26 13 at 10 13 AM" title="Image 8-26-13 at 10.13 AM.png" border="0" width="600" height="354" />

Citrix provides a good amount of cmdlets with the SDK, and includes the Citrix.Common.GroupPolicy snap in.  However, that doesn't give us the full spectrum of possibilities with the SDK.  There's an extra file needed to manage anything Policy related, whether it's IMA or GPO policies.  That can be found <a href="http://community.citrix.com/download/attachments/162955822/Citrix.GroupPolicy.Commands.psm1?version=1">here.</a>

<h2>
Policy Management
</h2>
Policy Management. Some people might say so what, we can do it from within the Citrix App Center.  Not a big deal to recreate a policy.  But why recreate manually when you can automate something?  With the SDK, the first scenario listed above can be done in 2 lines of code - Export-CtxGroupPolicy  and Import-CtxGroupPolicy.  The SDK provides a very flexible and easy method to not only export all of your policies (Think Backup here), but to also import the policies.  Let's take a look at exporting a XenApp policy.

<em>If you're new to PowerShell, or just dabble in it, remember the command get-help.  Excellent details per cmdlet.  </em> 

<img src="http://egroup.jasonmangone.com/wp-content/uploads/2013/08/Image-8-26-13-at-10.30-AM.png" alt="Image 8 26 13 at 10 30 AM" title="Image 8-26-13 at 10.30 AM.png" border="0" width="600" height="225" />

Exporting a XenApp policy is as simple as issuing the command Export-CtxGroupPolicy.  Notice from the above image on the Export-CtxGroupPolicy cmdlet, there are a number of additional parameters that we can expose.  If you want to export a single policy (Scenario 2 from above), you can use the -PolicyName parameter.  If you want to export either the Computer or User policies, use the -Type parameter.  Very simple, but very powerful.  

<img src="http://egroup.jasonmangone.com/wp-content/uploads/2013/08/Image-8-26-13-at-10.50-AM.png" alt="Image 8 26 13 at 10 50 AM" title="Image 8-26-13 at 10.50 AM.png" border="0" width="486" height="61" />

The results of running the export command are 3 editable .XML files, one with the policy names, one with configuration, and one with the filters.

<img src="http://egroup.jasonmangone.com/wp-content/uploads/2013/08/Image-8-26-13-at-10.51-AM.png" alt="Image 8 26 13 at 10 51 AM" title="Image 8-26-13 at 10.51 AM.png" border="0" width="597" height="72" />

The resulting .xml files could be taken to another XenApp farm, and the Import-CtxGroupPolicy cmdlet could be run, pointed to the directory of the files, and the new policies would be available in the new farm.  Simple yet powerful in my opinion.  

<h2>
Migrating IMA policies to AD Group Policies
</h2>
At a customer site a few years back, I ran into an issue where the customer was pushing upwards of 100 policies in the IMA database, and anytime they would need to add/modify/remove a policy, either the IMA database would become corrupted, or it would be incredibly slow - even to just change the priority of a policy.  Long story short, we found that moving the policy items into Group Policies did not exhibit the same issues when the large number of policies was in place.  Great news, but how can we get all 100 policies migrated from the IMA database into Group Policies.  Sure, there is the XenApp Migration Tool, but that won't work here since we aren't migrating to a new farm.  Well, we've already touched on the Export and Import CtxGroupPolicy cmdlets and how easy it was.  Well, with a few additional commands and an additional parameter to the Export and Import commands, we can migrate policies between IMA and AD seamlessly.

Citrix provides an excellent CTX article on the exporting and importing of policies <a href="http://support.citrix.com/article/CTX128625">here,</a> and it's piece #1 of moving to the export/importing of policies.  

To go from IMA to AD (Or Local Server to AD/IMA), or AD to IMA policies we need to add one line to our plan, and that's using a PSDrive to allow us to navigate into the AD world.  Whether we are exporting from AD, or importing to AD, we need to use the PSDrive command to establish a 'mapped drive' connection to an existing GPO.  

Running the command, we need to know a) <strong>DomainGPODrive</strong> - what drive we want to use and b) <strong>DomainGPO</strong> - what GPO we are wanting to work against