When using VBScript in a network environment you sometimes want to retrieve the IP
Address
to use it, for example, to retrieve the correct server from which you want to
install software.
This isn't an easy task. The IP address isn't set in the environment when the computer
boots, so we've got to use another method to retrieve the address.
The method which I use in the script below is reading the IP address from the
CurrentControlSet
in the registry. This makes sure that, even when you use DHCP, you get the right IP address.
The script contains two functions: GetIPAddress
and GetIPOctet
. With
GetIPAddress
you get an array
which contains the IP address. Remember that element 0 in the array is actually the first octet of
your IP address.
wscript.echo GetIPOctet (1)
Above you find a little example which prints out the first octet of the IP address.
Here you find the complete script as it is now. Include it in your own programs at your own risk.
Always be careful, since you are reading the registry.
Function GetIPAddress
Dim key
Dim cTempIPAddress
Dim cIPAddress
dim cIPAddressKey
Set oSh = CreateObject("WScript.Shell")
cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"
cNicServiceName=oSh.RegRead(cNICSearch)
cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
cTempIPAddress=oSh.RegRead (cIPAddresskey)
cIPAddress=split (cTempIPAddress(0),".",4)
GetIPAddress=cIPAddress
End Function
Function GetIPOctet (nOctet)
Dim IPAddress
IPAddress=GetIPAddress
GetIPOctet=IPAddress(nOctet-1)
End Function