I’m great believer in scripting and automation in general – after so many years (believe it or not, but I’ve started with Powershell 8 years ago), opportunities to work on tons of exciting projects and with so many amazing people it simply feels natural for me to write the scripts on the go without even realizing that I’m doing so. And I’ve just realized that it might be useful for someone if I would share the snippets of code that I’m using.

Be aware – scripts in PoshCorner won’t be too robust, they won’t have great documentation and I don’t plan to go into details of how they work. These scripts will be mostly useful for someone who needs to find syntax or wants to build something robust and seeks some guidance. There are simply one-shot scripts that I usually just type into console and none of them takes longer than 1 hour to create.

All scripts are provided as-is (but that doesn’t mean you shouldn’t report any errors you have encountered) under following disclaimer:

“This software application is provided to you “as is” with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.”

Monitoring session state of XenApp session

During typical engagement, I tend to cross-check every configuration change that I do. Normal manual confirmation would simply take too much time – I write automated routines and at the end of the day I just check if the values are the ones that I expected…

I like to configure lingering policy (and pre-launch when I use thin desktop where application are provided by XenApp) and I always configure both disconnect and logoff timers. My typical basic configuration is 20 minutes for lingering disconnect and 30 minutes for lingering logoff, 1 hour for disconnect and 3 hours for logoff of active session. But it’s easy to make a mistake – for example configure timeouts through the policy that applies only to XenDesktop.

To monitor if this configuration works, I simply leave active session running over night and run following script. It’s rather simple, only argument it expects is -User (in Domain\UserName format). Optionally, you can use -Wait argument – it means that session is not running yet and it should wait for session to start.

Example usage if session is already launched:
.\Monitor-SessionState.ps1 -User “CITRITE\mzugec”

Example usage if session is not running yet:
.\Monitor-SessionState.ps1 -User “CITRITE\mzugec” -Wait

As a result, you should be able to see how session status is changing. State refers to general session state (Active, Disconnected…), while ApplicationState helps with tracking of lingering policy.

Martin Zugec