ValleyENT
asked on
Capture Terminal Server Client IP
Hello Everyone,
I am using the below code to map drives based on the end users subnet. The problem I am having is when a user connects to our terminal server from one of the below subnets it will not map the drive because it is picking up the servers IP rather than the Connecting Client's IP. Is there a way to resolve this?
I am using the below code to map drives based on the end users subnet. The problem I am having is when a user connects to our terminal server from one of the below subnets it will not map the drive because it is picking up the servers IP rather than the Connecting Client's IP. Is there a way to resolve this?
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.9"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\estrella"
Case "192.168.10"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\arrowhead"
Case "192.168.20"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\warner"
Case "192.168.50"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenfield"
Case "192.168.60"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\commercio"
Case "192.168.70"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\foothills"
Case "192.168.80"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\nwtucson"
Case "192.168.90"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenvalley"
Case "192.168.100"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\stmarys"
Case "192.168.200"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\corporate"
Case "192.168.205"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\pinnacle"
End Select
End If
Please try the script below and let me know if you get positive results or if nothing gets mapped.
Here is an article if %ClientName% does not grab the connected client...
https://www.experts-exchange.com/questions/24131917/clientname-environment-variable-not-present-at-login-script-on-windows-server-2008-TS.html
Here is an article if %ClientName% does not grab the connected client...
https://www.experts-exchange.com/questions/24131917/clientname-environment-variable-not-present-at-login-script-on-windows-server-2008-TS.html
Set objNetwork = CreateObject("Wscript.Network")
strServer = "."
Set objWMI = GetObject("winmgmts://" & strServer & "/root\cimv2")
Set objInstances = objWMI.InstancesOf("Win32_ServerSession",48)
On Error Resume Next
For Each objInstance in objInstances
With objInstance
WScript.Echo .ActiveTime
WScript.Echo .Caption
WScript.Echo .ClientType
WScript.Echo .ComputerName
WScript.Echo .Description
WScript.Echo .IdleTime
WScript.Echo .InstallDate
WScript.Echo .Name
WScript.Echo .ResourcesOpened
WScript.Echo .SessionType
WScript.Echo .Status
WScript.Echo .TransportName
WScript.Echo .UserName
End With
On Error Goto 0
Next
strConnectedClient = GetClientIP()
if strConnectedClient <> "" then
strSubnet = strConnectedClient
end if
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.9"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\estrella"
Case "192.168.10"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\arrowhead"
Case "192.168.20"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\warner"
Case "192.168.50"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenfield"
Case "192.168.60"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\commercio"
Case "192.168.70"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\foothills"
Case "192.168.80"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\nwtucson"
Case "192.168.90"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenvalley"
Case "192.168.100"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\stmarys"
Case "192.168.200"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\corporate"
Case "192.168.205"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\pinnacle"
End Select
End If
Function GetClientIP()
Dim aOctets
Dim sSubnet
Dim oShell
Set oShell = CreateObject("Wscript.Shell")
Dim sClient
sClient = oShell.ExpandEnvironmentStrings("%ClientName%")
Dim oPing
Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address='" & sClient & "'")
sSubnet = ""
For Each oRet in oPing
If Not IsNull(oRet.StatusCode) And oRet.StatusCode = 0 Then
if oRet.Address <> "" then
aOctets = Split(oRet.Address, ".")
If aOctets(0) <> "" Then
sSubnet = aOctets(0) & "." & aOctets(1) & "." & aOctets(2)
End If
end if
End If
Next
GetClientIP = sSubnet
End Function
ASKER
Neither one of these work. I am finding this very frustrating, if I create a batch file and do a simple "echo %clientip%" it sees the clients ip address. Why can't I simply integrate that into my original code?
How about this script?
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
strClientIP = GetClientIP()
if strClientIP <> "" then
strSubnet = strClientIP
end if
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.9"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\estrella"
Case "192.168.10"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\arrowhead"
Case "192.168.20"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\warner"
Case "192.168.50"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenfield"
Case "192.168.60"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\commercio"
Case "192.168.70"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\foothills"
Case "192.168.80"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\nwtucson"
Case "192.168.90"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenvalley"
Case "192.168.100"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\stmarys"
Case "192.168.200"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\corporate"
Case "192.168.205"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\pinnacle"
End Select
End If
Function GetClientIP()
Dim aOctets
Dim sClient
Dim oShell
Set oShell = CreateObject("Wscript.Shell")
sClient = oShell.ExpandEnvironmentStrings("%ClientIP%")
aOctets = Split(sClient, ".")
If aOctets(0) <> "" Then
GetClientIP = aOctets(0) & "." & aOctets(1) & "." & aOctets(2)
End If
End Function
ASKER
Says Line 68, Char 9 "script out of range, number: 1. Script fails to run? what could cause this?
Can you run this small script on the server and post the results.
Set oShell = CreateObject("Wscript.Shell")
sClient = oShell.ExpandEnvironmentStrings("%ClientIP%")
wscript.echo sClient
ASKER
Great, it actually echos "%clientip%" yesterday it was displaying the actual ip address. wtf?!?
I get the same thing. Nuts!
OK, back to the drawing board.
OK, back to the drawing board.
ASKER
Thank you so much for your help by the way. I wish there was a way to run a script in a citrix policy.
How about this script...
Care of;
http://community.citrix.com/display/xa/How+to+get+a+list+of+users+and+the+client+IP+addresses+using+Citrix+MFCOM
Care of;
http://community.citrix.com/display/xa/How+to+get+a+list+of+users+and+the+client+IP+addresses+using+Citrix+MFCOM
Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(1)
For Each objSession In objFarm.Sessions
WScript.Echo "User name : " & objSession.UserName
WScript.Echo "IP Address: " & objSession.ClientAddress
Next
ASKER
That works, but it dispalyed the username and ip address of every user that was connected! I had to click on like 50 times.
ASKER
Is there a way to tie in WTS_ClientAddress?
Sorry about the 50 clicks :-)
This is a good sign. This means that the script needs to pair the login ID with an IP address. This is done by looping and matching. There may be a problem doing this if you have many people logging in using the same name but seeing as how many time you had to click a few minutes ago that might not be a worry.
I will have a look at WTS_ClientAddress.
This is a good sign. This means that the script needs to pair the login ID with an IP address. This is done by looping and matching. There may be a problem doing this if you have many people logging in using the same name but seeing as how many time you had to click a few minutes ago that might not be a worry.
I will have a look at WTS_ClientAddress.
ASKER
Ok, thank you.
OK - this script loops looking for a matching username. Can you remember if the script from above showed usernames as "domain\usrename" or just "username". My script is matching just the username but it can also be modified to put in the domain name too.
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
strClientIP = GetClientIP()
if strClientIP <> "" then
strSubnet = strClientIP
end if
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.9"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\estrella"
Case "192.168.10"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\arrowhead"
Case "192.168.20"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\warner"
Case "192.168.50"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenfield"
Case "192.168.60"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\commercio"
Case "192.168.70"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\foothills"
Case "192.168.80"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\nwtucson"
Case "192.168.90"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenvalley"
Case "192.168.100"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\stmarys"
Case "192.168.200"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\corporate"
Case "192.168.205"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\pinnacle"
End Select
End If
Function GetClientIP()
Dim aOctets
Dim WSHNetwork
Dim strUserName
Dim sClient
Set WSHNetwork = WScript.CreateObject("WScript.Network")
While UserName = ""
strUserName = WSHNetwork.UserName
Wend
sClient = ""
Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(1)
For Each objSession In objFarm.Sessions
if strUserName = objSession.UserName then
sClient = objSession.ClientAddress
end if
Next
aOctets = Split(sClient, ".")
If aOctets(0) <> "" Then
GetClientIP = aOctets(0) & "." & aOctets(1) & "." & aOctets(2)
End If
End Function
ASKER
it showed just the username
My fingers are crossed - let me know if the script above worked...
ASKER
well, no errors, but it didn't map the dirve :(
I have made a little change to this script. If the user names match, the script will try and display your IP address.
Let me know if it does. If there are no errors and no popup, then I need to look harder at the usernames.
Let me know if it does. If there are no errors and no popup, then I need to look harder at the usernames.
Set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next
strClientIP = GetClientIP()
if strClientIP <> "" then
strSubnet = strClientIP
end if
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'Y:'")
If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.9"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\estrella"
Case "192.168.10"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\arrowhead"
Case "192.168.20"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\warner"
Case "192.168.50"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenfield"
Case "192.168.60"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\commercio"
Case "192.168.70"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\foothills"
Case "192.168.80"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\nwtucson"
Case "192.168.90"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\greenvalley"
Case "192.168.100"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\stmarys"
Case "192.168.200"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\corporate"
Case "192.168.205"
objNetwork.MapNetworkDrive "Y:", "\\ventsvr\kryptiq\pinnacle"
End Select
End If
Function GetClientIP()
Dim aOctets
Dim WSHNetwork
Dim strUserName
Dim sClient
Set WSHNetwork = WScript.CreateObject("WScript.Network")
While UserName = ""
strUserName = WSHNetwork.UserName
Wend
sClient = ""
Set objFarm = CreateObject("MetaFrameCOM.MetaFrameFarm")
objFarm.Initialize(1)
For Each objSession In objFarm.Sessions
if lcase(strUserName) = lcase(objSession.UserName) then
sClient = objSession.ClientAddress
end if
Next
wscript.echo sClient
aOctets = Split(sClient, ".")
If aOctets(0) <> "" Then
GetClientIP = aOctets(0) & "." & aOctets(1) & "." & aOctets(2)
End If
End Function
ASKER
No, it didn't do anything at all. The original code I posted was to map domain workstations. Is all of that code still needed since this will be a seperate script just for terminal server? I have a feeling the orginal code and your code is conflicting some how.
Hmmm, could be.
I was thinking that this script would only run on the terminal server. I will look at this more tomorrow.
I was thinking that this script would only run on the terminal server. I will look at this more tomorrow.
ASKER
Thanks for your time.
ASKER
I made some progress here. I downloaded the attached zip file. registered the dll. ran the vbs script and it returned an ip address on my terminal server. I finally succesfully pulled the clients ip address using this dll. Now, is there a way we can add the script found in the attached file and segment the octets of the ip address that script return to map the drive? I think this will work!
wtsclient.zip
wtsclient.zip
Just my luck - I cannot open the ZIP file. Could you post the script and I will see what can be done.
ASKER
This only works if you have th DLL registered.
set oWts = CreateObject("WtsScript.Wt sQuery")
WScript.Echo oWts.ClientIp
The echo returns the proper client ip address.
set oWts = CreateObject("WtsScript.Wt
WScript.Echo oWts.ClientIp
The echo returns the proper client ip address.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OMG, You are the man!!! I wish I could award you 10,000 points, that was some effort on your behalf. Admins, give this guy some extra points.
ASKER
I accidently gave you a B. Do you know if that can be changed to an A?
I have no idea. Don't worry about changing the score, I'm just glad everything is working at your end.
I appreciated the comments above :-)
I appreciated the comments above :-)
http://www.ctrl-alt-del.com.au/CAD_Utils.htm#Freeware
Use envtscip.exe to setup an environment variable ClientIP