Powershell Script to launch a Published Application from one or more XenApp Servers

by Stan Czerno June 11, 2014 04:06 CST

[UPDATE] This script will not actually alert you to errors if there is a problem with the IMA Service. Please see my latest post, Powershell Script to launch one or more Published Applications from Storefront.

Two weeks ago I had a server in my lab that would not launch an application. The event log had event 10001 listed and it read "A usable server cannot be found on which to launch the application...".

So I checked out my homegrown monitoring service (which I will blog about later on) and everything was fine. The User Load was 0, the ICA port was listening and responding correctly, the XML service was running, etc. etc., but nothing would launch. This server has been running fine for more than two years.

I did some Google-Fu and came up with some articles and forum posts. The only thing that worked for me was to remove the server from the farm, reboot and then re-join the Farm.

So, afterwards I thought it would be useful to know if there was ever an issue when launching a Published Application from one or more XenApp Servers. I wrote the following script to do just that.

I schedule it to run once per hour between 6AM and 6PM. If there is an error I get an email with a summary of the issue, including which server has an issue. I run the script on a Windows 7 VM, I did not test running it from a XenApp Server.

The Scheduled Task is setup like this:
# Program/Script: %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe
# Arguments: -ExecutionPolicy Bypass -file C:\Scripts\LaunchPublishedApp.ps1

As an additional bonus, I am also logging how long it takes to log on to each server. I am averaging 19 seconds with just Command Prompt. I utilize Citrix Profile Management on the servers so 19 seconds is pretty darn good.

In order for the script to interact with the ICO, it requires the following to be set in the registry:
x64: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Citrix\ICA Client\CCM
x86: HKEY_LOCAL_MACHINE\SOFTWARE\Citrix\ICA Client\CCM

AllowLiveMonitoring REG_DWORD 1
AllowSimulationAPI REG_DWORD 1

Let me know what you think of the script and if you know of a better way of doing this.

http://www.czerno.com/default.asp?inc=/html/Citrix/Scripts/LaunchPublishedApp.asp


Tags: , ,

Catergories: Citrix | XenApp | Powershell

Comments (34) -

Roberto
November 2, 2014 19:54 CST

Hello,

I use this script to test the logons the morning before people starts to work. The scripts works fine if there is only one XenApp 6.5 Server, as soon there are 2 or more servers I receive a lot of errors.

When I run the script and there are more then two servers, sporadically the Testuser can login but the scripts writes an error that the user could not login and then I receive an email with that server. But the login works fine and the test user is not logged off.

Further I can run the script only once. After that I have to reboot the server that starts the script. Otherwise I will receive always following error in the logfiles:
*****
[SUCCESS] Testing Published Application, Windows-Explorer LYS, on Server xxxxxx07
[SUCCESS] Launching ICA Session using Citrix Client version 14.1.200.13
[SUCCESS] Connect Started at 11/03/2014 05:39:45
[INFO] Waiting for an ICA Event
[SUCCESS] ICA Event Detected: EventConnectFail
[ERROR] Connection Failed to server xxxxxx07
[INFO] Clicking OK in the Citrix Receiver dialog window
[INFO] Finding a window named Citrix Receiver
[WARNING] There was no Window titled Citrix Receiver, the error is most likely due to the script
[INFO] Number of Active Sessions is: 0
[SUCCESS] Connection Ended at 11/03/2014 05:39:58
[SUCCESS] Total Elasped Connection Time is: 13.0884 sec(s)

[INFO] No New Alerts Detected

[SUCCESS] Email Sent

[SUCCESS] Total Elapsed Script Time: 119.5272 sec(s)

[SCRIPT_END] Script Ended at 11/03/2014 05:40:01
*****

Can someone help me with that problem?

Thanks and Regards,
Roberto

Stan Czerno
November 3, 2014 02:58 CST

I will have to build a third server in my Lab to see why more than two servers is failing.

Stan Czerno
November 3, 2014 09:26 CST

I was unable to re-produce your issue. I added a VM to my farm and the script successfully ran on all three Servers.

Ross McCarthy
November 3, 2014 05:44 CST

Hi there,
This is a great script and thanks so much for posting it.
I'm trying to extend it a little to look for a particular window opening before marking a success. We have some old apps that generate dll errors etc on launch that we need to catch and repair manually each time. Checking that the application window opens will make this a real good trick.
Thanks again and I'll forward you any changes for review
Ross

Stan Czerno
November 5, 2014 01:57 CST

I am not sure you are going to be able to achieve this since the Windows you are looking for are in the Citrix Session, not local.

Ross McCarthy
November 5, 2014 05:14 CST

Thats a good point! Thanks for saving me time.
Maybe we could capture the screen somehow... I'll report back!

Stan Czerno
November 5, 2014 06:38 CST

