Problem with batch script

TIMECOP79 0 Reputation points
2023-06-12T19:45:00.5966667+00:00

Hi, I have a problem with the following script.

@echo off  REM Check the OS version wmic os get Caption | findstr /i "Windows 10 Enterprise" > nul if %errorlevel% equ 0 (     set "install_folder=\\server\share\EnterpriseAppFolder" ) else (     wmic os get Caption | findstr /i "Windows 10 Professional" > nul     if %errorlevel% equ 0 (         set "install_folder=\\server\share\ProfessionalAppFolder"     ) else (         echo Unsupported OS version.         exit /b 1     ) )  REM Install the application echo Installing application from %install_folder%... msiexec /i "%install_folder%\application.msi" /qn  REM Add additional installation steps if needed  exit /b 0 

In both cases, it always chooses the first option, and %errorlevel% is equal to zero. Even though it is Windows 10 Professional, it selects the "Windows 10 Enterprise" option. Although the script seems to be correct, please help me.

Windows for business Windows Client for IT Pros Devices and deployment Set up, install, or upgrade
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AllenLiu-MSFT 49,311 Reputation points Microsoft External Staff
    2023-06-13T03:08:01.43+00:00

    Hi, @TIMECOP79

    Thank you for posting in Microsoft Q&A forum.

    Based on the provided script, the issue seems to be with the findstr command. The search string is currently set to "Windows 10 Enterprise" and it will match any OS containing those words, including "Windows 10 Enterprise" and "Windows 10 Enterprise N". The same is true for the search string "Windows 10 Professional".

    To fix the issue, you can modify the search strings to match the exact OS version by adding a space at the end of the search strings. Here's the updated script:

    @echo off
    REM Check the OS version
    wmic os get Caption | findstr /i "Windows 10 Enterprise " > nul
    if %errorlevel% equ 0 (
        set "install_folder=\\server\share\EnterpriseAppFolder"
    ) else (
        wmic os get Caption | findstr /i "Windows 10 Professional " > nul
        if %errorlevel% equ 0 (
            set "install_folder=\\server\share\ProfessionalAppFolder"
        ) else (
            echo Unsupported OS version.
            exit /b 1
        )
    )
    REM Install the application
    echo Installing application from %install_folder%...
    msiexec /i "%install_folder%\application.msi" /qn
    REM Add additional installation steps if needed
    exit /b 0
    

    With this modification, findstr will only match OS versions that contain the exact search string, which should solve the issue you're facing.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Add comment".

    0 comments No comments

  2. TIMECOP79 0 Reputation points
    2023-06-13T08:01:12.37+00:00

    Hello,

    First of all, thank you very much for taking the time to respond to me. Unfortunately, I ran the test on "Windows 10 Professional," but the script still points to the "Windows 10 Enterprise" folder. To be precise, the "wmic" command displays "Microsoft Windows 10 Enterprise." I believe I have found the real issue, which lies in the "else" statement not being taken into account. Now I need to figure out how to solve the problem.

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.