'Check which current network domain machine is in and run a command based on which domain it resides
Morning everyone..
Trying to script the following in Windows 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
dev1.mydevdomain1.net,
dev2.mydevdomain2.net,
dev3.mydevdomain3.net,
dev4.lab.mydevdomain3.net
QA
qa1.myqadomain1.net,
qa2.myqadomain2.net,
qa3.test.myqadomain3.net,
qa4.myqadomain4.net
PRD
prd1.myprddomain1.net,
prd2.test.myprddomain2.net,
prd3.myprddomain3.net,
prd4.myprddomain4.net
What we want to do is first determine the network domain which the current machine where the Windows batch script is current on so using the env variable %USERDNSDOMAIN% I get:
dev1.mydevdomain1.net
So I figured I could do the following using a wildcard but its not working as expected:
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.
Solution 1:[1]
for /f "delims=_" %%m in ("%USERDNSDOMAIN%") do (
if /i "%%m"=="dev" (copy command[s] for dev)
if /i "%%m"=="qa" (copy command[s] for qa)
if /i "%%m"=="prd" (copy command[s] for prd)
)
See for /? from the prompt for how-it-works.
--- after original post edits
if /i "%userdnsdomain:*.=%"=="mydevdomain1.net" ECHO {copy command{s} for dev}
if /i "%userdnsdomain:*.=%"=="mydevdomain2.net" ECHO {copy command{s} for dev}
if /i "%userdnsdomain:*.=%"=="mydevdomain3.net" ECHO {copy command{s} for dev}
if /i "%userdnsdomain:*.=%"=="myqadomain1.net" ECHO {copy command{s} for qa}
if /i "%userdnsdomain:*.=%"=="myqadomain2.net" ECHO {copy command{s} for qa}
if /i "%userdnsdomain:*.=%"=="myqadomain3.net" ECHO {copy command{s} for qa}
if /i "%userdnsdomain:*.=%"=="myprddomain1.net" ECHO {copy command{s} for prd}
if /i "%userdnsdomain:*.=%"=="myprddomain2.net" ECHO {copy command{s} for prd}
if /i "%userdnsdomain:*.=%"=="myprddomain3.net" ECHO {copy command{s} for prd}
where the copy commands are echoed for testing
--- simplification, given comment and noting that the actual domains are highly unlikely to be mydevdomain1.net...
for %%s in (mydevdomain1.net mydevdomain2.net mydevdomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto dev
...and follow the bouncing ball.
---- My test setup
@ECHO OFF
SETLOCAL
FOR %%u IN (
dev1.mydevdomain1.NET dev2.mydevdomain2.NET dev3.mydevdomain3.NET
dev1.mydevdomain0.NET dev99.mydevdomainmissing.NET dev3.mywrongdomain3.NET
aq1.myQAdomain1.NET qa2.myqadomain2.NET qa3.myqadomain3.NET
prd1.myprddomain1.NET prd2.myprddomain2.NET prd3.myprddomain3.NET
) DO SET USERDNSDOMAIN=%%u&CALL :process
GOTO :EOF
:process
for %%s in (mydevdomain1.net mydevdomain2.net mydevdomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto dev
for %%s in (myqadomain1.net myqadomain2.net myqadomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto qa
for %%s in (myprddomain1.net myprddomain2.net myprddomain3.net) do if /i "%userdnsdomain:*.=%"=="%%s" goto prd
ECHO "%userdnsdomain%" --^> "%userdnsdomain:*.=%" NOT found
GOTO :eof
:dev
ECHO "%userdnsdomain%" --^> "%userdnsdomain:*.=%" is dev - DO dev copies
GOTO :eof
:qa
ECHO "%userdnsdomain%" --^> "%userdnsdomain:*.=%" is qa - DO qa copies
GOTO :eof
:prd
ECHO "%userdnsdomain%" --^> "%userdnsdomain:*.=%" is prd - DO prd copies
GOTO :eof
The %%u loop simply assigns various strings to userdnsdomain and runs the :process routine on each.
The report echoed for each shows what was tested, the derives string and the result in a form like
"dev1.mydevdomain0.NET" --> "mydevdomain0.NET" NOT found
--- a completely different approach
SET "majordomain="
for /f "delims= " %%s in (
'findstr /i /e /L /c:" %userdnsdomain%" q71068320.txt 2^>nul'
) DO IF DEFINED majordomain (SET "majordomain=ambiguous") ELSE (SET "majordomain=%%s")
ECHO "%userdnsdomain%" --^> majordomain=%majordomain%
This is the :process routine that the %%u loop could invoke in the test framework.
Initially, majordomain is set to nothing
The next 3 lines could be concatenated. I just showed them as 3 separate lines for ease of reading.
The findstr finds all lines in a file q71068320.txt (name unimportant. It's made up of the SO question number so I can keep various answers together) which /e End with the /L Literal /c:"string to find" and /i makes the match case-insensitive. Note that the string-to-find is Space%USERDNSDOMAIN%.
The file q71068320.txt contains (testing example)
dev dev1.mydevdomain1.NET
dev dev2.mydevdomain2.NET
dev dev3.mydevdomain3.NET
dev dev4.lab.mydevdomain1.net
qa aq1.myQAdomain1.NET
qa qa2.myqadomain2.NET
qa qa3.myqadomain3.NET
prd prd1.myprddomain1.NET
dev dev2.mydevdomain2.NET
That is, the major domain followed by a space and the actual domain name.
The 2^>nul suppresses error messages from findstr ('not found' etc.) - 2>nul would normally suppress error messages, but the ^ is required to tell cmd that the > belongs to the findstr, not the for.
The findstr output is then analysed by the for /f, using Space as a separator, and assigning the first "token" of the line found to %%s (default behaviour).
If a line is found, majordomain will be set to that first token on the line detected. If it has already been set, then more than one line matches in the file, so we have a potential duplicate.
When all matching lines have been found in the file, majordomain is set to the major domain specified in column 1 of the file, or to ambiguous if more than one line matches, or to nothing meaning 'nothing found'.
--- small adjustment ---
SET "majordomain="
FOR /f "tokens=1*delims=." %%b IN ("%userdnsdomain%") DO (
for /f "delims= " %%s in (
'findstr /e /L /c:" %%c" q71068320e.txt 2^>nul'
) DO IF DEFINED majordomain (SET "majordomain=ambiguous") ELSE (SET "majordomain=%%s")
)
ECHO "%userdnsdomain%" --^> majordomain=%majordomain%
ECHO GOTO do%majordomain%copy
where the file q71068320e.txt contains
dev mydevdomain1.NET
dev mydevdomain2.NET
dev mydevdomain3.NET
dev lab.mydevdomain1.net
qa myQAdomain1.NET
qa myqadomain2.NET
qa myqadomain3.NET
prd myprddomain1.NET
dev mydevdomain2.NET
Here, the for ... %%b tokenises the string in userdnsdomain', putting that part before the first .(the delimiter) into%%band the remainder of the string (token*) into %%c. Match for <kbd>Space</kbd>%%c in the revised file q71068320e.txt` (the initial part of the name has been removed).
Note that the goto command that is echoed shows goto dodevcopy for instance. docopy would be reached if majordomain is empty; doambiguouscopy if majordomain is set to ambiguous.
Personally, I don't like using keywords as labels, hence doXXXcopy.
... but while I was documenting this, a better method came to mind:
SET "majordomain="
for /f "delims= " %%s in (
'findstr /e /L /c:" %userdnsdomain:*.=%" q71068320e.txt 2^>nul'
) DO IF DEFINED majordomain (SET "majordomain=ambiguous") ELSE (SET "majordomain=%%s")
ECHO "%userdnsdomain%" --^> majordomain=%majordomain%
ECHO GOTO do%majordomain%copy
This avoids the extra for loop, and searches for "Space%userdnsdomain:*.=%" which is userdnsdomain with all characters up to the first . (*.) replaced by nothing
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
