DOS batch files are usually very simple, but it's possible to include some logic. The CMD.EXE language is pretty limited, but it works. (This is CMD.EXE not PowerShell, which is much nicer.) Here's an example of using IF to select a user login.
This is for a Buffalo terastation, which has a simplistic integration with Active Directory: it doesn't have file-level security, just share-level security. I happened to want to do something that's kind of "wrong" on a regular domain member server: share something widely, but restrict writing to specific domain users. The way to do this is to join the NAS to the domain, then give read/write access to the relevant users, then have everyone else log in as the local guest account. That guest account has no password.
set puser=workgroup\guest if %USERNAME% EQU jkawakami set puser=windowsdomainname\jkawakami net use p: /d /y net use p: \\terastation\share /user:%puser%
puser holds the name of the user we send to the terastation. Normally, it's workgroup\guest, but if the username is jkawakami, the puser gets set to jkawakami's domain login.
If this were coded in a real language, the logic would look like this:
net use p: /d /y if (%USERNAME%=="jkawakami") { net use p: \\terastation\share } else { net use p: \\terastation\share /user:workgroup\guest }
Basically, with the old DOS command line interface scripts, you use temporary variables to set parameters, and use logic to set those parameters.
Another technique is to use the GOTO statement to skip over some lines of code. Here are two blocks of code:
:BLOCKA REM block a code goes here GOTO END :BLOCKB REM block b code goes here GOTO END :END
Here it is with some logic added:
RUNB=0 if %USERNAME% EQU johnk set RUNB=1 if RUNB EQU 1 GOTO BLOCKB :BLOCKA REM block a code goes here GOTO END :BLOCKB if RUNB NEQ 1 GOTO END REM block b code goes here GOTO END :END
Ouch. That's giving me memories of BASIC.
Here's some untested code that might work, and is slightly better. Just came to me as I reviewed the article.
set JUMPTO=END if %USERNAME% EQU johnk set JUMPTO=BLOCKB goto %JUMPTO% goto END :BLOCKA if %JUMPTO% NEQ BLOCKA goto REM block a code goes here goto END :BLOCKB if %JUMPTO% NEQ BLOCKB 1 goto END REM block b code goes here goto END :END
This new code sets the JUMPTO value in the logic, so you can see where you jump.
The "if" statements at the top of each block help guard against accidentally executing the code.
The goto END at the bottom of the top block guards against accidentally runnning BLOCKA's code.
This code reminds me of why flowchart diagrams were used.