Page 1 of 1
Launch Elevated CMD - how?
Posted: 31 Jul 2015 11:39
by SkyFrontier
Hi, there.
How could I launch an elevated CMD prompt with, say, "netsh wlan start hostednetwork" ready to be ENTER'ed?
Re: Launch Elevated CMD - how?
Posted: 31 Jul 2015 11:53
by highend
You mean: Dos box open, and you still need to press enter to execute that specific command?
Code: Select all
$comspec = ("%osbitness%" == 64) ? "%windir%\System32\cmd.exe" : "%windir%\SysWOW64\cmd.exe";
$cscript = ("%osbitness%" == 64) ? "%windir%\System32\cscript.exe" : "%windir%\SysWOW64\cscript.exe";
$vbsFile = "%TEMP%\~OpenElevatedCMD.vbs";
$vbsContent = <<<>>>
Set UAC = CreateObject("Shell.Application")
UAC.ShellExecute "$comspec", "/k ECHO netsh wlan start hostednetwork & PAUSE >NUL & netsh wlan start hostednetwork", "", "runas", 1
>>>;
writefile($vbsFile, trim($vbsContent));
run """$cscript"" ""$vbsFile"" //nologo", , 0, 0;
If you want to write that string after the
prompt of the dos box you probably could do it with an .ahk script (untested)...
Re: Launch Elevated CMD - how?
Posted: 31 Jul 2015 12:12
by highend
If the command needs to be editable...
Code: Select all
#NoEnv
#SingleInstance, Force
#NoTrayIcon
if not A_IsAdmin
{
Run, *RunAs "%A_ScriptFullPath%"
ExitApp
}
Run, %comspec% /k, , , pid
WinWaitActive, %comspec% ahk_pid %pid%
ControlSend, , netsh wlan start hostednetwork, ahk_pid %pid%
Re: Launch Elevated CMD - how?
Posted: 31 Jul 2015 13:17
by SkyFrontier
I was afraid that it was no direct way which would avoid bat files creation. That's why I thought at least a "open elevated, sendkeys(<parameters>), wait for user to hit enter" operation would be possible.
Thanks again... nice day there!
Re: Launch Elevated CMD - how?
Posted: 31 Jul 2015 13:29
by highend
Open a command prompt and using sendkeys would work but...
1. XY needs to be started with admin permissions to be able to open a command prompt with the necessary rights
2. What happens if you're working while you execute that script? The sendkeys can fail (send keys to the wrong window)
3. XY has no internal way to make sure a specific window is active before executing the sendkeys command
The .ahk way is the easiest and it doesn't create temporary files. Con: big (compiled) file size
Re: Launch Elevated CMD - how?
Posted: 31 Jul 2015 13:39
by SkyFrontier
In this specific case I'm trying to automate the dumb operations themselves, no extra scripting intended.
I noticed there's no buffer for me to re-use the launched command by pressing up/down arrow keys, so sendkeys() could be a solution. I'll work later on this and report back if I stumble into dumbness.

Re: Launch Elevated CMD - how?
Posted: 23 Aug 2015 18:37
by SkyFrontier
No other way I could keep the elevated CMD window opened, having the entered command at my disposal on arrow up, highend, thus avoiding future extra typing for reuse or edit?
Re: Launch Elevated CMD - how?
Posted: 24 Aug 2015 17:11
by totmad1
I was wondering if you had looked at nircmd from nirsoft it has two commands,
which might be relevant , elevate and elevatecmd the last one elevates its own commands.
The use of this utilty/cli does not require elevating XY.
Re: Launch Elevated CMD - how?
Posted: 24 Aug 2015 17:34
by yusef88
totmad1 wrote:I was wondering if you had looked at nircmd from nirsoft it has two commands,
which might be relevant , elevate and elevatecmd the last one elevates its own commands.
The use of this utilty/cli does not require elevating XY.
I use it like this
Code: Select all
run "nircmd elevate net user %username% *"
Re: Launch Elevated CMD - how?
Posted: 24 Aug 2015 22:20
by highend
having the entered command at my disposal on arrow up, highend, thus avoiding future extra typing for reuse or edit
Mh? You want a cmd window which has it's history included?
Re: Launch Elevated CMD - how?
Posted: 25 Aug 2015 02:11
by SFmob
if by history you mean the first (auto) entered command, 'netsh wlan...', in case, then yes, because it's base for further actions.
__
mob profile, sorry
Re: Launch Elevated CMD - how?
Posted: 25 Aug 2015 08:28
by highend
if by history you mean the first (auto) entered command
From your first post:
How could I launch an elevated CMD prompt with, say, "netsh wlan start hostednetwork" ready to be ENTER'ed?
You didn't want it "auto-entered"...
If that has changed:
Code: Select all
#NoEnv
#SingleInstance, Force
#NoTrayIcon
if not A_IsAdmin
{
Run, *RunAs "%A_ScriptFullPath%"
ExitApp
}
SetKeyDelay, 0
Run, %comspec% /k, , , pid
WinWaitActive, %comspec% ahk_pid %pid%
ControlSend, , netsh wlan start hostednetwork{Enter}, ahk_pid %pid%
Re: Launch Elevated CMD - how?
Posted: 28 Aug 2015 01:03
by SkyFrontier
Sorry, now I see I wasn't clear on that sentence.
Thanks much!
Is there a way to
Code: Select all
run """cmd.exe -netsh wlan start hostednetwork""";
, no prompt, no window flash?
Re: Launch Elevated CMD - how?
Posted: 28 Aug 2015 07:38
by highend
run - show = 0?
Re: Launch Elevated CMD - how?
Posted: 28 Aug 2015 08:30
by SkyFrontier
Forgot that one, thanks!