How-to set the subnet of a computer, to a system variable for use in logon scripts.

Ron MalmsteadSr. Developer
Often times network admins need to run domain logon scripts for seperate subnets, but also for a seperate set of computers within that subnet.  In this circumstance, configuring Active Directory Sites and Services then linking a GPO to site/subnet might not be an option.

So now what ?

This is a real world example of this situation and my accepted solution:

This is the solution you can employ so that your logon scripts can identify the subnet a computer exists order to decide whether to run or terminate.  Currently the script is only designed for class C network identification.

You need two things to make this work...
1) The attached vbscript (paste into notepad and save as *.vbs)
2) A program called setx.exe (Found in the MS Resource kit -

Once you have successfully employed this can use the conditional code in your logon scripts using the subnet as a system variable.
Example: The subnet is 192.168.6, which was set as a variable %IPNETID%, and I want a script to only run if that is the subnet the machine is presently on...
Therefore the first line on the logon batch script would be the following....

IF NOT %IPNETID% == 192.168.6 EXIT

or you could use this...

IF %IPNETID% == 192.168.6 net use m: "\\server\share"

This is my original solution which was published on Microsoft Script Center repository - Technet (

Now when you move machines between subnets, the proper logon scripts will run automatically after the first reboot.

NOTE: One caveat i've found, is that this script when used as a startup script will sometimes set the system variable only after the second reboot/logon, on a newly domain-joined machine.  I believe this is because of machine authentication reasons... not sure though.  Make sure the setx file is accessible to all machines, and that this script is set to run prior to other scripts running because the variable needs to be set prior to being used as a condition in your other logon scripts.
Ron MalmsteadSr. Developer

Comments (3)

Ron MalmsteadSr. Developer


@Echo off
Set Prompt=$D$T  $B$G
::Deployment Script
::Version: 11.3
::Created by: DuaneMizell (
::Created: 12/2009
::Modified: 03/2012
for /f "tokens=2 delims=[]" %%i in ('pathping -p 1 -q 1 -4 %computername%') do set IP=%%i
for /f "tokens=2 delims=." %%i in ('echo %IP%') do set OCTET2=%%i
set strIP=%OCTET2%
if ["%strIP%"]==["14"] (
echo 14th Floor IP address...
goto 14Office)
if ["%strIP%"]==["15"] (
echo 15th Floor IP address...
goto 15Office)
if ["%strIP%"]==["16"] (
echo 16th Floor IP address...
goto 16Office)
if ["%strIP%"]==["18"] (
echo 18th Floor IP address...
goto 18Office)
if ["%strIP%"]==["23"] (
echo 23trd Floor IP address...
goto 23Office)
if ["%strIP%"]==["24"] (
echo 24th Floor IP address...
goto 24Office)
if ["%strIP%"]==["25"] (
echo 25th Floor IP address...
goto 25Office)

Echo Not a good IP address: 10.%strIP%.x.x (This computers IP address is: %IP%) Automatically pulling files from home office!!!

goto DefaultInstall

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer1\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer2\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer3\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer4\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer5\SomeFolder
goto Install

@Echo On
::DO: Stuff

set InstallPath="\\SomeServer6\SomeFolder
goto Install

@Echo on
::DO: Stuff

set InstallPath="\\SomeServer00\SomeFolder
goto Install

rem Insert install code here. Use the variable "%InstallPath%" (without quotes)  to call the local office path to \\SomeServerX\SomeFolder
rem Example for an installation of Whatever.MSI located in \\SomeServerX\SomeFolder:
rem msiexec /package %InstallPath%\SomeFolder\Whatever.MSI" /passive /norestart
REM Insert install code below:

goto eof
Set Prompt=$P$G
echo Done!
Steve KnightIT Consultancy

Another easy way to get the parts of IP address during the login script, along similar lines:

@echo off
for /f "tokens=2 delims=:" %%a in ('ipconfig ^|find "IP Address"') do call :process %%a & goto :next
echo Subnet is %subnet% and host is %host%
set site=Unknown
if %network%==10 set site=SiteA
if %network%==20 set site=SiteB
if %network%==30 set site=SiteC
if %network%==40 set site=SiteD
if %subnet%==192.168.1 set site=SiteE
if %subnet%==128.127.1 set site=Dragon-IT

echo site is %site%

goto :Eof

echo Found %1
for /f "tokens=1-4 delims=." %%a in ("%1") do (set subnet=%%a.%%b.%%c)&(set host=%%d)&(set network=%%c)

Open in new window

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.