VB script to get VirtualIP

Add a comment January 26th, 2011

I built this script to get the virtual ip of the usersession as i needed to send that to the application when the user started. I built two versions of it one using mfcom, the user needs to have certain admin rights in the farm to get this going which I dont like, and the other uses WMI and the registry to figure it out. Its not perfect and should be modified to fit your situation. It will only get the last sessions virtualip, as thats all i needed. So if the user has 2 sessions going you will only get the last one of them.

It is very not documented as i was in abit of rush writing it. Sorry for that! =)

** Updated with bug fix discussed in the comments thnx Augustin **

Here is the script (sesInfo.vbs):

‘ Written by Roger Eklund (roger.eklund at envokeit.com)
‘ Use at your own risk!
‘ To get the Client Virtual IP run “sesInfo.vbs ip”
‘ To get the Client hostname run “sesInfo.vbs clientname”

Const HKCR = &H80000000 ‘HKEY_CLASSES_ROOT
Const HKCU = &H80000001 ‘HKEY_CURRENT_USER
Const HKLM = &H80000002 ‘HKEY_LOCAL_MACHINE
Const HKU  = &H80000003 ‘HKEY_USERS
Const HKCC = &H80000005 ‘HKEY_CURRENT_CONFIG
Const REG_SZ        =1
Const REG_EXPAND_SZ =2
Const REG_BINARY    =3
Const REG_DWORD     =4
Const REG_MULTI_SZ  =7
Const MetaFrameWinFarmObject = 1
Const MetaFrameWinSrvObject = 6
Const Active=1
Const Connected=2
SessionID=GetSessionID

args = WScript.Arguments.Count

If args < 1 then
WScript.Echo “usage: sesInfo.vbs [ip|clientname]”
wscript.echo “Will get either VirtualIP of the client”
wscript.echo “or the Clientname”
WScript.Quit
end If

choice = ucase(wscript.arguments.item(0))

Select Case choice
Case “CLIENTNAME”
hkey=HKU
Set oReg = GetObject( “winmgmts:!root/default:StdRegProv” )
curUser = GetCurrentUser(“.”)
uSid = GetSIDFromUser(curUser)
wscript.echo vbCrLf & GetVal(hkey,uSid+”\Volatile Environment” , “CLIENTNAME”)
Case “IP”
Set oReg = GetObject( “winmgmts:!root/default:StdRegProv” )
hkey=HKLM
path = “SOFTWARE\Citrix\VIP\” & SessionID
Vip = GetVal(hkey,path , “VIPAddress”)
wscript.echo hex2ip(Vip)
Case Else
wscript.echo “omg”
End Select
Function hex2ip(hex)
Dim oct1
Dim oct2
Dim oct3
Dim oct4
Dim IPadress

If len(hex) = 7 Then
hex = “0″ & hex
end if

if len(hex) > “8″ or len(hex) < “8″ Then
wscript.echo “Not a Hex number: ” & hex
wscript.quit(0)
end if

oct1 = (Mid(hex, 1, 2))
oct2 = (Mid(hex, 3, 2))
oct3 = (Mid(hex, 5, 2))
oct4 = (Mid(hex, 7, 2))
hexNumber1 = “&H” & oct1
hexNumber2 = “&H” & oct2
hexNumber3 = “&H” & oct3
hexNumber4 = “&H” & oct4
int1 = CLng(hexNumber1)
int2 = CLng(hexNumber2)
int3 = CLng(hexNumber3)
int4 = CLng(hexNumber4)
hex2ip = int1 & “.” & int2 & “.” & int3 & “.” & int4
End Function
Function convertIP(BinIP)

Dim x
Dim Num
Dim IPNumeric
Dim IPAddy

IPNumeric = BinIp

For x = 1 To 4

Num = int(IPNumeric / 255 ^ (4 – x))
IPNumeric = IPNumeric – (Num * 255 ^ 4 – x)

If Num > 255 Then
IPConvert = “0.0.0.0″
wscript.echo(“fail” & Num)
wscript.quit
End If

If x = 1 Then
IPAddy = Num
Else
IPAddy = IPAddy & “.” & Num
End If

