none
How to create a batch file to run programs based on OS. RRS feed

  • Question

  • I need to add a batch file to MDT, where I have two version of a program - one for Windows 7, one for Windows 8 - both x64. I've seen samples where programs install based on 32 or 64 bit, but these are both 64 bit. I just need a simple batch file to run the correct version of the program on the matching OS.... Win 7E or Win 8.1E. Then I can add the batch file to MDT. I know that MDT can run programs based on OS but here they prefer to have one installer for both versions, based on the IF statement scenario. Thanks!
    Wednesday, February 10, 2016 8:02 AM

Answers

  • Here's pretty much how I used my batch file. This should give you an idea of how to adapt it to your needs.

    @echo off
    
    REM *****************************************************
    REM * Purpose: Configure system for home use	  	*
    REM * Scripted to work on all 32bit and 64bit systems	*
    REM * Author: Daniel Vega	 			*
    REM *****************************************************
    
    REM *********************************************************************
    REM * Apply the Microsoft Office MAK and activate the license		*
    REM *********************************************************************
    IF EXIST "%systemdrive%\program files (x86)\Microsoft Office\Office15\Winword.exe" goto :Office15x64
    IF EXIST "%systemdrive%\program files\Microsoft Office\Office15\Winword.exe" goto :Office15x86
    IF EXIST "%systemdrive%\program files (x86)\Microsoft Office\Office14\Winword.exe" goto :Office14x64
    IF EXIST "%systemdrive%\program files\Microsoft Office\Office14\Winword.exe" goto :Office14x86
    
    
    :Office15x64
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /act
    goto :Firefox
    
    :Office15x86
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /act
    goto :Firefox
    
    :Office14x64
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office14\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office14\ospp.vbs" /act
    goto :Firefox
    
    :Office14x86
    cscript "%systemdrive%\Program Files\Microsoft Office\Office14\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files\Microsoft Office\Office14\ospp.vbs" /act
    goto :Firefox
    
    
    REM ****************************
    REM * Set Firefox preferences. *
    REM ****************************
    :Firefox
    IF EXIST "%systemdrive%\program files (x86)\Mozilla Firefox\firefox.exe" copy "%CD%\mozilla.cfg" "%systemdrive%\Program Files (x86)\Mozilla Firefox" /Y
    IF EXIST "%systemdrive%\program files\Mozilla Firefox\firefox.exe" copy "%CD%\mozilla.cfg" "%systemdrive%\Program Files (x86)\Mozilla Firefox" /Y
    goto :WinAct
    
    
    REM ************************************************************
    REM * Apply the Microsoft Windows MAK and activate the license *
    REM ************************************************************
    :WinAct
    REM Check Windows Version
    ver | findstr /i "6\.1\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win7
    ver | findstr /i "6\.2\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win8
    ver | findstr /i "6\.3\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win81
    goto warn_and_exit
    
    :ver_Win7
    :Run Windows 7 specific commands here
    cscript %windir%\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    NET STOP "SOFTWARE PROTECTION"
    NET START "SOFTWARE PROTECTION"
    cscript %windir%\system32\slmgr.vbs /ato
    goto end
    
    :ver_Win8
    :Run Windows 8 specific commands here
    cscript %windir%\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    NET STOP "SOFTWARE PROTECTION"
    NET START "SOFTWARE PROTECTION"
    cscript %windir%\system32\slmgr.vbs /ato
    goto end
    
    :ver_Win81
    REM *** Removed, because the MAK is now set in the task sequence for 8.1 home machines.
    goto end
    
    :warn_and_exit
    echo Machine OS cannot be determined. License has not been activated
    Pause
    
    :End
    exit


    If this post is helpful please vote it as Helpful or click Mark for answer.

    • Proposed as answer by David Bloomer Wednesday, February 10, 2016 8:54 PM
    • Marked as answer by the1rickster Tuesday, February 16, 2016 3:25 AM
    Wednesday, February 10, 2016 8:02 PM
  • If you want to leverage MDT:

    I usually import a x86 installer and then check the x86 boxes in MDT of the OS' that it is compatible with. I mark the imported application as hidden. Then I do the same for x64. Then I add a new "application bundle" and add these two apps as dependencies. Now MDT will do all the work for you of determining which installer to use depending on the OS that is running.


    If this post is helpful please vote it as Helpful or click Mark for answer.

    • Proposed as answer by David Bloomer Wednesday, February 10, 2016 8:54 PM
    • Marked as answer by the1rickster Tuesday, February 16, 2016 3:26 AM
    Wednesday, February 10, 2016 8:31 PM

