How to start a program with admin privileges in a batch file - windows-7

How do I start/call a batch file from another, but with administrative privileges, so that it doesn't give me errors like the following?
Access is denied error code 5
Here is something like what I would like it to be.
echo PLEASE TYPE YOUR USERNAME AND PASSWORD IN THE FIELDS BELOW.
echo.
echo.
echo.
echo.
set /p u=Username:
echo.
set /p p=Password:
start next.bat %u% %p%

No matter which way you chose, You must accept run it with admin privilege, so the point is which way is shorter? You could Right Click > Run as administrator as jean-Michael said although I prefer james approach (using vbScript) but if you don't want to use another vbScript file and just want benefit of just click on batch file and accept run with admin privilege (Note you have one right click and left click less) I suggest you this:
create a shortcut from your batch file and right click on shortcut > Properties > Shortcut Tab > Advanced now check the Run as administrator check-box. every time you execute it from the shortcut you just have one click to accept run it with admin privilege.
Hope this help.

Sometimes third party utilities like AutoIt (see runas function) are not an option - but if you do have that option, check it out as that will let you do exactly what you're aiming to. You can then call the AutoIt script from your script and use its runas function.
Windows runas doesn't support providing a password unless you're happy with the /savecred option - which is fine if you're only running the task from a single computer. The first time it will ask you for a password, but after that it won't (though you still have to use /savecred option each time you use it). I've got a feeling using this could be a huge security hole. But since it seems this is for your own machine, in your batch use this:
runas /user:computername\username /savecred yourcommand.exe
Another way is to make a scheduled task that can be called by your script. You can make it using the GUI or from an elevated command line as described here.
You can then call it from your script like this:
SCHTASKS /Run /TN yourtaskname

Simply put the bat file into the Windows directory, and it will run as administrator. I tried this myself, and it worked:
C:\Windows\batch_file.bat
It should work like that.

Within the batch file itself there is no way to run as an administrator, however if you launch the batch file from within a .VBS file, you are able to specify a 'runas' parameter.
set shell=CreateObject("Shell.Application")
shell.ShellExecute "your_batch_file.bat",,"C:\path\to\thedirectory", "runas", 0
set shell=nothing
This will launch your batch file as an administrator, and you can enable or disable the shell display (this example hides it as i wanted my program to run in the background without being seen).

Right click -> Run as administrator.
I think microsoft made as much as they could to prevent batch script to get administrative privileges on their own.

#cmd, I posted an example (How can I test effective permissions of a user from a batch script?) to run another bat file with ShellExecute and elevated rights (only when it's needed).
Take a look if it's what you looking for and what you need. If not, let me know and we could adapt your script to make it work.
good luck

I actually joined just to answer this, the simplest way by far is to create a shortcut to the program you want to run, then set the shortcut to run as administrator and just call the shortcut from the batch file. This will run with the settings specified in the shortcut and you could place this shortcut in the same folder as your batch file or just call it from the start menu.
Example:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Folder\Shortcut Name.lnk"

Related

Windows Task Scheduler Doesn't Run VBScript