Next
convertIP=IPAddy

End Function
Function GetVal( strHive, strTree, strKey )

‘ Read all keys and their values for the entire section into an array
If oReg.EnumValues( strHive, strTree, sKeys, iKeyType ) Then
GetVal = “-None-”
Else
For Count = 0 to UBound( sKeys )
‘ Select the requested key
If strKey = sKeys( Count )  Then
‘ Format the output

Select Case iKeyType( Count)
Case REG_SZ
oReg.GetStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sValue
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue strHive, strTree, sKeys( Count ), sValue
GetVal = sValue
Case REG_BINARY
oReg.GetBinaryValue strHive, strTree, sKeys( Count ), aValue
GetVal =  Join( aValue,”" )
Case REG_DWORD
oReg.GetDWORDValue strHive, strTree, sKeys( Count ), lValue
GetVal = hex(lValue)
Case REG_MULTI_SZ
oReg.GetMultiStringValue strHive, strTree, sKeys( Count ), sValue
GetVal =  Join( sValue,”" )
End Select
End If
Next
GetVal = GetVal
End If
End Function

Function GetCurrentUser(sstrComputer)
On Error Resume Next
Dim sstrKeyPath,sstrCU,sstrCUD,sstrCurrentUser,sstrCurrentUserDomain
sHKEY_LOCAL_MACHINE = &H80000002
Set sobjRegistry = GetObject(“winmgmts:\\” & sstrComputer & “\root\default:StdRegProv”)
sstrKeyPath = “SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon”
sstrCU = “DefaultUserName”
sstrCUD = “DefaultDomainName”
sobjRegistry.GetStringValue sHKEY_LOCAL_MACHINE, sstrKeyPath, sstrCU, sstrCurrentUser
sobjRegistry.GetStringValue sHKEY_LOCAL_MACHINE, sstrKeyPath, sstrCUD, sstrCurrentUserDomain
GetCurrentUser = sstrCurrentUserDomain & “\” & sstrCurrentUser
End Function
Function GetSIDFromUser(UserName)

Dim DomainName, Result, WMIUser
If InStr(UserName, “\”) > 0 Then
DomainName = Mid(UserName, 2, InStr(2,UserName, “\”) -2)
UserName = Mid(UserName, InStr(2, UserName, “\”) + 1)
Else
DomainName = CreateObject(“WScript.Network”).UserDomain
End If
On Error Resume Next
Set WMIUser = GetObject(“winmgmts:{impersonationlevel=impersonate}!” _
& “/root/cimv2:Win32_UserAccount.Domain=’” & DomainName & “‘” _
& “,Name=’” & UserName & “‘”)
If Err = 0 Then Result = WMIUser.SID Else Result = “”
On Error GoTo 0
GetSIDFromUser = Result
End Function
Function GetSessionID()
Dim TheFarm, TheSession, TheSession2
Dim sessionID, strComputerName
SessionID = -1

Set objWSHNet = CreateObject(“WScript.Network”)
strServerName = objWSHNet.ComputerName
Set TheFarm = CreateObject(“MetaFrameCOM.MetaFrameFarm”)
theFarm.Initialize(MetaFrameWinFarmObject)
Set TheSession = theFarm.GetSession(MetaFrameWinSrvObject, strServerName, SessionID)
strSessionID = TheSession.SessionID
GetSessionID = strSessionID
End Function

  1. October 20th, 2011 at 10:10 | #1

    Very useful.

    I found a small bug, on the session I want to trak IP is 10.184.126.166 but program returns 171.136.26.6

    Looking at the registry the value is AB881A6, I think the missing trailing 0 makes the hex2ip fail as it will try as first octet AB instead of 0A, second as 88 instead of B8 and so go on.

    Anyway, thanks because the information was very useful :)

  2. July 26th, 2012 at 05:07 | #2
    Nick Matese

    Does anyone have an updated script to return proper IP, I too am having the issue described by Agustin.

  3. September 6th, 2012 at 11:45 | #3
    gobba

    Thnx Agustin!

    Ive updated the script Nick.

  1. No trackbacks yet.
Comments feed