How to get ClientName within logon script on Server 2008 R2 XenApp 6

You could get the client name from the %CLIENTNAME% environment variable on a Server 2003 terminal server and get your logon script to do different things depending on its value.

Using VBScript you could do oShell.ExpandEnvironmentStrings(“%CLIENTNAME%”).

Try this on a Server 2008 R2 (including XenApp 6) server and you’ll just get %CLIENTNAME% back. Not very helpful.

Note that my logon script is configured via Group Policy: User Configuration, Policies, Windows Settings, Scripts (Logon/Logoff).

I’m guessing that they run a little too early, before the session is fully initialised.

However all is not lost, it is possible to get the client name, but it’s a bit more of a round about route!

The following is all VBScript:

Function sessionNumber
 Dim oShell, oExec, sOutput, iUserPos, iUserLen, iStatePos
 Set oShell = CreateObject("WScript.Shell")
 Set oExec = oShell.Exec("query session %username%")
 sOutput = LCase(oExec.StdOut.ReadAll)
 iUserPos = InStr(sOutput,LCase(oShell.ExpandEnvironmentStrings("%username%")))
 iStatePos = InStr(sOutput,"active")
 iUserLen = Len(oShell.ExpandEnvironmentStrings("%username%"))
 sessionNumber = CInt(Trim(Mid(sOutput,iUserPos+iUserLen,iStatePos-iUserPos-iUserLen)))
End Function

Function clientName
 Dim oShell
 Set oShell = CreateObject("WScript.Shell")
 On Error Resume Next
 clientName = LCase(oShell.RegRead("HKCU\Volatile Environment\"&sessionNumber&"\CLIENTNAME"))
 If Err.Number<>0 Then
 clientName =  "unknown"
 End If
End Function

WScript.Echo clientName

So we get the session number by parsing the output from the Query Session command, then use this to read the client name from the user’s registry. Luckily the registry does seem to be present and correct even though the environment variables themselves are not yet readable.

Because the above functions execute the Query Session command each time they’re run, you’re probably best stuffing the output of clientName into a something global (variable, dictionary, array – you choose) rather than calling the function every time you need the client name.

This entry was posted in Scripting, Windows, XenApp and tagged , , , , , , . Bookmark the permalink.

13 Responses to How to get ClientName within logon script on Server 2008 R2 XenApp 6

  1. Steve says:

    First, thanks for the great example. Wasn’t sure if you defined gdInfo somewhere else in your script, it’s not in this function though. I replaced that line with the below.

    iUserLen = Len(oShell.ExpandEnvironmentStrings(“%username%”))

    And all is right in the world again. I still can’t believe DOS is the best way to do this though.

    Like

  2. Mohamed B. says:

    Thanks for the post, very helpful indeed. I am wondering how to access the clientname for a user when the script is run by the administrator.

    Like

  3. Rob says:

    Thank you sooooo much!

    Like

  4. Benny K. says:

    Great Post!
    Please note that if you have legacy systems such as win xp or win 2003, then the script won’t work as the volatile environment does not have a session number in these systems. you can get around this with something like this:

    set oEnv=WshShell.Environment(“Volatile”)
    If clientName “unknown” Then ClientN = clientName Else ClientN=oEnv(“CLIENTNAME”)

    Like

  5. Tim Miller Dyck says:

    Thanks so much for this post. It was very helpful when moving to a Windows Server 2008 R2 terminal server environment where the 2003 simple method of using the CLIENTNAME environment variable didn’t work any longer in our scripts.

    Like

  6. Blues_Wolf says:

    Same thing, less lines, a cmd from the Citrix forums

    @echo off
    for /f “tokens=1-3” %%1 in (‘query session %USERNAME% ^| find “>”‘) do set ses_num=%%3
    for /f “tokens=1-3” %%1 in (‘reg query “HKCU\Volatile Environment\%ses_num%” /v CLIENTNAME’) do set clientname=%%3 )

    Regards

    Like

  7. Pingback: How To Get The Clientname Within A Logon Script? #PowerShell #RemoteDesktopServices #CitrixXenApp | out-web.net

  8. Citrix Friend says:

    @Blues_Wolf: perfect.

    Like

  9. Ole Hansson says:

    It doesn’t work with citrix, at least not the latest version. Use clientname with fasttrack. It can even give you the client ip address. Check http://www.fasttrackscript.com/citrix for the XenApp version. There is also an RDS version somewhere on the site

    Like

  10. Richard says:

    The code is good but not Always what you need though.. What if the user already have one or more sessions on the server? Then the code will pick up the first line containing the username and it might be the wrong one :(

    Like

    • rcmtech says:

      It’s generally a really bad idea to allow multiple sessions from the same user. Causes all kinds of file locking and profile issues. I never allow it on my systems, so my code works fine for me :-)

      Like

  11. Hans Straat says:

    Works also like charm in a huge printerscript we have, with vmware dem loaded (logon trigger)

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.