24 October 2011

How to determine the client IP address in XenApp 6

I have been modifying a client's 2500 line VBScript login script recently (what a beast!) and had to find a replacement for ICACLIENTINFO.EXE which was used in their Presentation Server 4.0 environment to determine the client IP address and map printers based on subnet. ICACLIENTINFO.EXE does not work with XenApp 6/6.5, so I had to find a replacement method. After some hunting through the registry I found this:
HKLM\SOFTWARE\Citrix\Ica\Session\2\Connection\ClientAddress
Now I just had to find a way to determine the Session ID, which was easier than I thought:
HKCU\Volatile Environment\2


And now to put it together into a script:
Set objShell   = CreateObject("Wscript.Shell")
aRegKeys       = RegEnum(".", "HKCU", "Volatile Environment")
sessionID      = aRegKeys(0)
strIPCTXClient = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Citrix\Ica\Session\" & sessionID & "\Connection\ClientAddress")

WScript.Echo "Client IP Address: " & strIPCTXClient



'************************************************************************
'* 
'*  Function RegEnum()
'* 
'* Purpose: Enumerate all subkeys of the specified registry key.
'*
'* Input: strHkey - registry hive (HKLM, HKCU etc)
'* strKey - the registry key to enumerate
'*
'* Output:  An array of the subkeys. An empty array is returned for an error.
'*
'************************************************************************
Function RegEnum(strTarget, strHkey, strKey)
Const VBObjectError = -2147221504 
Const FUNCTIONNAME = "RegEnum ()"

Dim intHkey

Const HKEY_CLASSES_ROOT   = &H80000000
Const HKEY_CURRENT_USER   = &H80000001
Const HKEY_LOCAL_MACHINE  = &H80000002
Const HKEY_USERS          = &H80000003
Const HKEY_CURRENT_CONFIG = &H80000005
 
Select Case UCase(strHkey)
  Case "HKCR"
intHkey = HKEY_CLASSES_ROOT
  Case "HKCU"
intHkey = HKEY_CURRENT_USER
  Case "HKLM"
intHkey = HKEY_LOCAL_MACHINE
  Case "HKU"
intHkey = HKEY_USERS
  Case "HKCC"
intHkey = HKEY_CURRENT_CONFIG
  Case Else
 Err.Raise vbObjectError, FUNCTIONNAME, "Invalid HKEY: " & strHkey
 RegEnum = Array()
 Exit Function
End Select

On Error Resume Next
Dim objReg
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_ 
strTarget & "\root\default:StdRegProv")

If Err <> 0 Then
RegEnum = Array()
Exit Function
End If

' Get all subkeys in the specified key
Dim arrSubKeys
objReg.EnumKey intHkey, strKey, arrSubKeys

If Err <> 0 Then
RegEnum = Array()
Exit Function
End If

If IsArray(arrSubKeys) Then 
RegEnum = arrSubKeys
Else
RegEnum = Array()
End If

End Function

2 comments:

  1. Here is another way

    Set objCTXWMIService = GetObject("winmgmts:\\localhost\root\citrix\hdx")
    Set colCTXItems = objCTXWMIService.ExecQuery("Select ClientIP from Citrix_Sessions")

    For Each objItem in colCTXItems
    ClientIP = Cstr (objItem.ClientIP)
    objClientName = Cstr (objItem.ClientName)
    Next

    ReplyDelete
  2. Thanks for the script, do you know if this works only with newer clients?

    ReplyDelete