All replies

  • Hi,

    Find Your answer on below link and threads:

    https://community.spiceworks.com/topic/971547-running-cmd-or-bat-via-mdt-with-admin-rights

    https://social.technet.microsoft.com/Forums/en-US/51985319-683d-4ba5-854d-4e61eaa6c0dc/running-a-batch-file-in-a-task-sequence?forum=mdt

    Other ref:

    https://social.technet.microsoft.com/Forums/en-US/490bdde1-ee4e-4563-85ee-a4ca7f6d3a95/application-install-from-batch-file-in-mdt?forum=mdt

    Thanks,

    Mayank Dhama

    -------------------------------------

    Mark as answer if this helps You!


    Mayank Dhama

    Wednesday, February 10, 2016 8:26 AM
  • I'm already running other batch files in MDT. This is not an issue. I don't need to know how to run them with admin rights. I was asking how to create a batch file to run one version of a program for Win7 OS, and the Win8 version of the installer for the Win8 OS. One batch file, two installers, one for each OS. It should query the OS and if it sees Win7, then it runs my Win7 version installer, the same for Win8.
    Thanks

    Wednesday, February 10, 2016 8:37 AM
  • Why do you need the batch file to make the distinction?  MDT already has the ability to filter on a specific OS.

    Logs are very important. https://keithga.wordpress.com/2014/10/24/video-mdt-2013-log-files-basics-bdd-log-and-smsts-log/ Mention any customizations you have made.

    Wednesday, February 10, 2016 8:57 AM
    Moderator
  • I asked the same question. They want the batch file on hand to run outside of MDT whenever they need to, outside of cloning. I also have never used or experimented with the OS feature in MDT. I made them aware that MDT 'should' be able to install version-specific installers based on the OS of the targets, which is fine but they still want a batch file for outside of cloning. This post would additionally inform me how to create that batch file to be used in MDT, and also to be run manually when desired. Thanks!
    Wednesday, February 10, 2016 9:00 AM
  • Here's part of a deprecated batch file I wrote.

    REM Check Windows Version
    ver | findstr /i "6\.1\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win7
    ver | findstr /i "6\.2\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win8
    ver | findstr /i "6\.3\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win81
    goto warn_and_exit
    Just add a string for Windows 10. For what it's worth I dropped my batch files in favor of using vbscript or powershell, they are more powerful.


    If this post is helpful please vote it as Helpful or click Mark for answer.

    Wednesday, February 10, 2016 5:19 PM
  • Dan_Vega has the answer for you. Especially with the recommendation to use Powershell instead of batch. This kind of thing is way easier with Powershell. And using Powershell with MDT is super simple, and gets you logging for free.
    Wednesday, February 10, 2016 6:37 PM
  • I appreciate this info. How do you code this to actually run the file once the OS is determined?

    ver | findstr /i "6\.1\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win7 (or 8).

    Then when it makes is determination of the OS, do you just use

    START xxx.bat for 7, and another
    START xxx.bat for 8?

    I think you have to add :Win7 first, then some instruction....

    and :Win8 and another instruction to somehow run the 8 version for the 8 OS?


    Could you please give me an example of the two different scenarios of using this with
    Win7 and Win8? First to determine the OS, which is above, and then to kick off the exe
    which runs for that OS? Then the other for Win8. This is what I cant figure out.
    • Edited by the1rickster Wednesday, February 10, 2016 7:24 PM more detailed
    Wednesday, February 10, 2016 7:21 PM
  • Not sure if this actually works. I'm not the best with batch files:

    REM Check Windows Version
    ver | findstr /i "6\.1\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win7
    ver | findstr /i "6\.2\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win8
    ver | findstr /i "6\.3\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win81
    ver | findstr /i "10\.0\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win10
    goto warn_and_exit
    
    :ver_Win7
    runWin7Installer.exe
    Exit
    :ver_Win8
    runWin8Installer.exe
    Exit
    :ver_Win81
    runWin81Installer.exe
    Exit
    :ver_Win10
    runWin10Installer.exe
    Exit
    :warn_and_exit
    echo No appropriate OS found
    Exit 1

    Wednesday, February 10, 2016 7:34 PM
  • Ha. Thanks! I barely knew how to ask this question about batch files, let alone how to word a
    question about using Powershell to run one version of software for 7, the other version for 8. :)
    Wednesday, February 10, 2016 7:36 PM
  • Here's pretty much how I used my batch file. This should give you an idea of how to adapt it to your needs.

    @echo off
    
    REM *****************************************************
    REM * Purpose: Configure system for home use	  	*
    REM * Scripted to work on all 32bit and 64bit systems	*
    REM * Author: Daniel Vega	 			*
    REM *****************************************************
    
    REM *********************************************************************
    REM * Apply the Microsoft Office MAK and activate the license		*
    REM *********************************************************************
    IF EXIST "%systemdrive%\program files (x86)\Microsoft Office\Office15\Winword.exe" goto :Office15x64
    IF EXIST "%systemdrive%\program files\Microsoft Office\Office15\Winword.exe" goto :Office15x86
    IF EXIST "%systemdrive%\program files (x86)\Microsoft Office\Office14\Winword.exe" goto :Office14x64
    IF EXIST "%systemdrive%\program files\Microsoft Office\Office14\Winword.exe" goto :Office14x86
    
    
    :Office15x64
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /act
    goto :Firefox
    
    :Office15x86
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office15\ospp.vbs" /act
    goto :Firefox
    
    :Office14x64
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office14\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files (x86)\Microsoft Office\Office14\ospp.vbs" /act
    goto :Firefox
    
    :Office14x86
    cscript "%systemdrive%\Program Files\Microsoft Office\Office14\ospp.vbs" /inpkey:XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    cscript "%systemdrive%\Program Files\Microsoft Office\Office14\ospp.vbs" /act
    goto :Firefox
    
    
    REM ****************************
    REM * Set Firefox preferences. *
    REM ****************************
    :Firefox
    IF EXIST "%systemdrive%\program files (x86)\Mozilla Firefox\firefox.exe" copy "%CD%\mozilla.cfg" "%systemdrive%\Program Files (x86)\Mozilla Firefox" /Y
    IF EXIST "%systemdrive%\program files\Mozilla Firefox\firefox.exe" copy "%CD%\mozilla.cfg" "%systemdrive%\Program Files (x86)\Mozilla Firefox" /Y
    goto :WinAct
    
    
    REM ************************************************************
    REM * Apply the Microsoft Windows MAK and activate the license *
    REM ************************************************************
    :WinAct
    REM Check Windows Version
    ver | findstr /i "6\.1\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win7
    ver | findstr /i "6\.2\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win8
    ver | findstr /i "6\.3\." > nul
    IF %ERRORLEVEL% EQU 0 goto ver_Win81
    goto warn_and_exit
    
    :ver_Win7
    :Run Windows 7 specific commands here
    cscript %windir%\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    NET STOP "SOFTWARE PROTECTION"
    NET START "SOFTWARE PROTECTION"
    cscript %windir%\system32\slmgr.vbs /ato
    goto end
    
    :ver_Win8
    :Run Windows 8 specific commands here
    cscript %windir%\system32\slmgr.vbs /ipk XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
    NET STOP "SOFTWARE PROTECTION"
    NET START "SOFTWARE PROTECTION"
    cscript %windir%\system32\slmgr.vbs /ato
    goto end
    
    :ver_Win81
    REM *** Removed, because the MAK is now set in the task sequence for 8.1 home machines.
    goto end
    
    :warn_and_exit
    echo Machine OS cannot be determined. License has not been activated
    Pause
    
    :End
    exit


    If this post is helpful please vote it as Helpful or click Mark for answer.

    • Proposed as answer by David Bloomer Wednesday, February 10, 2016 8:54 PM
    • Marked as answer by the1rickster Tuesday, February 16, 2016 3:25 AM
    Wednesday, February 10, 2016 8:02 PM
  • That may be a template I can work around. My scenario is so much more a simpler need than this.

    I need this version software to run on this OS type, which is x64.
    I need that version of the software to run on this other OS type, also x64.

    Meanwhile, in MDT, for now, I will just add both versions and use the WMI query based on OS. This at least
    gets the correct version on the appropriate OS. If I can't get the batch to work, well, thats what we have
    specialists for. :)

    Does anyone know which method is better in MDT: Editing the application and using the WMI query for the OS, or changing the properties of the application to say....Only run on this OS (instead of the default Run On Any OS)?

    Wednesday, February 10, 2016 8:20 PM
  • If you want to leverage MDT:

    I usually import a x86 installer and then check the x86 boxes in MDT of the OS' that it is compatible with. I mark the imported application as hidden. Then I do the same for x64. Then I add a new "application bundle" and add these two apps as dependencies. Now MDT will do all the work for you of determining which installer to use depending on the OS that is running.


    If this post is helpful please vote it as Helpful or click Mark for answer.

    • Proposed as answer by David Bloomer Wednesday, February 10, 2016 8:54 PM
    • Marked as answer by the1rickster Tuesday, February 16, 2016 3:26 AM
    Wednesday, February 10, 2016 8:31 PM
  • So your choice is to uncheck Run on Any Platform, select the specific OS it can run on....
    then add the 2nd application version, allow it to only run on a specific OS as well.

    I wasn't aware that bundling them as dependencies would work that way. I always thought in order for
    the 2nd app to run, the first had to. Interesting. Its really a shame there is not much online info for MDT,
    but at the same time, Im grateful for this forum.

    Wednesday, February 10, 2016 9:04 PM
  • Not only that but you can use an application bundle to install apps in a specific order, just order them the way you want them to install from top to bottom.

    If this post is helpful please vote it as Helpful or click Mark for answer.

    Wednesday, February 10, 2016 9:12 PM
  • That much I knew...about one depending on the other to have run first. I was not aware that one
    did not particularly have to run for the subsequent one to run.
    Wednesday, February 10, 2016 9:41 PM
  • It's great, I use it to install a base set of apps and then tuck the bundle into all my task sequences no matter what OS or architecture they are, they will only get the apps designed for them.


    If this post is helpful please vote it as Helpful or click Mark for answer.

    Wednesday, February 10, 2016 9:49 PM