I am trying to automate a VBScript by using Windows Task Scheduler. However, I tried to use cscript.exe + "C:\...\script.vbs" but it didn't run. I also tried to directly run the same command in CMD (cscript.exe "C:\...\script.vbs") and it worked.
What might be the problem?
EDIT :
I just tried to switch the setting to "Run only when user is logged on" from "Run whether user is logged on or not" and it worked. I am wondering if there is a way to make my task scheduled run even when the user is logged off.
After hours of research, one of Blake Morrison (from Microsoft)'s blogs came up; it mentioned that
If running a .vbs / .ps1 script, try launching it from a .cmd / .bat script
see Help! My Scheduled Task does not run…
That blog also explains a lot of rules/tips when using Task Scheduler.
So please create a .cmd/.bat file that calls for your VBScript. For example: cscript.exe YourScript.vbs would be in your .cmd/.bat script.
Write a batch file like this:
echo "Started!" > c:\foldergoeshere\log.txt
cscript.exe "C:\...\script.vbs" > c:\foldergoeshere\log.txt
echo "Stopped!" > c:\foldergoeshere\log.txt
Then schedule the batch file instead of the vbs. That will allow you to see what is happening that is preventing it from running. Any error that you would have seen executing in the console (CMD), will be instead output to that log file between "Started!" and "Stopped!"
What's the hassle all about? I don't use .cmd/.bat and script works! (Windows7 here)
My VBS script (as a scheduled task) runs well on any scenario of these 4:
cscript and option "Run only when user is logged on"
cscript and option "Run whether user is logged on or not"
wscript and option "Run only when user is logged on"
wscript and option "Run whether user is logged on or not"
It's only that on the 1st scenario I encounter the black command window flashing on my screen.
Action settings:
or
My script, which simply creates a file:
Set objFSO = CreateObject("Scripting.FileSystemObject")
filename = "C:\Temp\" & Hour(Time) & Minute(Time) & Second(Time)
Set objFile = objFSO.CreateTextFile(filename)
Greg answered this https://superuser.com/a/816073
Basically you need to create 2 folders:
You have to create a folder (or two on a 64bit-windows):
(32Bit, always) C:\Windows\System32\config\systemprofile\Desktop
(64Bit) C:\Windows\SysWOW64\config\systemprofile\Desktop
Fixed the issue for me (and I could point to the .vbs file, not bat needed).
The .vbs file is running invisibly, which is a consequence of running it with the 'logged on or not' option.
You will not be allowed to interfere with a user using the computer, which means you will be able to help yourself, but not others.
Please read the following text from the Task Scheduler Help menu:
Task Security Context
You can specify that a task should run even if the account under which the task is scheduled to run is not logged on when the task is triggered.
To do this, select the radio button labeled Run whether user is logged on or not.
If this radio button is selected, tasks will not run interactively.
To make a task run interactively, select the Run only when user is logged on radio button.
You can do this.
cmd as the Program.
/c start cscript //nologo "W:\Pathto with spaces\VBScript.vbs"
/c send the following to the cmd
start starts the next parameter
cscript //nologo "W:\Path with spaces\VBScript.vbs" self explanatory. Path wrapped in quotes to allow spaces in the filename.
Screenshot
Have experienced more than once that a VBScript running as planned task worked fine for months and years but suddenly would not work any more despite nothing was changed. Have tried to reactive the task using all the recipes given here and elsewhere, but no success. My workaround was to create a new planned task with all settings copied from the original one.
I tried this on Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
AKA Window 7, But in my case i first cd to the dir and then ran the .bat file to execute the .vbs file
Go to start menu
Search for run
Once it opens, Type "shell:startup" and press Enter
You'll be directed to the Start Up folder. In this folder you can put create a .bat file which you want to execute the .vbs file (You'll do this in a special way)
cd C:\the\path\were.vbs\file\located && wscript.exe start_app.vbs
What your doing is cd to the directory cd C:\the\path\were.vbs\file\located
And then run the .vbs file wscript.exe start_app.vbs

Using RUNAS to use the local Administrator account to run a second batch file with elevated privileges

I am trying to develop a simple script that can run a second script as the machine's local administrator. As the first script I am currently trying:
set name=%computername%
runas /noprofile /user:%name%\Administrator "cmd.exe /c %cd%bg2.2.bat"
When I run the script it never seems to run the second script, which is located in the same directory. The second script is only for copying files.
del "C:\Windows\obh_logo.bmp"
copy "%cd%obh_logo.bmp C:\Windows\obh_logo.bmp"
When the first script runs I am prompted for the admin password as expected and once I enter it, a second command prompt seems to flicker on the screen then vanishes. It never appears to run the second script. What seems to be the reason for this not working?
%cd%will probably not point where you think it does, (just tested it, of course the variable gets replaced before the command gets called and does actually point to the first scripts dir, but the problem still persists in the second script)
the runaschanges the working directory for the cmd.exe instance you are starting, probably to C:\Windows\system32.
replace it with %~dp0, which yields the drive and path of the current batch file.
But! Your script will not run the second file elevated, just under the Administrator account, which is not the same. Manual confirmation is always necessary, see here for possible ways to request elevation from within your script.
I now assume the missing rights is the actual problem, add a pause statement in your second script to watch it, or call it with cmd /k so the console doesn't close.

How to create a shortcut to launch an App with admin privileges from the cmd-line?

