run command line as admin

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

run command line as admin

07 May 2024, 12:16

hello. i have the following snippet of code (provided below). the code works when run in an elevated powershell. when i integrated it to ahk, running the script without admin rights and my code doesn't work. i tried running the ahk script as admin and it worked. my question is. what approaches are there for this?
1. i can run the entire script as admin and run the command line no problem (altho i prefer the second option).
2. i can run an elevated powershell through autohotkey to run the command line (altho i have no clue how, the "run" function doesn't have an "admin" parameter)
what do you think is the better approach and how to implement it?
also in any case i don't want text boxes popping out asking "are you sure you want to run as admin", i want this done the hidden way.
thanks in advance

Code: Select all

^numpad1::{
    Run "PNPUTIL /disable-device /deviceid `"MY_HID_HERE_123`""

}
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 12:21

Hello,

AHK can accommodate this specific need directly, to run an individual command line as admin.
The *RunAs verb may be used in place of the Run as administrator right-click menu item.
Source: Run / RunWait - Syntax & Usage | AutoHotkey v2
Example
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 12:46

the example you referenced attempts to run the entire ahk script as admin (with a dialog box). i want to just run that specific line as admin, is this possible?
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 12:55

As I mentioned, AHK can run an individual command line as admin. Instead of specifying the script as the target of your "Run *RunAs" function call, you can specify any other command line.
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 12:59

okay i get your point but i have no idea how to use it. the quotation marks are confusing me. is this correct syntax?
Run '*RunAs "PNPUTIL /disable-device /deviceid `"MY_HID`""'
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 13:09

I think you will need to try it. You have the right idea. A general point of guidance would be like this.
  1. Take a command line that would work in Windows.
  2. Put single quotation marks around it.
  3. Insert *RunAs inside the first quotation mark.
  4. Insert Run before the entire quoted string.
You may need to fiddle with where the quotation marks are. Read the remarks in the documentation for Run.

With AHK v2, if you are using single quotation marks to quote your strings, then you would not need to escape a double quotation mark inside such a string. The cited pages contain examples. Use of ComSpec might require an extra set of quotation marks as noted in the remarks.
Is this correct syntax?
As I sometimes say, to learn whether your script works, run your script! :)
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 13:17

yeah i ran it and i'm getting "the system cannot find the file specified ".
Error: Failed attempt to launch program or document:
Action: <PNPUTIL /disable-device /deviceid "MY_HID" >
Verb: <RunAs>
Params: <>

another opinion: how about we use the approach in run as documentation?

Code: Select all

RunAs "Administrator", "MyPassword"
Run "RegEdit.exe"
RunAs  ; Reset to normal behavior.
altho i tried putting my username and password and i get "username or password is incorrect".
1. which approach you think is cleaner / better (if any)?
2. any clue on how to solve the problem?
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 13:31

You can experiment and use the approach that works. With your original attempt, I would put the full path to your program in double quotation marks. You will see an example of that in the documentation that I have cited.

Any variables defined within your script would be used as unquoted expressions.

Code: Select all

#Requires AutoHotkey v2.0
param := 'C:\Windows'
app   := 'C:\Windows\explorer.exe'
commandLine := '"' app '" "' param '"'
MsgBox commandLine, 'Command line', 'Iconi'
Run '*RunAs ' commandLine
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 14:06

i tried this. powershell windows flashes for a split second then closes and the device isnot disabled.

Code: Select all

    param := 'PNPUTIL /disable-device /deviceid `"my_hid`" '
    app   := 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
    commandLine := '"' app '" "' param '"'
    Run '*RunAs ' commandLine
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 14:09

It appears to me that you have not quoted your device properly. What is your exact command line that works in a Windows batch file?

It is surely not this one, which you would have seen with the MsgBox.

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "PNPUTIL /disable-device /deviceid "my_hid" "

Incidentally, you might want to see the other forum posts that show how to use AHK to run (specifically) a PowerShell script.
Last edited by mikeyww on 07 May 2024, 14:11, edited 1 time in total.
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 14:10

i never ran it in a batch file. i tried just the code in powershell when launched as admin
edit 1: ook will look into it. can i just know how i can solve my other problem. the issue with logging in providing username and password (i might use this approach for other things too). this is my code (if useful).

Code: Select all

    RunAs "pc-1", "password-123"
    Run "PNPUTIL /disable-device /deviceid `"my_hid`" "
    RunAs
again the problem is incorrect password. however, if i remove the line with "run ..." and put a msgbox it works fine (no error), but when i bring back the run function i get the error
Last edited by marypoppins_1 on 07 May 2024, 14:38, edited 1 time in total.
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 14:12

You might want to adapt one of the existing AHK scripts that runs a PowerShell script.

viewtopic.php?p=461481#p461481

viewtopic.php?f=76&t=82830
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

07 May 2024, 14:57

not sure if you saw the edit of my last message. anyway i fixed the issue and now it works. debugged using "-noexit" and fixing the ampersand in ahk.
i want to now fix the issue of using runas with my personal account. how do we go about this?

Code: Select all

    RunAs "user1" , "password123" , "@domainname"
   
    param := 'PNPUTIL /disable-device /deviceid `"HID\abc`" '
    app   := 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
    commandLine := '"' app '" -noexit "' param '"'
    ; MsgBox commandLine, 'Command line', 'Iconi'
    Run  commandLine 
    RunAs
i want to use runas approach in case it saves me the prompt of "are you sure you want to run as admiin ...."
my problem: username or password is incorrect no matter what i do
Last edited by marypoppins_1 on 07 May 2024, 15:30, edited 1 time in total.
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: run command line as admin

07 May 2024, 15:24

Sorry I'm not more directly helpful here, but I cannot directly test this and do not use PowerShell a lot. If you uncomment the MsgBox, however, I think you will see that the quotation marks surrounding HID are misplaced-- I think. I cannot tell you the right command line because I do not know what syntax your PNPUTIL command should use or would normally use.

Below is an alternative that you may wish to test.

Code: Select all

#Requires AutoHotkey v2.0
app   := A_WinDir '\System32\WindowsPowerShell\v1.0\powershell.exe'
param := 'PNPUTIL /disable-device /deviceid "HID\a`'&`'b`'&`'a`'&`'c"'
commandLine := '"' app '" -noexit ' param
MsgBox commandLine
If I were doing this, I would test your command line in a Windows batch file and get it working there first.

Perhaps others here will know more about your specific issues that remain with RunAs.
User avatar
xMaxrayx
Posts: 201
Joined: 06 Dec 2022, 02:56
Contact:

Re: run command line as admin

08 May 2024, 02:40

Powershell may not treated nice unless you allow it in windows.
my suggestion is make a bat/powershell script file and the AHK hotkey just normal command to run that script, it's slower and look not nice but it always work.
and you can add Admin-right checker to that bat/ps1 script.
make sure that "script path" is on allowlist in windows.
-----------------------ヾ(•ω•`)o------------------------------
https://github.com/xmaxrayx/
marypoppins_1
Posts: 123
Joined: 28 Oct 2020, 02:58

Re: run command line as admin

08 May 2024, 05:30

okay i guess i can make this. if i want to disable / enable the device do i make 2 script files or is there a way to put them both in one file and trigger each when needed?
also
xMaxrayx wrote:
08 May 2024, 02:40
make sure that "script path" is on allowlist in windows.
what do you mean? and hhow is this done?

edit 1: now that i think of it (more), i can make a separate ahk script to run as admin (better, no?). it's one script i won't modify / test in it so no risk. at the same time i want to make a hotkey with a toggle (case 1 change tray icon and enable device, both should be doable since script is admin, case 2: change tray icon and disable device..)
i just have one question. how can we make multi line strings that are taken literally and directly pasted into the run (of the windows powershell) without giving me autohotkey syntax errors (evading quotes with `, and putting & in a string to be understood as "string")

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: diegg1739 and 19 guests