Disable network (wifi), retrieve output, but elevated admin needed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
RUBn
Posts: 51
Joined: 27 Nov 2013, 05:12
Contact:

Disable network (wifi), retrieve output, but elevated admin needed

16 Aug 2015, 21:58

I am almost done with making my new "disable wifi" script. The only problem is that, because I want to retrieve the output, I can't get an elevated prompt. "RunAs" doesn't work with ComObjects out of the box.
Anyone any idea?

Code: Select all

RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Execute a single command via cmd.exe
    exec := shell.Exec(ComSpec " /C " command)
    ; Read and return the command's output
    return exec.StdOut.ReadAll()
}
#^+w::MsgBox % RunWaitOne("netsh interface show interface")			;	<-- Show netwotk interfaces
#!w::			;	<-- Disable wifi
	RunAs Admin,,
	MsgBox % RunWaitOne("netsh interface set interface name=""Wi-Fi"" disable")
	RunAs
	returnn
P.S. I found this related topic (Script needs to run cmd as admin ), but it's a bit above my head. If the answer is in there, can anyone tell me how to aplly that to my goal?

I am also: Image-motiv for professional, volunteer & open source (web) development
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Disable network (wifi), retrieve output, but elevated admin needed

17 Aug 2015, 08:10

I have used this code for years to force a script to run as admin:

Code: Select all

	run_as_admin(){
		Global 0
		IfEqual, A_IsAdmin, 1, Return 0
		Loop, %0% {
			params .= A_Space . %A_Index%
		}
		DllCall("shell32\ShellExecute" (A_IsUnicode ? "":"A"),uint,0,str,"RunAs",str,(A_IsCompiled ? A_ScriptFullPath
			: A_AhkPath),str,(A_IsCompiled ? "": """" . A_ScriptFullPath . """" . A_Space) params,str,A_WorkingDir,int,1)
		ExitApp
	}
There may be better versions in the thread(s) you linked, but I have not tested those.
Simply call it at the start of your script and it will run as admin (You will get a UAC popup if UAC is on).
User avatar
RUBn
Posts: 51
Joined: 27 Nov 2013, 05:12
Contact:

Re: Disable network (wifi), retrieve output, but elevated admin needed

18 Aug 2015, 13:54

Thanks evilC.
But do you mean that my whole AHK script will run as administrator with this? If so, I have my doubts that I want to do that. I am afraid that other hotkeys and stuff and some miscodings now and then can then do bad things with the health of my system. (Also, if kids ever touch the wrong keys on the wrong moment ..) I also think that if I will have to remove that popup every time I restart my PC (this script always runs), I will lose more time thatn I would win with this hotkey, though I would probably find a way to make Windows trust this script, dunno, it's the first thing that worries me most.

So, isn't there a way to make just this particular hotkey work as admin, (also with UAC popup if nessecary)?

I am also: Image-motiv for professional, volunteer & open source (web) development
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Disable network (wifi), retrieve output, but elevated admin needed

18 Aug 2015, 14:30

Yes, the whole script will run as admin.
Running a script that you wrote (ie you know it isnt malicious) as admin is not inherently dangerous. If the script isn't told to do anything dodgy (like delete the windows folder) then I very much doubt kids / fat fingering is going to hose your system.

Yeah, the popup bit is annoying though, especially for a script you have start on boot - although I think there may be a way to "sign" scripts (compile it and sign exe?) so that UAC does not activate for that script.

A cursory search turns up these threads that may be of use:
http://www.autohotkey.com/board/topic/7 ... -programs/
http://ahkscript.org/boards/viewtopic.php?t=4334
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Disable network (wifi), retrieve output, but elevated admin needed

18 Aug 2015, 14:54

Wow, you actually did me a favour.

I checked out SKAN's RunAsTask() and it does what my run_as_admin() does, but it only ever triggers UAC once.
Even when you reboot, it doesn't ask ever again.

Highly recommended.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 219 guests