vbs run program as admin and with a priority level - vbscript

I want on startup to launch a vbs script that will launch another program as administrator and set the priority of that program to above normal or high priority.
I currently have made it to launch the program as admin but am stuck on setting the process level.
Set app = CreateObject("Shell.Application")
app.ShellExecute """d:\SYNC\Dropbox\PORTABLE_PROGRAMS\ahk\Navigare\KeyboardEnchancer\KeyboardEnchancer.exe""", , , "runas", 3

I edited the answer to address your permission problem, the script now self-elevates to run as administrator, more info at: How to Automatically Elevate a Vbscript to Run it as Administrator?. Tested and working flawlessly on my machine.
If WScript.Arguments.length = 0 Then
Set objShell = CreateObject("Shell.Application")
'Pass a bogus argument, say [ uac]
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
Set objShell= CreateObject("Shell.Application")
strComputer = "."
Const HIGH_PRIORITY = 128
processName = "notepad.exe" ' The process name of your app
appName = "C:\Windows\System32\notepad.exe" ' The app you want to run
objShell.ShellExecute appName, , , "runas", 1
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & processName & "'")
For Each p in colProcesses
p.SetPriority(HIGH_PRIORITY)
Next
End If
More info at: SetPriority method of the Win32_Process class and ShellExecute method.

I used this to change a script's own priority from within its execution:
Sub SetPriority()
Const IDLE = 64, BELOW_NORMAL = 16384, NORMAL = 32, ABOVE_NORMAL = 32768, HIGH_PRIORITY = 128, REAL_TIME = 256
GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("Select * From Win32_Process Where CommandLine Like '%" &Wscript.ScriptName& "%'").ItemIndex(0).SetPriority(HIGH_PRIORITY)
End Sub
Call SetPriority()

This answers the question... as admin with priority.
dim sFile: sFile=chr(34)& "C:\Windows\System32\notepad.exe" &chr(34)
CreateObject("Shell.Application").ShellExecute "cmd.exe","/c START /HIGH /B """" " &sFile, ,"runas", 1

Related

viewing running processes with vbscript

I want to see all the processes running on my computer but the cmd command only gives the applications, not any scripts or smaller files. I am trying to figure out a way to list all the processes in a more advanced way that will list EVERYTHING currently running. Does anyone know a way to do that with vbscript? Or if there is a better way to do this what is it?
Using TaskList Command
TaskList Command can be used to display a list of all running applications and services with their details and Process IDs(PIDs).
Dim ProTFPath, ProTF, StrPrInfo, StrPrInfoA, PrInfo
Set WshShell = WScript.CreateObject("Wscript.Shell")
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
ProTFPath = "C:\PROCESSES.txt"
WshShell.Run "CMD /C TASKLIST /V /FO LIST > """ + ProTFPath + """", 0, True
' Here Run is used instead Exec to avoid console window flashes.
If FSO.FileExists(ProTFPath) Then
Set ProTF = FSO.OpenTextFile(ProTFPath, 1, False)
End If
StrPrInfoA = ProTF.ReadAll
PrInfo = Split(StrPrInfoA, VbCrLf + VbCrLf)
For I = 0 To UBound(PrInfo)
WScript.Echo PrInfo(I)
Next
Erase PrInfo
ProTF.Close
If you no longer need this file, add following lines to the end of the script:
If FSO.FileExists(ProTFPath) Then
FSO.DeleteFile(ProTFPath, True)
End If
See more information about TaskList here.
EXE_Process = AllProcessRunningEXE(".")
Vbs_Process = AllProcessRunningVBS (".")
Function AllProcessRunningEXE( strComputerArg )
strProcessArr = ""
Dim Process, strObject
strObject = "winmgmts://" & strComputerArg
For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
strProcessArr = strProcessArr & ";" & vbNewLine & Process.name
Next
AllProcessRunningEXE = Mid(strProcessArr,3,Len(strProcessArr))
End Function
Function AllProcessRunningVBS (strComputerArg)
strProcessArr = ""
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputerArg & "\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'cscript.exe' OR Name = 'wscript.exe'")
For Each objItem in colItems
strProcessArr = strProcessArr & ";" & vbNewLine & objItem.CommandLine
Next
AllProcessRunningVBS = Mid(strProcessArr,3,Len(strProcessArr))
Set objWMIService = Nothing
Set colItems = Nothing
End Function

Running a VBS script elevated to get remote computer serial number

Ok, I have an error someplace in here, but not sure where. I am NOT a coder by any means, this is something I have put together from a couple of different sources. This code works, however it seems to run once as a normal user and once at elevated permissions... I just need it to run just once at elevated permissions.
Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
End if
On Error Resume Next
Dim System
if Wscript.Arguments.Count >0 then
sSystem=Wscript.Arguments(0)
end if
ComputerName = InputBox("Enter the name of the computer you wish to query")
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
for each SN in SNSet
MsgBox "The serial number for the specified computer is: " & SN.SerialNumber
next
This is the part that re-runs your script with elevated privileges by using the Shell.ShellExecute method with the "runas" verb:
If WScript.Arguments.length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" &_
" RunAsAdministrator", , "runas", 1
End if
Re-running the script with the additional parameter RunAsAdministrator makes sure that the re-run script skips the above part (since WScript.Arguments.Length is greater than 0 due to that parameter) and goes directly to the "worker" code.
However, the above code snippet doesn't exit after re-running the script, so both the elevated and the original invocation are executing the worker code.
Add a WScript.Quit statement to your code to make the original invocation exit right after re-running itself with elevated permissions and the issue will disappear:
If WScript.Arguments.Length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe", _
"""" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
WScript.Quit 0
End If
that's all (for remote computer):
ComputerName = InputBox("Enter the name of the computer you wish to query")
winmgmt1 = "winmgmts:(impersonationLevel=impersonate}!//"& ComputerName &"\root\cimv2")
Set SNSet = winmgmt1.ExecQuery("Select * from Win32_BIOS")
for each SN in SNSet
MsgBox "The serial number for the specified computer is: " & SN.SerialNumber
next

