Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#16 Post by zask » 15 May 2017 10:17

aGerman wrote:A batch file always runs in a console window. As you know you can hide it with a VBScript snippet. So what you could do is to run it directly from a VBScript.
(I wonder what hiding a batch window is even good for. If you don't want it you should rather move to another language.)

Steffen


I use multiple script files on startup on my computer, I just don't like all the windows on my computer

zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#17 Post by zask » 15 May 2017 10:23

aGerman wrote:A batch file always runs in a console window. As you know you can hide it with a VBScript snippet. So what you could do is to run it directly from a VBScript.
(I wonder what hiding a batch window is even good for. If you don't want it you should rather move to another language.)


Your right there is no purpose, except just for convince, I don't even like the uac appearing every time, however I like the trick you used above with "woami"... Pretty clever.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#18 Post by aGerman » 15 May 2017 10:29

Instead of placing the script into the startup folder use shortcuts. You can adjust the window style ("Run:") to Minimized in their properties.
As I told you - alternatives are
- Running directly from VBScript
- Running as scheduled task

Steffen

zask
Posts: 26
Joined: 14 Dec 2015 17:58

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#19 Post by zask » 15 May 2017 12:58

aGerman wrote:Instead of placing the script into the startup folder use shortcuts. You can adjust the window style ("Run:") to Minimized in their properties.
As I told you - alternatives are
- Running directly from VBScript
- Running as scheduled task

Steffen


I understand, I simply just wanted the process to be automatic. if that makes sense. also I have a way to minimize the window, but its annoying having like 4 minimized windows every time the computer starts, wanted to move to an more easy script that I can just copy, paste, and do it all at once, it saves me more time on busy days too.
thank you for your help.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#20 Post by aGerman » 15 May 2017 13:51

Du you have to pass any arguments to one of your batch files or do you have to simply run them?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Run batch file invisible on startup with uac, if uac fails to obtain admin, continue batch file after uac is closed

#21 Post by aGerman » 16 May 2017 11:38

Okay thats what I was thinking about. Save it as VBScript (extension .vbs).

Code: Select all

Option Explicit

' Variable declarations
Dim objWSHShell, objShellApp, objFSO, flags, strBatPath, strAbsBatPath, arrBatPaths

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Comma-separated list of batch files
' Every name has to be enclosed in quotation marks
' You have to use relative or absolute paths if the files are not saved in the script directory
arrBatPaths = Array("X.bat", "Y.bat", "Z.bat")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

' Create necessary ActiveX objects
Set objWSHShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")


' Make sure the working directory is the script directory
objWSHShell.CurrentDirectory = objFSO.GetParentFolderName(WScript.ScriptFullName)

' Run WHOAMI in a hidden console window and try to find SIDs S-1-5-32-544 (Administrators)
'  and S-1-16-12288 (High Mandatory Level - that is, the script was run elevated)
' Possible values of flags afterwards:
'  0 - the user doesn't belong to the admin group, the file wasn't run elevated
'  1 - the user belongs to the admin group, the file wasn't run elevated
'  2 - the user doesn't belong to the admin group, the file was run elevated
'  3 - the user belongs to the admin group, the file was run elevated
flags = objWSHShell.Run( _
"%comspec% /q /von /c ""set ""ret=0""&(" & _
"for /f tokens^=5^ delims^=^"" %i in ('whoami /groups /fo csv /nh^|findstr ""\<S-1-5-32-544\> \<S-1-16-12288\>""') do " & _
"if ""%i""==""S-1-5-32-544"" (set /a ""ret|=1"") else if ""%i""==""S-1-16-12288"" set /a ""ret|=2""" & _
")&exit /b !ret!""", 0, True)

' Only if the user belongs to the admin group and the script file wasn't already run elevated
'  invoke the UAC prompt and run this VBScript alevated again.
If flags = 1 Then
  objShellApp.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """", "", "runas", 0
  WScript.Quit
End If

' Run every batch file of the array in a new cmd.exe process with hidden window
' Every cmd.exe process will inherit the privilege level of the VBScript (wscript.exe process)
' The working directory of a cmd.exe process will be set to the script directory of the batch file run in this process
For Each strBatPath In arrBatPaths
  If objFSO.FileExists(strBatPath) Then
    strAbsBatPath = objFSO.GetAbsolutePathName(strBatPath)
    objShellApp.ShellExecute "cmd.exe", "/c """ & strAbsBatPath & """", objFSO.GetParentFolderName(strAbsBatPath), "open", 0
  End If
Next

Advantages:
- no flashing console window anymore
- only one UAC prompt for all of the batch files listed in arrBatPaths

Steffen

Post Reply