[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


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

Published on
7,864 Points
Last Modified:
Ron Malmstead
...it's been a while since I've been on Experts-Exchange.
Since I last used this site, a lot has changed...for the better!
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: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Q_22746765.html?sfQueryTermInfo=1+locat+login+script+vb

This is the solution you can employ so that your logon scripts can identify the subnet a computer exists on...in 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 - http://www.microsoft.com/downloads/details.aspx?FamilyID=DC2D3339-8F36-4FBA-A406-2A2A2AD7208C&displaylang=en)

Once you have successfully employed this method....you 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 (http://www.microsoft.com/technet/scriptcenter/csc/scripts/desktop/settings/cscds060.mspx)

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.

Expert Comment

@Echo off
Set Prompt=$D$T  $B$G
::Deployment Script
::Version: 11.3
::Created by: DuaneMizell (http://www.experts-exchange.com/M_4688963.html)
::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!
LVL 44

Expert Comment

by:Steve Knight
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


Featured Post

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
Learn how to collaborate with office 365 Office Online

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month