Basic VBS help - optimizing VBS script

I found this simple script that outputs the logical disk sizes.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size
Next
My VBS skills are very poor and I need help:
I would like to get a single size number of ONLY the C and D partitions added together
if the size (from step1) is not equal to 500-GB (between 450,000,000,000 and 550,000,000,000) I need the computer to prompt a warning and "press any key" to continue
I don't want a pop-up window since this is going to run from the prompt of WinPE, is it possible to get the output in the prompt window?
I'm asking a lot so thank you in advance if you can help
You will need to start your script using cscript.
The code for this comes from http://ask.metafilter.com/79481/vbscript-printing-to-command-line
This allows the echos to go to the command line instead of a Message box.
CheckStartMode
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
If(objDisk.DeviceID="C:" or objDisk.DeviceID="D:") then
Wscript.Echo "DeviceID: " & objDisk.DeviceID & " with a Disk Size: " & objDisk.Size
TotalSize = CCur(TotalSize) + CCur(objDisk.Size)
End if
Next
If(TotalSize <450000000000 or TotalSize >550000000000) then
Wscript.Echo "Disk size of " & TotalSize & " is out of range."
Wscript.Echo "Press enter to contine."
z = WScript.StdIn.Read(1)
End if
Wscript.Echo "Complete, Press enter to end."
z = WScript.StdIn.Read(1)
Sub CheckStartMode
' Returns the running executable as upper case from the last \ symbol
strStartExe = UCase( Mid( wscript.fullname, instrRev(wscript.fullname, "\") + 1 ) )
If Not strStartExe = "CSCRIPT.EXE" Then
' This wasn't launched with cscript.exe, so relaunch using cscript.exe explicitly!
' wscript.scriptfullname is the full path to the actual script
set oSh = CreateObject("wscript.shell")
oSh.Run "cscript.exe """ & wscript.scriptfullname & """"
wscript.quit
End If
End Sub

Obtaining process that launched (for instance) iexplore with VBScript/JScript

Is there a way to (ideally with a scripting language like VBScript / JScript) get details of a process that spawned a different program i.e., In the case when Computrace LoJack launches iexplore, to handle communications with the internet?
You can use WMI to check the ParentProcessId for the process you are interested in. In the case of "normal" user mode applications, the parent process should be explorer.exe.
strProcess = "iexplore.exe"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = '" & strProcess & "'")
For Each objProcess in colProcesses
WScript.Echo objProcess.ParentProcessId
Next
In the case of Internet Explorer, make sure you check for the ID of IE as well since it will spawn multiple instances of itself. Try something like this:
strProcess = "iexplore.exe"
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = 'explorer.exe' OR name = 'iexplore.exe'")
i = 0
arrIds = Array()
For Each objProcess in colProcesses
ReDim Preserve arrIds(i)
arrIds(i) = objProcess.ProcessId
i = i + 1
Next
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process " _
& " Where name = '" & strProcess & "'")
For Each objProcess in colProcesses
intParentID = objProcess.ParentProcessId
blnIsFound = False
For Each intID in arrIds
If intID = intParentID Then
blnIsFound = True
Exit For
End If
Next
If blnIsFound = False Then
WScript.Echo "Process " & objProcess.ProcessId & " spawned by process " & objProcess.ParentProcessId
End If
Next

How to create a user account in Windows Vista using VBScript?

How to create a user account in Windows Vista using VBScript?
I'm using the following script. It's working fine on Windows XP, but gives me an error on Windows Vista:
strUserName = "username"
strPassword = "password"
strComputer = "."
set objSystem = GetObject("WinNT://" & strComputer)
set objUser = objSystem.Create("user", strUserName)
objUser.SetPassword strPassword
objUser.SetInfo
I am able to run this script on my Vista box just fine, and it creates the user.
I suspect you might be having a UAC issue. This article provides some options for elevating the permissions of your script.
Option 1 – the code relaunches itself with elevated permissions:
If WScript.Arguments.length = 0 Then
Set objShell = CreateObject("Shell.Application")
'Pass a bogus argument, say [ uac]
objShell.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
'Add your code here
End If
Option 2 – a separate launcher script:
Set objShell = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")
strPath = FSO.GetParentFolderName (WScript.ScriptFullName)
If FSO.FileExists(strPath & "\MAIN.VBS") Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\MAIN.VBS" & Chr(34), "", "runas", 1
Else
MsgBox "Script file MAIN.VBS not found"
End If

Resources