Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: run cmd as admin

Hello experts,

The following AutoHotkey allows me to run cmd as admin.
;====================================
;Run Cmd
;====================================
#!c::
If !(A_IsAdmin) ;you are not admin
;If (A_IsAdmin) you are admin
{
;~ Run,%comspec% /k
  Run, *RunAs %comspec% /k
  Return
}
Else
{
  msgbox,ADMIN=%A_IsAdmin%
  Return
}

Open in new window


I was wondering if there is a way to put an error level and review the procedure if necessary.
Thank you for your help.
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Luis Diaz

ASKER

Ok Joe, I will read the documentation.Thank you.
Joe,

Thank you again for those links. They help me to discover another approach rather than ErrorLevel.
I like the try, catch approach as it is user friendly to manage and in the meantime I can protect AutoHotkeys/Hotstrings in case of errors.
As regards my previous proposal I was wondering if I can proceed like this:
;====================================
;Run Cmd
;====================================
#!c::
try
{
  If !(A_IsAdmin) ;you are not admin
  ;If (A_IsAdmin) you are admin
{
  ;~ Run,%comspec% /k
  Run, *RunAs %comspec% /k
  Return
}
Else
{
  msgbox,ADMIN=%A_IsAdmin%
  Return
}
}
catch
{
  MsgBox, 16,, There was a problem while trying to access to cmd
  ExitApp
}

Open in new window

I tested in one of my computer with Win10 and it works. However I don't know how can I simulate the catch block.
Also, concerning try/catch approach I didn't see in the documentation the option to manage time-out . Example try an action limited to 25 seconds if actions couldn't be performed catch and report that there was an error because time expired. In that case I suppose that I need to going through a loop time-out.
Thank you in advance for your feedback.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,
Exactly what I was looking for. Catch error in case that run as admin or not as admin failed. I added your previous comments in my knowledge base.
Thank you very much for y our help.
Hi Luis,

> Exactly what I was looking for.

Great news!

> I added your previous comments in my knowledge base.

Glad to hear it! After it gets built up, I'm sure you'll find it to be very valuable. As I mentioned earlier, I have more than 4,300 entries in mine and not a day goes by that I don't use it. This thread is going in there right now. :) Regards, Joe
Thank you Joe!