launch a hidden cmd window administrative(with no prompt)?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
taripo
Posts: 228
Joined: 01 Aug 2011 13:48

launch a hidden cmd window administrative(with no prompt)?

#1 Post by taripo » 15 Mar 2014 11:22

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?

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: launch a hidden cmd window administrative(with no prompt

#2 Post by aGerman » 15 Mar 2014 17:27

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).

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
That's it. Once you need an elevated command prompt just doubleclick the shortcut and it will open without UAC confirmation.


Regards
aGerman

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: launch a hidden cmd window administrative(with no prompt

#3 Post by taripo » 15 Mar 2014 18:48

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.

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 "=ÿþ"

Image

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: launch a hidden cmd window administrative(with no prompt

#4 Post by aGerman » 15 Mar 2014 19:17

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

taripo
Posts: 228
Joined: 01 Aug 2011 13:48

Re: launch a hidden cmd window administrative(with no prompt

#5 Post by taripo » 15 Mar 2014 20:00

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

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: launch a hidden cmd window administrative(with no prompt

#6 Post by aGerman » 16 Mar 2014 04:56

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.:

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

Post Reply