Category Archives: Scripting

Exporting XenApp policys with powershell.

I needed to export our XenApp policies, as both a backup and a reference for an upcoming proof of concept involving XenApp 6. As no migration path is available, having the exported policies in a text document I can keep them open side by side while creating the new policies.

The following assumes your have the Xenapp Cmdlets installed and called.

foreach ($policy in get-xapolicy) {get-xapolicyconfiguration $policy.policyname | out-file “$env:temp$policy.txt”}

if you would prefer xml files:

foreach ($policy in get-xapolicy) {get-xapolicyconfiguration $policy.policyname | export-clixml “$env:temp$policy.xml”}

The above script dumps the policys in the chosen format to your temp folder. To auto jump to temp add “explorer $Env:temp” to the end of the line. E.G:

foreach ($policy in get-xapolicy) {get-xapolicyconfiguration $policy.policyname | export-clixml “$env:temp$policy.xml”} explorer $env:temp

Exporting and Importing XenApp Load Evaluators

For such a simple idea this was strangely not covered on google,  so  I had set about to create an EXE for the support guys in house to move our production load evaluators to our test environments.

Load evaluator information for the most part was accessible easily through mfcom, everything from high to low water marks and schedules are well covered, but for some reason IP ranges are mysteriously excluded and not supported. Personally this didnt bother me all that much, as we dont use ip ranges, but without it, a “covers all” published EXE was pointless.

So instead of develop a half assed gui, I moved to Powershell instead. Powershell covers this task so easily. If you havent installed the xenapp powershell cmdlets in your citrix environment, go… NOW. They’re excellent.

Open “powershell with Xenapp commands (ctp3)”:

Exporting a load evaluator:

get-xaloadevaluator -loadevaluatorname InsertLoadEvaluatorname | export-CLIXml c:pathfilename.xml

Once you’ve exported your load evaluator data, move to the new farm and run the following:

Importing a new load evaluator:

import-CLIXml c:pathfilename.xml | new-xaloadevaluator

Note: if the load evaluator already exists, and you wish to update it, use:

Updating a current Load evaluator:

import-CLIXml c:pathfilename.xml | set-xaloadevaluator

And thats it!

Determining if a number is odd or even using a batch file

Our challenge recently was seperating the reboots of our citrix servers to ensure that half of our farm is always available. This is particularly useful if you are a 24/7 house and need to know exactly what servers restart and when they restart.

So to begin, our server names consist of “servername00xx” where xx is the number of the server, the task at hand was to ensure that 0001, 0003, 0005 etc restarted on one night and 0002, 0004, 0006 etc restarted the second night. To do this i needed to use the system variable “hostname” and read the last digit in the name to determine if the number was odd or even.

To get determine whether a number is odd or even i used the following logic. If you divide an even number by 2 then multiply it by two you will get the same number back, e.g. 8/2 = 4, 4*2 = 8, 8 is equal to 8 .

An odd number in batch is treated as follows 9/2=4, 4*2=8 ,9 is NOT equal to 8.

Rem Begining Logic Check for even or odd server
for /f “tokens=1-2 delims=00″ %%a in (‘hostname’) do set compnumber=%%b
set /a divnumber=%compnumber%/2
set /a sum=%divnumber%*2

With the above excerpt, i set three variables, %compnumber% (which is the number after the 00), divnumber (a variable for the result of the computernumber divided by two) and sum, which is the resulting divnumber * 2.

This divides compnumber by 2 and multiplys divnumber by 2 setting the resulting number to sum.

Rem Running if checks
if %compnumber% NEQ %sum% goto odds
if %compnumber% EQU %sum% goto evens

This next step is our determiner:

If the number divided by two and multiplied by two is not the same as the original number, the number is odd, the if statement then sends the script to the odds labeled portion of our script.

If the number divided by two and multiplied by two is the same as the original number, well you get the idea, its even and jumps down.

<—Do something for odd servers here—>

<—Do something for even servers here—>

Forcing a printer online using a script:

printer_iconRecently while installing Zetafax 11, we noticed that some of our maintenance scripts on our XenApp servers were causing the zetafax printer to fall offline on reboot. Below is a simple to use (built in to server 2003) script that can be used to force a printer online.

The Following will force “Printer Name” to work online:

cscript C:WINDOWSsystem32prncnfg.vbs -t -p “Printer Name” -workoffline

As an extra function, the below will rename a local printer:

Cscript %windir%system32Prncnfg.vbs -x -p “old printer name” -z newprintername

Disabling Dep with a script

We had this problem recently with our XenApp servers, as part of the latest service pack DEP (or Data Execution Protection) has now been enabled for all applications accross all platforms and this can cause havok in a terminal services environment.

The problem with Dep is simple, its not a Reg Key, its not an environment variable its a system setting loaded on startup from the boot.ini file. To make matters worse its a kinda complicated setting as it sounds opposite to what it is exactly doing.

Take this for example, this is a server with DEP enabled:

multi(0)disk(0)rdisk(0)partition(1)WINDOWS=”Windows Server 2003, Standard” /NoExecute=OptOut /fastdetect

What confuses me with the above statement is why Opting out means DEP is enabled? To disable DEP you need to change this switch to OptIn, which makes no sense to me at all.

To automate this change, i used a tool called RPL.exe, this is a direct translation of the Unix command/tool to replace text inside of a text file. Using RPL i could search the boot.ini for the optout statement and replace it with the optin (aka turn off Dep).

Because boot.ini is a system and read only file by default, i needed to use the attrib command so first i set about removing the read only, hidden and system file attributes on the file in order to edit it:

attrib -r -h -s c:boot.ini

once the file was editable, i now performed the rpl command to search and replace:

rpl.exe -i /noexecute=optout /NoExecute=OptIn c:boot.ini

Once the file had been changed if neccessary it was time to set the file back to read only, hidden and system before closing the script:

attrib +r +h +s c:boot.ini

And voila, next reboot and Dep is gone :)