Login script that checks current IP address to a load certain script

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.

Script to Check Adobe Version and update if needed

Here you are Mike, I’ve played with a few scripts and had great results… but this one has to be my crowning achievement at this point.

Some background info first: We needed most of our PCs to have Adobe Reader version 8.1.4 installed. We have automatic updates for adobe blocked on the firewall, and we also couldn’t install 9.0 because it would not be supported by another piece of software. Some of the PCs still had adobe reader 7.0, most had 8.0 or 8.1. I wanted a script that got called in a login script and checked what version they had and ran a silent install of adobe 8.1.4 if needed.

First, I got myself a full exe of Adobe 8.1.3 (for some annoying reason you can only patch to 8.1.4 from 8.1.3, and there is no full install of 8.1.4). I used resources that adobe themselves provide to make a custom msi package and also set it to include the separate 8.1.4 patch. This all gets dumped into a directory on a network share that all users have access to.

I then added this line to the users login scripts (batch files) to call the script:

cscript “\\LocalServer\share\folder\adberdr81Silent\setup files\adobe814.vbs” /quiet /passive

This calls the script silently and does not ask the user if they want to run or whatever other BS safety messages Windows XP vomits at you normally when you run a file off the network.

Now the VBscript (script in blue, my comments for this post in normal):

Dim FilePath
Dim fso
Dim version
Dim WshShell

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

If objFSO.FileExists(“c:\program files\adobe\reader 8.0\reader\acrord32.exe”) Then
Set objNet = WScript.CreateObject(“WScript.Network”)
strCaption = “Adobe Reader”
Set objWMI = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2”)
For Each objSoftware in objWMI.ExecQuery(“SELECT * FROM Win32_Product Where Caption Like ‘%” & strCaption & “%'”)
version = objSoftware.Version
Next

^ This says look to see if the folder for adobe 8.0 exists on the local C:, and if it does, it searches the registry for what exact version it is and stores it in the “version” variable. If the folder for 8.0 does not exist, the script can assume that either 7.0 is installed, or no version of adobe is installed and will run the setup in the “else” option below:

Else

set objWshShell= CreateObject(“Wscript.Shell”)
set objEnviroment = objWshShell.Environment(“PROCESS”)
objEnviroment (“SEE_MASK_NOZONECHECKS”) = 1

^ This changes the registry so that it will disable those annoying open file warnings during the script

Set WshShell = WScript.CreateObject(“WScript.Shell”)
WScript.Echo WshShell.CurrentDirectory
WshShell.CurrentDirectory = “Y:\folder\adberdr81Silent\Setup Files”

^ This changes the running directory (I had trouble with some stations thinking I was trying to run setup.exe out of the windows directory, this fixed that) Y: is mapped to the “share” on the local server.

Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “setup.exe”

set objWshShell= CreateObject(“Wscript.Shell”)
set objEnviroment = objWshShell.Environment(“PROCESS”)
objEnviroment (“SEE_MASK_NOZONECHECKS”) = 0

WScript.quit

^ This calls the setup.exe located in that folder and the custom adobe msi I made to install completely silently (among other custom settings) takes it from here. Then the script undoes the change I made to disable the open file warnings and exits the script. Adobe 8.1.4 is now installed.

End If

If version = “8.1.4” Then

WScript.quit

^ Here we have the other option, the script finds the adobe 8.0 directory and is now going to check that version variable. For my purposes, checking if the version is 8.1.4 (highest version of reader 8 ) is what I needed. If that version is already installed the script quits and does nothing further.

Else

set objWshShell= CreateObject(“Wscript.Shell”)
set objEnviroment = objWshShell.Environment(“PROCESS”)
objEnviroment (“SEE_MASK_NOZONECHECKS”) = 1

Set WshShell = WScript.CreateObject(“WScript.Shell”)
WScript.Echo WshShell.CurrentDirectory
WshShell.CurrentDirectory = “Y:\folder\adberdr81Silent\Setup Files”

Set WshShell = WScript.CreateObject(“WScript.Shell”)
WshShell.Run “setup.exe”

set objWshShell= CreateObject(“Wscript.Shell”)
set objEnviroment = objWshShell.Environment(“PROCESS”)
objEnviroment (“SEE_MASK_NOZONECHECKS”) = 0

WScript.quit

end if

^ Otherwise, if Adobe reader 8 is found, but does not equal version 8.1.4, then the script updates it to the version required by running the silent setup. From that point the setup.exe is smart enough to just install the patch(es) needed. Adobe made these easy to customize.

So there you have it. This script was added to the login process and if the PC didn’t already have Adobe Reader 8.1.4, it would install it. Otherwise it would not do anything. Either way the end user had no idea it was happening. I’m sure this script is sloppy, but it did exactly what I needed it to do – and saved Celeste and I a shitload of tedious work – , so I was pleased.

-Dave out