There are ways to have PowerShell create a Screen Shot.

gallery.technet.microsoft.com/.../eeff544a-f690-4f6b-a586-11eea6fc5eb8

http://poshcode.org/585

Roberto
November 4, 2014 20:58 CST

Hey Stan,

We installed a Windows 7 machine (32bit) and configured the script on this machine. Now the script is working fine.

Do you know why the script works fine on a Windows 7 machine but not on Windows Server 2008 R2 Server? we will test this further and if we find the solution we will inform you.

And a last question: is it possible to introduce an option so that an Email is send when the script runned without errors? perphaps witch green color and the information that no error where found.

Thank you for that fantastic script. You have done a great job!

Regards,
Roberto

Stan Czerno
November 5, 2014 01:54 CST

I am not sure why it would not work from Server 2008 R2. I never tested it from a server, only Windows 7.

It would be possible to alert on Success as well, but I could not add that feature right now, my current workload does not allow it.

Pawel
November 5, 2014 03:18 CST

I believe the problem is with 64 bit OS - I can launch it but it doesn't detect that the application was launched. The problem lies here:

            $ICAEvents = "EventConnectFail","EventLogin","EventLoginFail","EventDisconnect"
            $a = 0
            do
                {
        $a++
        $LogonNotif = $ICA.GetNotificationReason()

I get $ICA.GetNotificationReason() as EventNone and the script always shows Failure even though the app launched. Will checked that and let know.
Thanks for great job

Stan Czerno
November 5, 2014 03:22 CST

Which "problem" are you referring to?

This script has to run in the 32 bit Powershell; %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe

Pawel
November 5, 2014 03:36 CST

yes I launch it in Powershell (x86) but still I get the above and in the log:
Waiting for an ICA Event
Did not detect a Successful Logon or a Connect Failure
The last ICA Notification was: EventNone
Assuming Logon Failure, Server is not available or the Application is not available
Check the Published Application, Server Logons are Enabled, User Account and/or Script User Variables

But application launched properly Smile

Stefan
January 6, 2015 22:20 CST

Hi
I have the same issue.
$ICA.GetNotificationReason() alwasy gets back "EventNone" even if the Application launched.
Do you have any idea why??

Paweł
January 8, 2015 05:39 CST

Hi Stefan,

I was not able to find the source of the problem. So I changed the script and the way I'm checking if the app has launched. Please check it here:
pawelserwan.wordpress.com/.../

Stan Czerno
January 7, 2015 01:25 CST

Are you making the necessary registry changes?

In order for the script to interact with the ICO, it requires the following to be set in the registry:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Citrix\ICA Client\CCM
AllowLiveMonitoring REG_DWORD 1
AllowSimulationAPI REG_DWORD 1

CTX911
January 21, 2015 19:00 CST

You created realy good stuff! But unfortunately this script will not work for me because of audio and encryption.

Stan Czerno
February 1, 2015 05:46 CST

[UPDATE] This script will not actually alert you to errors if there is a problem with the IMA Service. Please see my latest post, Powershell Script to launch one or more Published Applications from Storefront.

www.czerno.com/.../powershell-script-to-launch-one-or-more-published-applications-from-storefront

Prakash
July 16, 2015 20:32 CST

Still I'm facing the issue with Task Scheduler. When I ran the script (Health Check) manually it works perfectly how I expect... but, the same script if I ran via Task Scheduler the half of the output was truncated. Could you please suggest me on this...

Stan Czerno
July 17, 2015 01:51 CST

Can you clarify what your problem is?

What do you mean by "the half of the output was truncated"?

Prakash
July 17, 2015 02:12 CST

I'm using the following command to get check the server load
"$CurrentServerLoad = Get-XAServerLoad -ServerName $server"

When I ran it manually, it returns the exact output in th email but, when I ran the script via Task Scheduler it read the line and get the value as "null" and it goes to else condition and it returns the output as "IMA Err" under ServerLoad Column (in email)

Could you please suggest how to overcome this when I run the Script via Task Scheduler.

Stan Czerno
July 17, 2015 02:20 CST

When you schedule the Task, are you running it with a Citrix Admin account?

Prakash
July 17, 2015 02:22 CST

Yes. It's a Citrix Domain Account and it has full admin privileges.

Prakash
July 17, 2015 02:26 CST

When I ran the below command in "Command Prompt" it's working fine.

Powershell.exe -ExecutionPolicy Bypass -file "D:\XA_6.5_Scripts\Testing\XenAppServerHealthCheck.ps1"

Also, tried that, save the above command as batch file and ran the batch file via task scheduler. That also returns the ServerLoad column as NULL

Stan Czerno
July 17, 2015 02:36 CST

So the account is a Citrix Administrator.

Is the Task set to "Run with highest privileges"?

Is the Task ran on the same machine as when you run it manually?

Create a test.ps1 script and add this to the script:

"Get-XAServerLoad -ServerName <ADD A SERVER HERE> | out-file D:\XA_6.5_Scripts\test.txt"

Schedule this script the same as the other script and see if the text file contains the correct info.

Prakash
July 17, 2015 03:00 CST

1. Created the test.ps1 and it contains "Get-XAServerLoad -ServerName <xxx> | out-file D:\XA_6.5_Scripts\test.txt"
2. Ran the script manually and I got the output file test.txt and it returns the xxx servername details as below
ServerName Load MachineName
---------- ---- -----------
XXX         100 XXX
3. Delete the test.txt file & ran the test.ps1 from task scheduler.
4. test.txt file didn't generated for task scheduler

I'm worried. :'(

Stan Czerno
July 17, 2015 03:56 CST

Are you using the same account when running it Manually and Scheduled?

Are you sure the Task is set to "Run with highest privileges"?

Also, this is not the Blog Post for the Health Check Script. This blog post is "Powershell Script to launch a Published Application from one or more XenApp Servers". You are using the Health Check script, right?

Use this test script:

$FileName = "D:\XA_6.5_Scripts\test.txt"
$ServerName = <ADD A SERVER HERE>
$Echo = "Testing Script"
$echo | out-file $FileName

if ((Get-PSSnapin "Citrix.*" -EA silentlycontinue) -eq $null)
{
  try { Add-PSSnapin Citrix.* -ErrorAction Stop }
  catch { Write-Error $Error[0] -ErrorAction Continue
    $Error[0] | out-file $FileName -append
  }
}

Try {Get-XAServerLoad -ServerName $ServerName | out-file -append $FileName}
catch { Write-Error $Error[0] -ErrorAction Continue
  $Error[0] | out-file $FileName -append
  }

Prakash
July 20, 2015 02:01 CST

Pal,

Thank you for the support and reply. I tried the above example and finally found the bug but, need your assistance...

I've checked the task is set to "Run with highest privileges" and I've chosen "Run only when user is logged on" then, its working fine and getting the expecting result.
When I changed the option from "Run only when user is logged on" into "Run whether user is logged on or not" and ran the script from task scheduler and im getting the error for server load option.

Please check and advice.

Thank you

Stan Czerno
July 20, 2015 02:23 CST

I am not sure what the issue is. The D: drive you referenced is a Local Drive and not a Mapped Drive, right?

Pawel Serwan
July 20, 2015 02:47 CST

Hi Prakash,

You probably talk about the problem I wrote about here:
pawelserwan.wordpress.com/.../

Regards,
Paweł

Prakash
July 20, 2015 03:27 CST

Thanks Stan & Pawel for your valuable suggestion.

I ran the Script manually and using Task scheduler and getting the same output.
But, right click the task --> properties --> general tab --> under security options section --> When I select the check box "Do not store password. The task will only have access to local resources" its throwing different error and its fails in "Server Load" and its throws an error at Uptime also.

I've uncheck the option & ran the script its working fine.

could you please suggest me why that option is required? and where can I get more details about this..

Thanks
Prakash

Pawel Serwan
July 21, 2015 04:28 CST

Hi Prakash,

Have you checked my post on my blog? You have to enable autologon as there is a problem with Interactive Screen in Session 0. Please check one more time and let me know.

Bill Rogge
September 7, 2016 08:32 CST

I am trying to utilize this script in a new position I took less than a month ago.  I am not getting any results since the ICA session never gets started.

Here is an excerpt of the logging.
Testing Published Application, Citrix AppCenter, on Server CTXXA01T
Launching ICA Session using Citrix Client version 13.4.400.9
Connect Started at 09/07/2016 13:48:48
Get-Process : Cannot find a process with the name "wfica32". Verify the process name and call the cmdlet again.At
C:\scripts\czerno\Monitor_GWL.ps1:393 char:29
+                 $sessionID= get-Process -name wfica32|sort starttime  ...
+                             ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (wfica32:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

YES, this is being executed in the x86 version of PowerShell.
I have verified that the credentials being utilized are in fact correct.
What is different, however, is that we are utilizing the Enterprise Client (V13.4.400.9).  I am not saying that is the issue, but it about the only thing I can tell that is actually different.

Have you ever seen this error being returned?  I have watched and wfica32 never gets started within Task Manager.

Bill Rogge
September 7, 2016 08:49 CST

This has been resolved.

I need to actually read the documentation and utilize Copy/Paste.  After spelling the registry keys correctly, the script executed as expected.

Stan Czerno
September 7, 2016 09:20 CST

I released a new version of this script, did you check it out? www.czerno.com/.../powershell-script-to-launch-one-or-more-published-applications-from-citrix-storefront-2-x-through-3-6

Comments are closed