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