Saturday, 14 May 2011

Creating a unified batch file for different environments

So I wanted one batch file that I could run everywhere at work or at home. And it would be able to detect where I am running it from.

I figured the best way to do this is to use batch labels, and then determine the SID of the machine that is running.

So first I need to determine the SID on the machine.

So here is a simple little Batch file GetSid.bat
   1: @echo off
   3: set GSID=UNKNOWN
   4: for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
   5:    if not "%%a"=="SID" (          
   6:       set GSID=%%a
   7:       goto :quit
   8:    )   
   9: )
  10: :quit
  11: if not [%1]==[SILENT] echo %GSID%

Call it like this

call GetSid.bat

So Here is the batch file:

   1: set MyWorkPC=S-1-5-21-111111111-3333333333-5555555555-7777
   2: set MyHomePC=S-1-5-21-222222222-4444444444-6666666666-8888
   4: if [%GSID%]==[%MyWorkPC%] call :WorkCheckins
   5: if [%GSID%]==[%MyHomePC%] call :HomeCheckins
   8: goto :EOF
   9: :WorkCheckins
  10: echo Do some work stuff
  11: goto :EOF
  14: goto :EOF
  15: :HomeCheckins
  16: echo Do some home stuff
  17: goto :EOF

Now everything is in one place and you can group common variables and then environmental specific commands

No comments:

Post a Comment