I would like to run a command from a regular cmd prompt, but the command requires administrative privileges. The command is "netstat -b >c:\blah\myfile.a" but that's not so relevant, as I might want the method to work for other commands too.
What can I do?
launch a hidden cmd window administrative(with no prompt)?
Moderator: DosItHelp
Re: launch a hidden cmd window administrative(with no prompt
You can't run it from a regular prompt. If you belong to the local administrators on the certain computer you could create a scheduled task that opens a command prompt with highest privileges.
To create such a sheduled task you could use a batch file (that you have to right-click and Run As Administrator once of course).
If the batchfile run successfully create a shortcut with targetThat's it. Once you need an elevated command prompt just doubleclick the shortcut and it will open without UAC confirmation.
Regards
aGerman
To create such a sheduled task you could use a batch file (that you have to right-click and Run As Administrator once of course).
Code: Select all
@echo off &setlocal
cd /d "%~dp0"
for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
>nul chcp 1252
set "taskname=Elevated CMD Prompt"
set "exec=C:\Windows\System32\cmd.exe"
set "args=/k cd /d "%userprofile%""
set "xmlfile=task.xml"
set "line_01=<?xml version="1.0" encoding="UTF-16"?>"
set "line_02=<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">"
set "line_03= <Triggers />"
set "line_04= <Principals>"
set "line_05= <Principal id="Author">"
set "line_06= <UserId>%userdomain%\%username%</UserId>"
set "line_07= <LogonType>InteractiveToken</LogonType>"
set "line_08= <RunLevel>HighestAvailable</RunLevel>"
set "line_09= </Principal>"
set "line_10= </Principals>"
set "line_11= <Settings>"
set "line_12= <Enabled>true</Enabled>"
set "line_13= <Hidden>false</Hidden>"
set "line_14= <Priority>5</Priority>"
set "line_15= </Settings>"
set "line_16= <Actions Context="Author">"
set "line_17= <Exec>"
set "line_18= <Command>%exec%</Command>"
set "line_19= <Arguments>%args%</Arguments>"
set "line_20= </Exec>"
set "line_21= </Actions>"
set "line_22=</Task>"
<nul >"%xmlfile%" set /p "=ÿþ"
>>"%xmlfile%" cmd /u /q /c "for /f "tokens=1* delims==" %%i in ('set line_') do echo %%j"
>nul chcp %oemcp%
schtasks /create /xml "%xmlfile%" /tn "%taskname%" /f
del "%xmlfile%"
pause
If the batchfile run successfully create a shortcut with target
Code: Select all
C:\Windows\System32\schtasks.exe /run /tn "Elevated CMD Prompt" /i
Regards
aGerman
Re: launch a hidden cmd window administrative(with no prompt
i'm trying to get it to work.
I got an error. So I did echo on and I added a pause and did a ctrl-c at the error.
I looked at task.xml I don't know if it should look like this but it is full of hex
http://pastebin.com/raw.php?i=qD4SsADB
here is the bat file a.bat that is the contents of your batch script copy/pasted in
http://ge.tt/api/1/files/5riWBXQ1/0/blob?download
I notice this line came up looking different on the cmd screen though maybe that's irrelevant <nul >"%xmlfile%" set /p "=ÿþ"

I got an error. So I did echo on and I added a pause and did a ctrl-c at the error.
Code: Select all
C:\abc>schtasks /create /xml "task.xml" /tn "Elevated CMD Prompt" /f
ERROR: The task XML is malformed.
(1,2)::
C:\abc>pause
Press any key to continue . . .
Terminate batch job (Y/N)? y
I looked at task.xml I don't know if it should look like this but it is full of hex
http://pastebin.com/raw.php?i=qD4SsADB
here is the bat file a.bat that is the contents of your batch script copy/pasted in
http://ge.tt/api/1/files/5riWBXQ1/0/blob?download
I notice this line came up looking different on the cmd screen though maybe that's irrelevant <nul >"%xmlfile%" set /p "=ÿþ"

Re: launch a hidden cmd window administrative(with no prompt
You saved the batch file with charset UTF-8. For that reason the Byte Order Mark (ÿþ) was written incorrectly to the XML file.
Use the Windows editor (notepad.exe) and make sure your chosen encoding is ANSI if you save the batch code.
Regards
aGerman
Use the Windows editor (notepad.exe) and make sure your chosen encoding is ANSI if you save the batch code.
Regards
aGerman
Re: launch a hidden cmd window administrative(with no prompt
thanks.
I see I can run blah.lnk and it opens an administrative command prompt window, or I can click blah.lnk in the start menu and it opens(though blinks for a moment before opening).
but let's say I have a batch file and most of it can run in the regular command window, but there is one command like netstat -b that needs administrative privileges.
And I have a regular cmd window open.
Is it possible for me to replace the netstat -b command in the batch file, with something else that launches an administrative window and runs the netstat -b e.g. netstat -b >file1 command?
For example, is it possible to cause that "elevate cmd prompt" task, to automatically run a particular command or a batch file?
thanks
I see I can run blah.lnk and it opens an administrative command prompt window, or I can click blah.lnk in the start menu and it opens(though blinks for a moment before opening).
but let's say I have a batch file and most of it can run in the regular command window, but there is one command like netstat -b that needs administrative privileges.
And I have a regular cmd window open.
Is it possible for me to replace the netstat -b command in the batch file, with something else that launches an administrative window and runs the netstat -b e.g. netstat -b >file1 command?
For example, is it possible to cause that "elevate cmd prompt" task, to automatically run a particular command or a batch file?
thanks
Re: launch a hidden cmd window administrative(with no prompt
At the beginning of my batch code I defined some variables. Just have a look at it and you will find that you could easily customize it. Eg.:
Now you have a task that will run "%temp%\elevated.bat". Whatever code you write into "%temp%\elevated.bat" -- it will be executed with administrative privileges.
Regards
aGerman
Code: Select all
set "taskname=Elevated Batch"
set "exec=C:\Windows\System32\cmd.exe"
set "args=/c "%temp%\elevated.bat""
set "xmlfile=task.xml
Now you have a task that will run "%temp%\elevated.bat". Whatever code you write into "%temp%\elevated.bat" -- it will be executed with administrative privileges.
Code: Select all
>"%temp%\elevated.bat" (
echo @echo off
echo ^>"c:\blah\myfile.a" netstat -b
)
schtasks /run /tn "Elevated Batch" /i
Regards
aGerman