Retrieve Client IP Address and assign to Session Variable

ENTPF used Ask the Experts™
I'm using the PowerShell SDK for XenApp to return session information on current user.  The command: Get-XASession -account $Env:USERDOMAIN\$Env:USERNAME returns the session info for the specified user as below:

SessionId                : 2
SessionName              : RDP-Tcp#0
ServerName               : SERVER
AccountName              : DOMAIN\user1
BrowserName              :
State                    : Active
ApplicationState         : NotApplicable
ClientName               : COMPUTER1
ClientVersion            :
LogOnTime                : 4/16/2012 9:52:47 PM
Protocol                 : Rdp
AccessSessionGuid        :
SmartAccessFilters       : {}
VirtualIP                :
EncryptionLevel          : Unknown
ServerBuffers            : 0 x 0
ClientIPV4               :
ClientBuffers            : 0 x 0
ClientBuildNumber        : 7601
ColorDepth               : Colors8Bit
ClientDirectory          : C:\Windows\system32\mstscax.dll
ClientProductId          : 1
ClientAddress            :
ClientId                 : 2784836247
HorizontalResolution     : 933
VerticalResolution       : 1500
ConnectTime              : 4/17/2012 8:58:07 AM
DisconnectTime           : 4/17/2012 8:58:07 AM
LastInputTime            : 4/17/2012 10:48:12 AM
CurrentTime              : 4/17/2012 10:48:12 AM
ClientCacheLow           : 0
ClientCacheTiny          : 0
ClientCacheXms           : 0
ClientCacheDisk          : 0
ClientCacheSize          : 0
ClientCacheMinBitmapSize : 0
FlashEnabled             : True
WmpEnabled               : True
DirectXEnabled           : True
UsbEnabled               : False
ClientType               :
MachineName              : SERVER

I would like to be able to take just the data from ClientAddress and assign it to a session variable that will then be referenced from within a published application.

Has anyone done anything like this before?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Give this a go. Hopefully it'll do what you want it to.

$var = Get-XASession -account $Env:USERDOMAIN\$Env:USERNAME | Select -Expand ClientAddress

[Environment]::SetEnvironmentVariable("CLIENTADDRESS", $var, "User")
Assuming that by session variable you mean a variable in the environment space:

Set-Item -path env:ClientAddress -value ((Get-XASession -account $Env:USERDOMAIN\$Env:USERNAME  | Where {$_.Split(":")[0].trim() -eq "ClientAddress" }).Split(":")[1].Trim())


Thanks for the help..  Trying to tweak this a little more.  Since it's currently a XenApp 6.0 farm,  had to change to "ClientIPV4"  and specify -Full.

$var = Get-XASession -account $Env:USERDOMAIN\$Env:USERNAME -Full | Select -Expand ClientIPV4

This will return the clientIP for every session in the farm for the specified user.  Since, in our testing, all users are connecting from the same device, the clientIP is identical for all sessions.  We have limited by setting the environment variable using $var[0] since it is returning a collection.  

Probably need to add a little more logic to determine if the session is on the server we're connected to in case the user has sessions from multiple client devices.  Customer says everyone will just be connecting from one device but I'd like to be certain.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial