Check which of many network domains the current machine is in then run a script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Check which of many network domains the current machine is in then run a script

#1 Post by thudo » 10 Feb 2022 09:10

Morning everyone..

Trying to script the following in DOS BATCH but its not turning out as I wished.

In our network we have three major domains, DEV, QA, and PRD and for each we also have sub-domains in all three so an example:

DEV
dev.mydevdomain1.net
dev.mydevdomain2.net
dev.mydevdomain3.net

QA
qa.myqadomain1.net
qa.myqadomain2.net
qa.myqadomain3.net

PRD
prd.myprddomain1.net
prd.myprddomain2.net
prd.myprddomain3.net

What we want to do is first determine the network domain which the current machine where the dos batch script is current on so using the env variable %USERDNSDOMAIN% I get:

dev.mydevdomain1.net

So I figured I could do the following using a wildcard but its not working as expected:

Code: Select all

IF %USERDNSDOMAIN% == *.mydevdomain1.net (echo "DEV 1 FOUND") ELSE IF %USERDNSDOMAIN% == *.mydevdomain2.net (echo "DEV 2 FOUND") ELSE IF %USERDNSDOMAIN% == *.mydevdomain3.net (echo "DEV 3 FOUND") ELSE (echo "DEV NOT FOUND)
In the end we want to detect the current DEV, QA, or PRD domain and copy a file.. so like the code example above this is our ultimate goal:

If current machine is in either *.mydevdomain1.net, *.mydevdomain2.net, *.mydevdomain3.net OR *.myqadomain1.net, *.myqadomain2.net, *.myqadomain3.net THEN copy X file ELSE IF machine is in *.myprddomain1.net, *.myprddomain2.net, *.myprddomain3.net THEN copy Y file.

Note the wildcards where they are.. that is somewhat important.

Any help would be most appreciated. Thank you community!
Last edited by thudo on 10 Feb 2022 20:10, edited 3 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check which network domain current machine is in

#2 Post by aGerman » 10 Feb 2022 12:05

So, you want to get the substring after the underscore?

Code: Select all

for /f "tokens=2 delims=_" %%i in ("%%") do if "%%i"=="mydevdomain1.net" ...
Steffen

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Check which network domain current machine is in

#3 Post by Aacini » 10 Feb 2022 12:37

You can not use an asterisk (or question-mark) as a "wild-card" for strings in Batch files.

Perhaps this works?

Code: Select all

for /F "tokens=1,2 delims=_." %%a in ("%USERDNSDOMAIN%") do (
   set "major=%%a"
   set "sub=%%b"
)
set "sub=%sub:~-1%"
echo %major% %sub% found

if %major% == dev copy Xfile ...
if %major% == qa  copy Xfile ...
if %major% == pr  copy Yfile ...
Antonio

thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Re: Check which network domain current machine is in

#4 Post by thudo » 10 Feb 2022 13:50

aGerman wrote:
10 Feb 2022 12:05
So, you want to get the substring after the underscore?

Code: Select all

for /f "tokens=2 delims=_" %%i in ("%%") do if "%%i"=="mydevdomain1.net" ...
Yep so anything after the _ or . is what we need.. what comes before it could be anything actually using the DEV environment as example..

dev1_mydevdomain1.net
dev2_mydevdomain2.net
dev3_mydevdomain3.net

this was why I tried searching the %USERDNSDOMAIN% for a wildcard *mydevdomain1.net for example as the dev1,2,3 should be the wildcard itself - its what comes after ie. mydevdomain1.net that is more important.

thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Re: Check which network domain current machine is in

#5 Post by thudo » 10 Feb 2022 13:55

Aacini wrote:
10 Feb 2022 12:37
You can not use an asterisk (or question-mark) as a "wild-card" for strings in Batch files.

Perhaps this works?

Code: Select all

for /F "tokens=1,2 delims=_." %%a in ("%USERDNSDOMAIN%") do (
   set "major=%%a"
   set "sub=%%b"
)
set "sub=%sub:~-1%"
echo %major% %sub% found

if %major% == dev copy Xfile ...
if %major% == qa  copy Xfile ...
if %major% == pr  copy Yfile ...
Thank you Antonio! That sorta works but is only picking up the dev1 or dev2 or dev3 in _mydevdomain1.net and needs to pick up everything after the dev1, dev2, dev3, qa1, etc..

Btw, one clarification.. it should be a "." instead if "_".. sorry about that. So what the sub-domain in each section should actually be:

DEV
dev.mydevdomain1.net
dev.mydevdomain2.net
dev.mydevdomain3.net

QA
qa.myqadomain1.net
qa.myqadomain2.net
qa.myqadomain3.net

PRD
prd.myprddomain1.net
prd.myprddomain2.net
prd.myprddomain3.net

thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Re: Check which network domain current machine is in

#6 Post by thudo » 10 Feb 2022 19:55

Update..

With some help I sort of have a semi-solution..

Code: Select all

for %%s in (mydevdomain1.net mydevdomain2.net mydevdomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto DEV
:DEV
Echo "DEV Environment Found"

for %%s in (myqadomain1.net myqadomain2.net myqadomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto QA
:QA
Echo "QA Environment Found"

for %%s in (myprddomain1.net myprddomain2.net myprddomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto PRD
:PRD
Echo "PRD Environment Found"
Problem with the above is as follows:

1) If I purposefully misname the domain I am in within the script so its now "mydevdomain0.net" (I am in "mydevdomain1.net" btw) so it should not find it and omit it still echos it at the goto under :DEV echo "DEV Environment found" whereas it should not have as none of the entries are now valid (this was just a test), and

2) If I add a whole other section just for QA it still evaluates all of it even though none of the %userdnsdomain% are valid as I am not in the QA domain - yet it evaluates them all + echos it. It should not as no QA domains are valid since I am testing in the DEV domain. :(

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Check which of many network domains the current machine is in then run a script

#7 Post by Squashman » 10 Feb 2022 23:03

Batch files execute each line in the script from top to bottom. Regardless of the goto, once the for command is done executing it is going to execute the next command in the script.

thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Re: Check which of many network domains the current machine is in then run a script

#8 Post by thudo » 10 Feb 2022 23:10

Thanks Squashman.. guess this batch logic needs to first find the correct name (skipping over other searches if needed) then, once done, skip over all to the end.

thudo
Posts: 6
Joined: 10 Feb 2022 08:48

Re: Check which of many network domains the current machine is in then run a script

#9 Post by thudo » 16 Feb 2022 21:35

Update on this.. with further help I now have this which checks a txt file will all the fully qualified domain names and pipes it to a value %majordomain%

The file

Code: Select all

domainlist.txt
contains the following as an example:

Code: Select all

dev dev1.lab.mydevdomain1.net
dev dev2.lab.mydevdomain2.net
dev dev3.lab.mydevdomain3.net
dev dev4.lab.mydevdomain4.net
qa qa1.qa.myqadomain1.net
qa qa2.qa.myqadomain2.net
qa qa3.qa.myqadomain3.net
prd prd1.prd.myprddomain1.net
prd prd2.prd.myprddomain2.net
prd prd3.prd.myprddomain3.net
Primary Code launched via CMD:

Code: Select all

@ECHO OFF
SETLOCAL
FOR %%u IN (
 lab.mydevdomain1.net lab.mydevdomain2.net lab.mydevdomain3.net
 lab.mydevdomain4.net qa.myqadomain1.net qa.myqadomain2.net
 qa.myqadomain3.net prd.myprddomain1.net prd.myprddomain2.net
 prd.myprddomain3.net
) DO SET %USERDNSDOMAIN%=%%u& CALL :process

GOTO :EOF

:process

SET "majordomain="
for /f "delims= " %%s in ( 'findstr /e /i /L /c:" %userdnsdomain%" domainlist.txt 2^>nul') DO IF DEFINED majordomain (SET "majordomain=ambiguous") ELSE (SET "majordomain=%%s")

ECHO "%userdnsdomain%" --^> majordomain=%majordomain%

if %majordomain% == dev (echo "DEV found") else (echo "DEV not found")
if %majordomain% == qa (echo "QA found") else (echo "QA not found")
if %majordomain% == prd (echo "PRD found") else (echo "PRD not found")
So, for example, if I am in the fully qualified dev4.lab.mydevdomain4.net and I run this I will get (not case sensitive btw which is more flexible):
"dev4.lab.mydevdomain4.net" --> majordomain=dev
"DEV found"
"QA not found"
"PRD not found"
So if DEV is found then I should be able to run a specific command line just for DEV. :)

Now the only challenge is I need to fully find all the qualified names to fill domainlist.txt as I originally only wanted to use wildcards like *.lab.mydevdomain1.net so the first section is the wildcard and the last parts of the domain are what is known. So it is not there fully but it is real close.

Post Reply