sqlsql-server-2008batch-fileunattended-processing

Batch run as administrator all users


I'm working on an unattended automated install of SQL 2008, 2012 and 2014 which so far is working with my batch command. The one issue I'm encountering is that in order to use a core function found in each program (management studio) properly on Windows 10, it needs to run as an admin.

In Windows 10 this can be done manually by opening the file location of the program shortcut - right click - properties - "compatibility" tab - "change settings for all users" - check the box "run this program as an administrator". Is there a way to have a batch command check that box? This way staff won't need to manually run it as an admin each time, it'll just open Management Studio automatically as an admin.

My batch command can be found below to automate the install. The beginning finds the installation files, sets the sa password to whatever I want and pulls the custom settings for the SQL installer from the configurationfile.ini file. I need the "run as an admin" for all users to run after all that.

start "" "%~dp0SQL2008\setup.exe" /SAPWD="XXXXXXXX" /ConfigurationFile="ConfigurationFile.ini" /quiet

Solution

  • @echo off
    setlocal
    
    call :reg_compat "C:\User\Test\test.exe" "WINXPSP3 RUNASADMIN"
    if errorlevel 1 echo Registry write failed.
    
    exit /b
    
    :reg_compat  fullpath, reg_data
    setlocal
    set "reg_key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
    set "reg_data="
    
    :: Get current registry data.
    for /f "tokens=1,2*" %%A in ('2^>nul reg query "%reg_key%" /v "%~1"') do (
        if /i "%%~A" == "%~1" set "reg_data=%%~C"
    )
    
    :: Write new entry and exit.
    if not defined reg_data (
        >nul reg add "%reg_key%" /v "%~1" /d "%~2" /f
        if errorlevel 1 exit /b 1
        exit /b 0
    )
    
    :: Copy original registry data.
    set "reg_data_original=%reg_data%"
    
    :: Append new data if not in original registry data.
    for %%A in (%~2) do (
        set "value_exist="
    
        for %%B in (%reg_data_original%) do (
            if "%%~A" == "%%~B" set "value_exist=1"
        )
    
        if not defined value_exist (
            call set "reg_data=%%reg_data%% %%~A"
        )
    )
    
    :: Continue only if registry data is modified.
    if "%reg_data_original%" == "%reg_data%" exit /b 0
    
    :: Write modified entry and exit.
    >nul reg add "%reg_key%" /v "%~1" /d "%reg_data%" /f
    if errorlevel 1 exit /b 2
    exit /b 0
    

    The code is set to write a test entry. The test entry will add Windows XP SP3 and Run As Admin compatibility for that filepath.

    For actual use change the arguments to call the label :reg_compat with the fullpath to the file as the 1st argument and the reg_data compatibility arguments as the 2nd argument. The compatibility arguments are uppercase and separated with a space.

    The label :reg_compat will write new entries or update entries with adding new compatibility arguments. The errorlevel from the called label is set to not 0 if reg add fails.

    Minimal code instead if you need just that:

    @echo off
    setlocal
    
    set "reg_key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
    reg add "%reg_key%" /v "C:\User\Test\test.exe" /d "WINXPSP3 RUNASADMIN" /f