Reputation: 157
I am trying to run one command as administrator but I am getting error. Below is the code.
VBScript:
MyPath ="C:\Destination"
Dim objShell
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "C:\batchScript.cmd " & MyPath &, "", "", "runas", 1
batchScript.cmd
:
echo %1
psfile %1 -c
Upvotes: 1
Views: 1209
Reputation: 200553
Wild guess (since you didn't deem it necessary to actually show the error message): you're using the concatenation operator (&
) without something to actually concatenate:
objShell.ShellExecute "C:\batchScript.cmd " & MyPath &, "", "", "runas", 1
' ^
and you're also passing arguments as part of the command.
Remove the spurious concatenation operator and pass the argument as an actual argument:
objShell.ShellExecute "C:\batchScript.cmd", MyPath, "", "runas", 1
Also, $args[0]
is not a valid variable in a batch file.
Upvotes: 1
Reputation: 9250
This is not my work, but I found it really a nice peace of code:
@echo off
:: ------- Self-elevating.bat --------------------------------------
@whoami /groups | find "S-1-16-12288" > nul && goto :admin
set "ELEVATE_CMDLINE=cd /d "%~dp0" & call "%~f0" %*"
findstr "^:::" "%~sf0">temp.vbs
cscript //nologo temp.vbs & del temp.vbs & exit /b
::: Set objShell = CreateObject("Shell.Application")
::: Set objWshShell = WScript.CreateObject("WScript.Shell")
::: Set objWshProcessEnv = objWshShell.Environment("PROCESS")
::: strCommandLine = Trim(objWshProcessEnv("ELEVATE_CMDLINE"))
::: objShell.ShellExecute "cmd", "/c " & strCommandLine, "", "runas"
:admin -------------------------------------------------------------
@echo Running as elevated user.
@echo Script file : %~f0
@echo Arguments : %*
@echo Working dir : %cd%
Use it as header for your CMD and it will do the rest.
Upvotes: 3