This question already has an answer here:
Prevent VBscript app from showing Console Window
(1 answer)
Closed 2 years ago.
I made a script that executes an EXE, which changes fan speed of my laptop. But on boot I get annoying console popup, how do I make it go away?
vbscript:
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "DellFanCmd", "ec-disable-nofanchg", "", "runas", 1
objShell.ShellExecute "DellFanCmd", "fan1-level1", "", "runas", 1
Thanks
Refer to ShellExecute method
Syntax
.ShellExecute "application", "parameters", "dir", "verb", window
.ShellExecute 'some program.exe', '"some parameters with spaces"', , "runas", 1
Key
application The file to execute (required)
parameters Arguments for the executable
dir Working directory
verb The operation to execute (runas/open/edit/print)
window View mode application window (normal=1, hide=0, 2=Min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default)
So your code can be written as below by changing 1 to 0
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "DellFanCmd", "ec-disable-nofanchg", "", "runas", 0
objShell.ShellExecute "DellFanCmd", "fan1-level1", "", "runas", 0
Related
This question already has answers here:
File checking on Windows Startup is failing [duplicate]
(2 answers)
Closed 2 years ago.
I have this script (below), not the best script but it works fine, however every time I restart my computer that new registry triplet is gone and I have no guess why.
I got no errors from this script, but if I let it run for a reasonably period of time a vbs Msgboxpops up with,
This script contains malicious content and has been blocked by your antivirus software.
I honestly don't think it is related but apparently I cannot post a question being concise due to text requirements limitations. Or is it related and the antivirus is wiping out that triplet? After this message the new register is still there (in the registry) but not after a restart.
Dim sKey
sKey = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\so_Robocopy"
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
If fso.FileExists("so_Robocopy.bat") Then
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & "so_Robocopy.bat" & Chr(34), 0
WScript.Sleep 300000
Loop
Else
RemoveFromRegistry()
End If
Function RemoveFromRegistry()
On Error Resume Next
wshShell.RegDelete sKey 'Error handling routine
End Function
Function RegisterOnWindowsStartUp()
If DoesRegistryExist = False Then
wshShell.RegWrite sKey, Chr(34) & WScript.ScriptFullName & Chr(34), "REG_SZ"
End If
End Function
Function DoesRegistryExist()
with CreateObject("WScript.Shell")
on error resume next
sValue = .regread(sKey)
DoesRegistryExist = (err.number = 0)
on error goto 0
End with
End Function
When you run the script, it will work fine, and no problem because you are running the script on the current directory and the so_Robocopy.bat existed on the same directory.
However, on Windows Startup, the script will execute on the Directory of Windows Startup and not on the original directory where your script is located.
Here's what happened to your code,
' Script execute from the Directory of Windows Starup
If fso.FileExists("so_Robocopy.bat") Then ' (1) There will be no so_Robocopy.bat on the Directory of Windows Startup, then this will return false.
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & "so_Robocopy.bat" & Chr(34), 0
WScript.Sleep 300000
Loop
Else (2) The condition is false, then remove the key from registry.
RemoveFromRegistry()
End If
Make sure you are looking from the original directory where your script is. You can use Scripting.FileSystemObject and WScript.ScriptFullName for that.
(1)
so_robocopy_file = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\so_Robocopy.bat"
If fso.FileExists(so_robocopy_file) Then ' (2)
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & so_robocopy_file & Chr(34), 0 ' (3)
WScript.Sleep 300000
Loop
Else
I'm currently making a script that runs on startup, and if it's not already doing that, it installs into C:\Users\Username\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup using FileSystemObject (shortened to FSO in the code) .CopyFile ScriptFullName to SpecialFolders ("Startup") but it returns an error
Line: 12
Char: 1
Error: Permission denied
Code: 800A0046
Source: Microsoft VBScript runtime error
This is for an application to easily installing any VBScript into Startup (no clue what it's gonna be now)
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
WScript.Quit
End if
strStartup = WshShell.SpecialFolders ("Startup")
strCurrent = WScript.ScriptFullName
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.CopyFile strCurrent, strStartup
x=msgbox("lol", 0, "lol")
I want to have it run as admin (it does) and not return the error Permission denied
Stephen Quan has answered the question -
CopyFile needs to target a file, not a folder!
Can anyone help me with running vbs from itself but with administrator rights?
I need rename computer with Windows 8 via VBScript, but it's possible only if I run my script through administrator command line (CMD → Run as Administrator → runScript.vbs). If I start script with classic CMD the computer isn't renamed.
My idea is I start script with user rights, without parameters and if there is no parameter, the script re-runs itself with admin rights and with parameter as identificator "I'm admin".
Does anyone know how I can do this?
Edit:
I tried this:
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1
End If
If UAC is enabled on the computer, something like this should work:
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If
'actual code
Add this to the beginning of your file:
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
WScript.Quit
End if
fun lil batch file
#set E=ECHO &set S=SET &set CS=CScript //T:3 //nologo %~n0.vbs /REALTIME^>nul^& timeout 1 /NOBREAK^>nul^& del /Q %~n0.vbs&CLS
#%E%off&color 4a&title %~n0&%S%CX=CLS^&EXIT&%S%BS=^>%~n0.vbs&%S%G=GOTO &%S%H=shell&AT>NUL
IF %ERRORLEVEL% EQU 0 (
%G%2
) ELSE (
if not "%minimized%"=="" %G%1
)
%S%minimized=true & start /min cmd /C "%~dpnx0"&%CX%
:1
%E%%S%%H%=CreateObject("%H%.Application"):%H%.%H%Execute "%~dpnx0",,"%CD%", "runas", 1:%S%%H%=nothing%BS%&%CS%&%CX%
:2
%E%%~dpnx0 fvcLing admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"& timeout 3 /NOBREAK>nul
:3
%E%x=msgbox("end of line" ,48, "%~n0")%BS%&%CS%&%CX%
Nice article for elevation options - http://www.novell.com/support/kb/doc.php?id=7010269
Configuring Applications to Always Request Elevated Rights:
Programs can be configured to always request elevation on the user level via registry settings under HKCU. These registry settings are effective on the fly, so they can be set immediately prior to launching a particular application and even removed as soon as the application is launched, if so desired. Simply create a "String Value" under "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" for the full path to an executable with a value of "RUN AS ADMIN". Below is an example for CMD.
Windows Registry Editor Version 5.00
[HKEY_Current_User\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"c:\\windows\\system32\\cmd.exe"="RUNASADMIN"
`My vbs file path :
D:\QTP Practice\Driver\Testany.vbs'
objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd.exe","/k echo test", "", "runas", 1
set x=createobject("wscript.shell")
wscript.sleep(2000)
x.sendkeys "CD\"&"{ENTER}"&"cd D:"&"{ENTER}"&"cd "&"QTP Practice\Driver"&"{ENTER}"&"Testany.vbs"&"{ENTER}"
--from google search and some tuning, working for me
This is the universal and best solution for this:
If WScript.Arguments.Count <> 1 Then WScript.Quit 1
RunAsAdmin
Main
Sub RunAsAdmin()
Set Shell = CreateObject("WScript.Shell")
Set Env = Shell.Environment("VOLATILE")
If Shell.Run("%ComSpec% /C ""NET FILE""", 0, True) <> 0 Then
Env("CurrentDirectory") = Shell.CurrentDirectory
ArgsList = ""
For i = 1 To WScript.Arguments.Count
ArgsList = ArgsList & """ """ & WScript.Arguments(i - 1)
Next
CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & ArgsList & """", , "runas", 5
WScript.Sleep 100
Env.Remove("CurrentDirectory")
WScript.Quit
End If
If Env("CurrentDirectory") <> "" Then Shell.CurrentDirectory = Env("CurrentDirectory")
End Sub
Sub Main()
'Your code here!
End Sub
Advantages:
1) The parameter injection is not possible.
2) The number of arguments does not change after the elevation to administrator and then you can check them before you elevate yourself.
3) You know for real and immediately if the script runs as an administrator. For example, if you call it from a control panel uninstallation entry, the RunAsAdmin function will not run unnecessarily because in that case you are already an administrator. Same thing if you call it from a script already elevated to administrator.
4) The window is kept at its current size and position, as it should be.
5) The current directory doesn't change after obtained administrative privileges.
Disadvantages: Nobody
i am getting run-time error 429, ActiveX error: cannot create object 'InternetExplorer.Application', while calling the below code from vbscript
Set IE = CreateObject("InternetExplorer.Application")
i am running the code as non admin account. if run as admin, the code works fine. What could be the problem? Appreciate help on this
Try to run as admin with this code :
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
Set IE = CreateObject("InternetExplorer.Application")
'Your rest of your code goes here
Can anyone help me with running vbs from itself but with administrator rights?
I need rename computer with Windows 8 via VBScript, but it's possible only if I run my script through administrator command line (CMD → Run as Administrator → runScript.vbs). If I start script with classic CMD the computer isn't renamed.
My idea is I start script with user rights, without parameters and if there is no parameter, the script re-runs itself with admin rights and with parameter as identificator "I'm admin".
Does anyone know how I can do this?
Edit:
I tried this:
If WScript.Arguments.Count = 0 Then
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "wscript.exe", "c:\Users\admin\Documents\selfConfigure.vbs -1", "", runas", 1
End If
If UAC is enabled on the computer, something like this should work:
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
WScript.Quit
End If
'actual code
Add this to the beginning of your file:
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
WScript.Quit
End if
fun lil batch file
#set E=ECHO &set S=SET &set CS=CScript //T:3 //nologo %~n0.vbs /REALTIME^>nul^& timeout 1 /NOBREAK^>nul^& del /Q %~n0.vbs&CLS
#%E%off&color 4a&title %~n0&%S%CX=CLS^&EXIT&%S%BS=^>%~n0.vbs&%S%G=GOTO &%S%H=shell&AT>NUL
IF %ERRORLEVEL% EQU 0 (
%G%2
) ELSE (
if not "%minimized%"=="" %G%1
)
%S%minimized=true & start /min cmd /C "%~dpnx0"&%CX%
:1
%E%%S%%H%=CreateObject("%H%.Application"):%H%.%H%Execute "%~dpnx0",,"%CD%", "runas", 1:%S%%H%=nothing%BS%&%CS%&%CX%
:2
%E%%~dpnx0 fvcLing admin mode look up&wmic process where name="cmd.exe" CALL setpriority "realtime"& timeout 3 /NOBREAK>nul
:3
%E%x=msgbox("end of line" ,48, "%~n0")%BS%&%CS%&%CX%
Nice article for elevation options - http://www.novell.com/support/kb/doc.php?id=7010269
Configuring Applications to Always Request Elevated Rights:
Programs can be configured to always request elevation on the user level via registry settings under HKCU. These registry settings are effective on the fly, so they can be set immediately prior to launching a particular application and even removed as soon as the application is launched, if so desired. Simply create a "String Value" under "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" for the full path to an executable with a value of "RUN AS ADMIN". Below is an example for CMD.
Windows Registry Editor Version 5.00
[HKEY_Current_User\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"c:\\windows\\system32\\cmd.exe"="RUNASADMIN"
`My vbs file path :
D:\QTP Practice\Driver\Testany.vbs'
objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd.exe","/k echo test", "", "runas", 1
set x=createobject("wscript.shell")
wscript.sleep(2000)
x.sendkeys "CD\"&"{ENTER}"&"cd D:"&"{ENTER}"&"cd "&"QTP Practice\Driver"&"{ENTER}"&"Testany.vbs"&"{ENTER}"
--from google search and some tuning, working for me
This is the universal and best solution for this:
If WScript.Arguments.Count <> 1 Then WScript.Quit 1
RunAsAdmin
Main
Sub RunAsAdmin()
Set Shell = CreateObject("WScript.Shell")
Set Env = Shell.Environment("VOLATILE")
If Shell.Run("%ComSpec% /C ""NET FILE""", 0, True) <> 0 Then
Env("CurrentDirectory") = Shell.CurrentDirectory
ArgsList = ""
For i = 1 To WScript.Arguments.Count
ArgsList = ArgsList & """ """ & WScript.Arguments(i - 1)
Next
CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & ArgsList & """", , "runas", 5
WScript.Sleep 100
Env.Remove("CurrentDirectory")
WScript.Quit
End If
If Env("CurrentDirectory") <> "" Then Shell.CurrentDirectory = Env("CurrentDirectory")
End Sub
Sub Main()
'Your code here!
End Sub
Advantages:
1) The parameter injection is not possible.
2) The number of arguments does not change after the elevation to administrator and then you can check them before you elevate yourself.
3) You know for real and immediately if the script runs as an administrator. For example, if you call it from a control panel uninstallation entry, the RunAsAdmin function will not run unnecessarily because in that case you are already an administrator. Same thing if you call it from a script already elevated to administrator.
4) The window is kept at its current size and position, as it should be.
5) The current directory doesn't change after obtained administrative privileges.
Disadvantages: Nobody