[Help] Run Batch script without windows prompt

Discussion in 'Scripting' started by dudedroid, Aug 21, 2015.

  1. dudedroid

    dudedroid MDL Junior Member

    Apr 28, 2011
    91
    7
    0
    #1 dudedroid, Aug 21, 2015
    Last edited by a moderator: Apr 20, 2017
    I have written a batch script which takes admin privileges and then Kill a certain process if exist and rerun it else just run it.

    Code:
    @echo off
    :: BatchGotAdmin
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        set params = %*:"=""
        echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        del "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    tasklist /fi "imagename eq SynTPEnh.exe" |find ":" > nul
    if errorlevel 1 taskkill /f /im "SynTPEnh.exe"&start "" "c:\Program Files\Synaptics\SynTP\SynTPEnh.exe"
    if errorlevel 0 start "" "c:\Program Files\Synaptics\SynTP\SynTPEnh.exe"
    I have scheduled it as task and the trigger is whenever someone unlock the workstation.

    Everything runs smoothly but the problem is there is command prompt popup when the script runs for a few second.

    For elimination of that i have created a Wscript

    Code:
    Set oShell = CreateObject ("Wscript.Shell") 
    Dim strArgs
    strArgs = "cmd /c touchpad.bat"
    oShell.Run strArgs, 0, false
    When i trigger this script from task scheduler it seems not to run. But doesnt report any error as well.

    My main aim is to run my batch script without cmd prompt on run.

    Regards
     
  2. bleedlikeme

    bleedlikeme MDL Novice

    Jul 24, 2015
    18
    4
    0
    #2 bleedlikeme, Aug 21, 2015
    Last edited by a moderator: Apr 20, 2017
    Changes:

    - Use .vbs script to check for admin privileges then run batch file in silent mode
    - Removed 'getadmin' code from batch since it's no longer needed; VBScript already takes care of this
    - Added 2 second delay before attempt to restart process

    Usage:

    - Trigger .vbs script from Task Scheduler

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

    touchpad.bat
    Code:
    @echo off
    cd /d "%~dp0"
    :Begin
    tasklist /fi "imagename eq SynTPEnh.exe" |find ":"
    if %errorlevel% == 0 (
        start "" "%ProgramFiles%\Synaptics\SynTP\SynTPEnh.exe"
        EXIT
    ) else (
        taskkill /f /im "SynTPEnh.exe"
    )
    ping -n 3 127.0.0.1 >nul
    goto Begin
    
    ------------

    touchpad.vbs
    Code:
    Set objWsh = WScript.CreateObject("WScript.Shell")
    Set objShellApp = CreateObject("Shell.Application")
    
    If WScript.Arguments.length = 0 Then 
        objShellApp.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " RunAsAdministrator", , "runas", 1 
        WScript.Quit
    Else
    End If
    
    objWsh.Run "" & """[drive:][path]filename""" & "", 0
    
    Notes:

    Modify the last line accordingly; specify the full path to batch file - eg. D:\scripts\touchpad.bat