i have a batch will with a small script as below. Now i want to modify it with RUNAS so that I can provide it a user without having the need to give him administrator password.
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~xyz0.ps1""' -Verb RunAs}"
PAUSE
I added the RUNAS with a few parameters, as shown below, but it encounters an error:
ECHO OFF
runas /profile /user:Administrator /savecred "PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~xyz0.ps1""' -Verb RunAs}""
PAUSE
ERROR:
Attempting to start PowerShell.exe -NoProfile -Command as user
"ABC\Administrator" ... '{Start-Process' is not recognized as an
internal or external command, operable program or batch file. Press
any key to continue . . .
PS: without introducing RUNAS, the batch file runs just fine.
can anyone help, please.
Related
How do I have to escape the quotation marks in this batch code?
I have the following code:
echo ""powershell -Executionpolicy Bypass -Command "get-clipboard > C:\File.txt""" > C:\Helptool.cmd
This code should generate the file "Helptool.cmd". The content should be as follows:
powershell -Executionpolicy Bypass -Command "get-clipboard > C:\File.txt
Where do I have to put ^ characters in the first batch code?
When I run the above code, the contents of the newly created batch file are incorrectly as follows:
"" powershell -Executionpolicy Bypass -Command "get-clipboard> C: \ File.txt" ""
The two quotes at the beginning and the three quotes at the end are undesirable. I'll get rid of them by escaping. or? Just how do I go about this?
You do not need any carets, ^, if you doublequote the entire PowerShell -Command:
This version uses the backslash to escape the nested doublequotes:
#Echo #"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Clipboard -Format Text -Raw -TextFormatType UnicodeText > \"C:\File.txt\"" > "C:\Helptool.cmd"
If you prefer to use singlequotes in PowerShell, there's nothing to escape:
#Echo #"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -Command "Get-Clipboard -Format Text -Raw -TextFormatType UnicodeText > 'C:\File.txt'" > "C:\Helptool.cmd"
Please note, that I have removed your -ExecutionPolicy parameter, as it is not needed for running -Commands, (only PowerShell -Files). The full path to, and extension for, the powershell executable, are optional, but recommended, as is the -NoProfile parameter. You should open a Windows PowerShell window, and read the output from Get-Help Get-Clipboard -Full, to find out what the -Format, Raw, and -TextFormatType parameters do.
I have a simple PowerShell script that using windows.forms for presenting and getting data using GUI.
It works when I run it using PowerShell , but doesn't work via CMD.
Here is the .ps1 example :
[System.Windows.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
batch file for executing :
powershell "&{start-process powershell -ArgumentList ' -noprofile -file c:\temp\gui.ps1' -verb RunAs} exit $LASTEXITCODE" < NUL
Th error I get is : Unable to find type [System.Windows.MessageBox].
I understood that the CMD running with different context and the assembly need to be loaded , so I tried to load it from the CMD command , but still same error .
powershell "&{[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); start-process powershell -ArgumentList ' -noprofile -file c:\temp\gui.ps1' -verb RunAs} exit $LASTEXITCODE" < NUL
Error:
Any idea ?
You could try something like this:
PowerShell -NoProfile -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\temp\gui.ps1""' -Verb RunAs}"; Exit $LastExitCode
this is completely untested
OK I figure it out.
The issue was with the PowerShell script,
I used a [System.Windows.MessageBox] instead a [System.Windows.Forms.MessageBox]
Before:
[System.Windows.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
After:
[System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
It works without changing the batch file.
Thanks,
Try running the following in the batch file:
powershell -command [System.Windows.Forms.MessageBox]::Show('message','Step 1','YesNoCancel','Question')
I hope this is helpful, if you have any other questions, please comment below.
I am trying to write a bat file that will accomplish the following:
psexec \\host1 cmd
d: #to change the remote server's drive
cd\
dir /s/b "file1" #searches for this file in host1 in its d: drive
How can I go about doing this
If you are on a current day Windows system, you can invoke the command on a remote computer using PowerShell from within your cmd .bat script. No need for psexec. The remote machine does need to be setup for PowerShell remoting. Get-Help about_Remote
powershell -NoProfile -Command "invoke-command HOST01 { cmd /C dir /S /B D:\file1 }"
If you are running in PowerShell:
invoke-command HOST01 { cmd /C dir /S /B D:\file1 }
Of course, in PowerShell you might as well use PowerShell cmdlets.
icm HOST01 { gci -n -rec D:\file1 }
-or-
Invoke-Command HOST01 { Get-ChildItem -Name -Recurse D:\file1 }
psexec \\host1 cmd /c "d: & cd\ & dir /s/b file1"
or simply
psexec \\host1 cmd /c "dir /s/b d:\file1"
The console in which the cmd is executed is automatically closed when the command finishes executing, so you won't actually see the result. You could leave cmd running (and console along with it) by using /k instead of /c, but that doesn't make much sense either. You appear to have an XY problem.
The parameter value "/user:GLOBAL\svc-prodapp" is not accepted at this path.
Need help in understanding if there is any issue with the syntax.
Below is my BAT File code:
PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs /user:GLOBAL\svc-prodapp}"
sc \\ABCDEVSQL002 stop "MSSQLServerOLAPService"
sc \\ABCDEVSQL002 start "MSSQLServerOLAPService"
$timeoutSeconds = 240
How do I run a batch file from another batch file with administrator rights?
I have tried the RUNAS command, but it requires the administrator password.
I am searching for an alternative for running a batch file by right clicking on it and running as an administrator. I want to automate it from another batch file.
Put each line in cmd or all of theme in the batch file:
#echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"
it works fine for me.
On Windows 7:
Create a shortcut to that batch file
Right click on that shortcut file and choose Properties
Click the Advanced button to find a checkbox for running as administrator
Check the screenshot below
You can use PowerShell to run b.bat as administrator from a.bat:
set mydir=%~dp0
Powershell -Command "& { Start-Process \"%mydir%b.bat\" -verb RunAs}"
It will prompt the user with a confirmation dialog. The user chooses YES, and then b.bat will be run as administrator.
Use
runas /savecred /profile /user:Administrator whateveryouwanttorun.cmd
It will ask for the password the first time only. It will not ask for password again, unless the password is changed, etc.
If you're trying to invoke a Windows UAC prompt (the one that puts the whole screen black and asks if you're granting administrator privileges to the following task), RUNAS is not the smoothest way to do it, since:
You're not going to get prompted for UAC authorization, even if logged in as the administrator and
RUNAS expects that you have the administrator password, even if your user is setup as a local administrator, in which case the former password is not a sound security practice, specially in work environments.
Instead, try to copy & paste the following code to ensure that your batch file runs with administrator privileges:
#echo off
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo Requesting Admin access...
goto goUAC )
else goto goADMIN
:goUAC
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:goADMIN
pushd "%CD%"
CD /D "%~dp0"
rem --- FROM HERE PASTE YOUR ADMIN-ENABLED BATCH SCRIPT ---
echo Stopping some Microsoft Service...
net stop sqlserveragent
rem --- END OF BATCH ----
This solution works 100% under Windows 7, 8.1 and 10 setups with UAC enabled.
Runas.exe won't work here. You can use VBScript to invoke the "Run as Administrator" shell verb. The Elevation Powertoys contain a batchfile that allows you to invoke an elevated command:
elevatecmd.exe
http://blogs.technet.com/b/elevationpowertoys/
CMD Itself does not have a function to run files as admin, but powershell does, and that powershell function can be exectuted through CMD with a certain command. Write it in command prompt to run the file you specified as admin.
powershell -command start-process -file yourfilename -verb runas
Hope it helped!
The complete solution I found that worked was:
#echo off
cd /D "%~dp0"
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
"Put your command here"
credit for:
https://stackoverflow.com/a/51472107/15087068
https://serverfault.com/a/95696
This a trick that i used if anyone wants they can try this in batch file.This will give you the admin prompt when you run the batch file
#echo off
cd \ && cd windows/system32 && command which needs admin credentials
pause