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.
Related
Am opening windows cmd.exe as administrator and executing an installer(just call installer.exe) and it runs fine. But if I open the cmd.exe in normal mode ( not as administrator) but run the command as user administrator ( runas /profile /user:adminstrator installer.exe) am not able to execute the installer successfully.
The installer unpacks certain files in c:\users\ dir.
The error that I get is :
"Error running java -Dpython.console.encoding=UTF-8 -jar C:\users\<username>/tools/x.jar : Program ended with an error exit code. "
How can I solve this issue? Since am trying to automate executing this installer, opening the cmd.exe as admin is out of question. I would like to run the command as a normal user or if not possible, as an admin.
Am new to Windows. Any help is appreciated.
Thanks
You could create the .bat file with the line you want to run, then follow the procedures listed below to have it automatically run as an administrator.
Right click on the original file and click Create shortcut.
Right click on the shortcut and select the properties option.
Under the shortcut tab, click on the advanced button in the bottom right hand corner.
Check the box that says run as administrator
Click ok, and then ok.
Now when you double click on the batch file shortcut it will run as an administrator. You can delete the original if you wish to.
At first: Yes, I know different answers like this, but this is not what I want ...
I have a Batch script (on Win7) which must be executed with Administrator rights. To do that the user must right click the Batch file and select "Runs as administrator" to execute the script. The "NET SESSION", "AT" and so on solutions of answers like in the link above doesn't not work, because "NET SESSION" returns me the same result: there is no different if I start the Batch file with a double click or if I start it while selecting "Run as Administrator". But I need to find out if the Batch file is started with "Run as administrator" ...
Any Solutions for that?
Ok. Thanks for your comments. Today I figure out what happens.
Our admin has change the configuration of the cmd.exe application so that the cmd.exe starts in every case with administration rights. So theres no different between a double click on "cmd.exe" and starting it with "Run as Adminstrator". And so my script returns the same result in both cases!
any idea why this is not working?
runas.exe /user:Administrator "cmd /c explorer"
it returns:
Windows cannot find the specified file. You may not have appropriate
permissions.
the following works by itself:
cmd /c explorer
thx!
Answer found here
http://winaero.com/blog/how-to-run-explorer-as-administrator-on-windows-8-1-windows-8-and-windows-7/
"Microsoft does not allow running Explorer as administrator in Windows 8.1/8 (and in Windows 7 too). The solution to run Explorer elevated...
1- take ownership of registry key below.
Right-click, Permissions, Advanced, Owner Change
or, Use Winaero's RegOwnershipEx application, which allows you to take ownership of registry keys
HKEY_CLASSES_ROOT\AppID{CDCBCFCA-3CDC-436f-A4E2-0E02075250C2}
2- rename or delete the value named 'RunAs'.
3- now Restore Ownership (this failed for me done manually-- i could not re-add TrustedInstaller --user not found).
4- at this point, you will be able to run explorer as admin
5- this process does not cleanly exit when you close the Explorer window! You must remember to terminate it every time you elevate Explorer after you are done working in the Explorer window.
Alternate solution: use Winaero's ELE.exe app to start any program as administrator from the command line.
Usually one wants to run as an Administrator to edit a file, or get permissions to delete a file:
To get to the permissions on a file you can right click on it and select Properties. Then select Security. You will see this dialog:
Then Click on Edit and change the permissions for the relevant user (e.g Mike).
You need to have admin privileges to do this, which can be set via User Account Control.
The File Permissions window looks like this:
Edit as required.
I have admin rights but Windows 8.1 still protects some system files (such as the Android studio.exe.vmoptions) and I used the above method to give me write access to the file.
Hope this helps.
You could always open a dos prompt as Administrator then go to the directory in question then do a Explorer . then it will open the location in question as the elevated user.
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"
I'm trying to run a batch file as admin. I found that I can use runas command which corresponds sudo command in Linux I think.
I tried
runas /noprofile /user:computername\adminuser "blah.bat start"
But it gives an error, saying :
Logon failure: user account restriction.. (msdos window doesn't allow me to copy anything) is there any way I can run this batch file as admin? Right click doesn't work because I can't include any parameters.
A workaround: You can create a shortcut to the batch file, add a parameter in the shortcut, then right-click to run the shortcut as admin.
Right-click the icon for the command-prompt and choose Run As Administrator. Then run you batch file from that window.
I believe that you can allow or disallow the RunAs command with the registry.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer
"HideRunAsVerb"= 1
See Disabling the RunAs Command