I just thought the other day about how we have been using something very handy at work for quite some time now that I have never thought to post. We use a class C network, and each branch/location, as well as each floor of our corporate center, are on different subnets for the third octet of the IP address (EG: 123.123.XXX.xxx). This is a Windows domain, with XP pro loaded on the client stations.
Each branch has their own files as well as a core piece of software that loads off of network drives that are stored of that location’s local server. We use .BAT login scripts for each location that map the proper drives and whatever else is needed for that location. This was working fine, until we ran into the issue of users wandering to locations different than their home branch. They would sign in as themselves, which -never mind roaming profile issues-, would cause them to load the login script for their home location which would not be of much help at their present local.
My boss and I did a little research; and poached, and customized, some handy code of the interwebz that would allow us to give users a generic .VBS script in active directory that would check what network they were in and then call the appropriate batch file that would map their drives. The .VBS script is saved in the same replicated folder on the domain controllers as the rest of .BAT script files are.
Here’s the code, including the original author’s (of part of this script anyway) comments:
‘Go and get the IP address of the current machine
strcomputer=”.”
Set objWMIService = GetObject(“winmgmts:\\” & strcomputer & “\root\CIMV2”)
Set IPItems = objWMIService.ExecQuery (“Select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE”)
For Each IPConfig In IPItems
If Not IsNull(IPConfig.IPAddress) Then
For i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
If varIP=”” Then
varIP=IPConfig.IPAddress(0)
End If
Next
End If
Next
‘Split the IP address up into 4 separate parts and put it into an array
ArrayIP=Split(varIP,”.”)
‘Create a variable containing the 3rd octet of the IP address
varThirdOctet=ArrayIP(2)
varSecondOctet=ArrayIP(1)
‘Check value of varThirdOctet and run appropriate code
Select Case True
Case varThirdOctet=”100″
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “SCRIPTA.BAT”
Case varThirdOctet=”2″
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “SCRIPTB.BAT”
End Select
The initial portion of the code basically looks at the network adapter and reads it’s current assigned IP address. It then splits the four octets into an array that we can load into variables. It then checks this variable against the list of possible matches in the select case segment and calls the batch file associated with that network when it finds a match. You can put more than just two choices in the select case, obviously, but I used two for this example. Remember that when working with an array, the it starts with 0 and not 1, so the third octet of our IP address is in “part 2” of the array.
You may have noticed in the code above that I also added a variable for the second octet. This year I have been working (almost done actually) on changing our networks to conform to best practice. Our first two octets were not proper numbers for an LAN IP address, and many of our locations had incorrect and/or completely illogical numbers for the third octet as well. We are now using the actual branch number as the third octet for all of our branch numbers.
In working on this project, I ran into the issue that when changing certain branches over to the new network the third octet would be the same as another location still on the old network. This would pretty much break the script’s concept, as it would simply load the first case in line that had the matching number. I got around this problem by simply adding a check for the second octet (which was also changed when I flipped a branch over) that would differentiate between the two networks in the case of a duplicate third octet. In the example below, we look at both the second and third octets to match a certain value to load the proper script:
Case varThirdOctet=”11″ And varSecondOctet=”1″
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “SCRIPTC.bat”
Case varThirdOctet=”11″ And varSecondOctet=”2″
Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “SCRIPTD.bat”
Note that the third octet we’re looking at for both these example locations are the same but the second octet is different, so we use the “and” modifier to ensure that the right script is loaded for the right network. There is no reason why you cannot take this a step further and look at more of the octets by adding another variable and extending the cases, as the whole IP address is already loaded into the array “ArrayIP”.
Disclaimers: I have rather limited knowledge of VBscript (and programing in general) and mostly muddle my way through this stuff, so I am no expert, and you should not look at this script as though it were written by one. This is used in a Windows domain environment with a Windows 2003 or 2008 domain controller at each location with the client PCs that are loading the script being Windows XP SP2 or SP3. I have not yet tested this script with Windows 7, or any other operating systems other than the ones listed above. I did not write the above code completely myself, rather I retrieved most of it off of the internet and modified it with my limited understanding of VBscript to suit our needs. I do not know the names of any of the original authors.