I have an installer (Inno-Setup) that installs my application to a path defined by the user. At the end of the install routine i want to create a shortcut that starts the application with admin privileges. The solution should work on all win version from winXP to Win7.
What can i do to achieve this?
I know that it is possible with a batch script, that executes a nasty vb-script. The disadvantage is that the cmd-window popup and it only works on win7 i guess.
I also tried the command mklink to create a hyperlink, but it does not work because it is not possible to pass an argument that set the admin priviliges.
You can add a registry-key that tells windows to execute your program as admin:
Under HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers, just add a key(REG_SZ) <Path to your exe> with the value RUNASADMIN. When you launch your exe, you will be prompted for admin-access.
With that, you can simply create a normal shortcut to your executable like you would do it with Inno-Setup.
If you want to do so via a cmd or a batch-file, you can use the following command:
reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "<Path to your exe>" /t REG_SZ /d RUNASADMIN
The "Run as admin" is a property of the executable, not the shortcut. You should add the required manifest that makes Windows prompt for elevation.
To do this on Windows XP, you will need to use the runas verb with ShellExecute() to run as a different user, but this will remove any ability to access the local profile. This can be done from your exe when it finds it's not running with full admin access.
After creating the shortcut, change its 21st byte (position 0x15) to 32 (0x20) to make it "Run as Administrator". Changing it back to 0 makes it a "normal" (non-admin) shortcut.
Can be done shortcutjs.bat:
shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -adminpermissions yes
-adminpermissions yes is for if you want to run the bat as administrator. You'll need the full path to your script.
The 'run as admin' tick sets a binary flag in the .lnk file (21st character) and this what the script is doing too - reads it as binary steam and changes that value.

How to use runas command in windows 2008 r2 server?

I have written a batch file which will call another batch file and delete some files. For this I need to have admin rights. I tried following command...
runas /user:bala#nsc cmd
Enter the password for bala#nsc: xxxxxxx
Even though bala#nsc has admin rights command prompt is getting opened as a simple user rather than administrator.
I guess, I am missing something. Please help me.
Your results aren't what I expected, either. But I've got a few workarounds to suggest.
You could turn off User Account Control.
You could go to Start --> All Programs --> Accessories, then right-click on "Command Prompt" and choose "Run as Administrator."
You could right-click on your batch script and do the same.
You could create a shortcut to either cmd or your batch script on your Desktop, then modify the properties of that shortcut to run as Administrator.
You could add some code to your batch script to check for admin privileges and prompt for escalation if needed.
Turning off UAC would be my choice.

How to change cscript.exe to wscript.exe execution for all VBScript files?

After installing the new server, I am facing an issue.
I have lot of .vbs files, all need to run in wscript, reason, I use all those command like WScript.Echo "hello"
I want to be able to see the output when I double click the VBScript file.
But when I right click on the vbs file, I see console, I want to change the default to Windows host, globally!
How can I do that?
You can change the default scripting host to wscript.exe like so:
wscript.exe //H:wscript
If you wish to set cscript as the default host, that works in the same way:
wscript.exe //H:cscript
You can execute cscript.exe with the same arguments for the same result.
You can switch the default script engine with:
wscript //H:Wscript
Good luck!
I was able to solve it by using the following steps:
selecting the VBScript file that I want to open,
right click to select default program for this,
browse to C:/windows/windows32/wscript.exe, and select this.
In command prompt (as administrator):
To set windows script host as default script host enter:
wscript.exe //H:WScript
To set command line based script host as default script host enter:
cscript.exe //H:cscript
Check the Windows Explorer settings for the filetype *.vbs (something like tools->options->file types etc.) and change the "open with" setting to cscript.
Edit: I now advise caution with the recommendations I give below. After continuing to toggle and test my settings, I find I am unable to re-establish cscript as my default script host. (Note that I also retried the procedures given by other answers to this question.)
In addition, I tried using Process Monitor (a.k.a., "ProcMon") to find the reason for my difficulties, but unfortunately have not been unsuccessful.
Finally, I also considered going back to an earlier Windows 7 restore point, but this was complicated by the fact that I just yesterday changed my domain password. So, for now, I'm going to have to put my investigation to rest as other tasks are pressing.
On last thought...I have also considered the possibility that there are network policy security settings that are thwarting my efforts.
Original Answer: Enter the following line in a Windows batch file:
ftype VBSFile="%%SystemRoot%%\System32\WScript.exe" "%%1" %%*
Then, run a Command Prompt as an Administrator and run the batch file.
I believe the above will make the change for all users on the system. To make the change for only the logged in user, do the following (on Windows 7):
Control Panel => Programs => Default Programs => Associate a file type or protocol with a program
Then, in the Name column, scroll down to .vbs and click Change program... in the upper right. Then choose one of the Recommended Programs. If you do not see Microsoft ® Windows Based Script Host, browse to the following file:
C:\Windows\System32\wscript.exe
The guy above who right-clicked to choose the default program was right, however the path should be: C:\Windows\System32\wscript.exe

Resources