Page 1 of 1

run command line as admin

Posted: 07 May 2024, 12:16
by marypoppins_1
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`""

}

Re: run command line as admin

Posted: 07 May 2024, 12:21
by mikeyww
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

Re: run command line as admin

Posted: 07 May 2024, 12:46
by marypoppins_1
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?

Re: run command line as admin

Posted: 07 May 2024, 12:55
by mikeyww
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.

Re: run command line as admin

Posted: 07 May 2024, 12:59
by marypoppins_1
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`""'

Re: run command line as admin

Posted: 07 May 2024, 13:09
by mikeyww
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! :)

Re: run command line as admin

Posted: 07 May 2024, 13:17
by marypoppins_1
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?

Re: run command line as admin

Posted: 07 May 2024, 13:31
by mikeyww
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

Re: run command line as admin

Posted: 07 May 2024, 14:06
by marypoppins_1
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

Re: run command line as admin

Posted: 07 May 2024, 14:09
by mikeyww
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.

Re: run command line as admin

Posted: 07 May 2024, 14:10
by marypoppins_1
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

Re: run command line as admin

Posted: 07 May 2024, 14:12
by mikeyww
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

Re: run command line as admin

Posted: 07 May 2024, 14:57
by marypoppins_1
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

Re: run command line as admin

Posted: 07 May 2024, 15:24
by mikeyww
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.

Re: run command line as admin

Posted: 08 May 2024, 02:40
by xMaxrayx
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.

Re: run command line as admin

Posted: 08 May 2024, 05:30
by marypoppins_1
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")