;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       ClientName() 
;; 
;;ACTION         Returns the name of the client computer when connecting via RDP/ICA 
;; 
;;AUTHOR         Allen Powell 
;;               format/comments by Glenn Barnas 
;; 
;;VERSION        1.0 - 2012/10/14 
;; 
;;HISTORY        1.0 - 2012/10/14 - Initial Release 
;; 
;;SYNTAX         ClientName() 
;; 
;;PARAMETERS     None 
;; 
;;REMARKS        None 
;; 
;;RETURNS        Name of client computer 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    WXP, W2K3, W2K8, W2k12 
;; 
;;EXAMPLES       ; If remote connection, get client hostname 
;;               If InStr('RDP,ICA', Left(%SESSIONNAME%, 3)) @$Client = ClientName() EndIf 
; 
Function ClientName()
 
  Dim $_AltRegView				; State of WOW64 Registry View 
  Dim $s_sessionid				; SessionID String 
  Dim $_iIndex					; index of SessID  
  Dim $_SessionID				; Session ID in Registry for 2K8a 
 
  ; define the alternate registry view for all actions 
  $_AltRegView = SetOption('WOW64AlternateRegView', 'On')
 
  Select
   ; Defined in the environment - no extra effort required 
   Case Len(%CLIENTNAME%)
    $ClientName = %CLIENTNAME%
 
   ; Citrix stores it in the registry 
   Case readvalue('HKLM\Software\Citrix\ICA\Session', 'ClientName')
    $ClientName = ReadValue('HKLM\Software\Citrix\ICA\Session', 'ClientName')
 
   ; Server 2008 - extract based on TEMP sub-path 
   Case InStr(@PRODUCTTYPE, 'Server 2008')
    $s_sessionid = SubStr(%TEMP%, 1 + InStrRev(%TEMP%, '\'))
    While $s_sessionid <> ''
      $_iIndex = InStr('0123456789ABCDEF', Left($_SessionID, 1))
      If $_iIndex
        $_SessionID = $_SessionID * 16 + (Cint($_iIndex - 1))
        $s_sessionid = SubStr($s_sessionid, 2)
      Else
        $_SessionID = 0
        $s_sessionid = ''
      EndIf
    Loop
    $ClientName = ReadValue('HKCU\Volatile Environment\' + $_SessionID, 'CLIENTNAME')
 
   ; Server 2003 - stored in the registry 
   Case InStr(@PRODUCTTYPE, 'Server 2003')
    $ClientName = ReadValue('HKCU\Volatile Environment', 'CLIENTNAME')
 
   ; Default - Not a server O/S, so use the workstation name 
   Case 1  
    $ClientName = @WKSTA
  EndSelect
 
  $_AltRegView = SetOption('WOW64AlternateRegView', $_AltRegView)
 
EndFunction