Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Script needs to run cmd as admin



  • Please log in to reply
5 replies to this topic
Thatkookooguy
  • Members
  • 8 posts
  • Last active: Oct 04 2013 07:37 PM
  • Joined: 08 Sep 2013

I'm trying to run cmd using ahk as admin on a computer not connected to the internet (therefore there's no need for an Admin password).

This is my code:

RunAs Admin,,
RunWait %comspec% /c chdir %A_ScriptDir% | Win7BootUpdaterCmd.exe my_animation_boot.bs7 ;,,Hide
RunAs

The problem is that Win7BootUpdaterCmd.exe requires admin privileges. Which means I need to open cmd as admin.

 

The code gives me the error: "Logon failure: user account restriction. Possible reasons are blank passwords not allowed, logon hou...".

 

So the first thing I tried was to add a password to the admin account currently running and trying again. This actually doesn't give any errors but the cmd window closes really fast and it seems nothing happened. (Win7BootUpdaterCmd should run for about a minute so I guess it didn't even start. plus, I checked the task manager and it didn't run in the bg either).

 

After that I thought that maybe if I ran the ahk script as admin it will give the admin privileges automatically to programs it runs (just as cmd with admin priv will give admin privs to Win7BootUpdaterCmd)

So I found this ahk script here on the forum which gives the running script admin privileges. I changed the code as followed:

RunAsAdmin()
;RunAs Admin,Password,
RunWait %comspec% /c chdir %A_ScriptDir% | Win7BootUpdaterCmd.exe my_animation_boot.bs7 ;,,Hide
;RunAs


RunAsAdmin() {
  Loop, %0%  ; For each parameter:
    {
      param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
      params .= A_Space . param
    }
  ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
      
  if not A_IsAdmin
  {
      If A_IsCompiled
         DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
      Else
         DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
      ExitApp
  }
}

This code ran, but cmd ran without admin privileges so Win7BootUpdaterCmd couldn't do it's job.

 

 

I'm lost -___-

help? :-)

 

Thanks,

Neil



Seidenweber
  • Moderators
  • 638 posts
  • Last active: Sep 06 2015 01:51 PM
  • Joined: 10 May 2011

This should do the trick: Use Task Scheduler to Launch Programs
 
Create a task for Win7BootUpdaterCmd.exe with highest privileges.
 
Then you can run your task from your script with

Run C:\Windows\System32\schtasks.exe /run /tn "TaskName" ; Where TaskName is the name you have chosen.

All questions & answers are related to AHK 1.1.19.03 x64 Unicode

 


AfterLemon
  • Moderators
  • 237 posts
  • Connoisseur of Boobs
  • Last active: Jan 04 2016 11:12 PM
  • Joined: 09 Oct 2012
If you're going to use a check to automatically run your script as admin if it is not, you MUST put this first.

EDIT: Did NOT notice the order of the brackets. Thanks Lexicos.

Edited by AfterLemon, 16 September 2013 - 03:54 PM.


Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer

This code ran, but cmd ran without admin privileges so Win7BootUpdaterCmd couldn't do it's job.

I don't see how that's possible. When I run your script (after removing the args from %comspec%), it brings up a UAC prompt, then launches an "Administrator:" command prompt. This is exactly what I expected.

When the script is running as admin, anything it launches also runs as admin. It's actually complicated to run something NOT as admin.

I'm not sure what effect ShellExecute-RunAs has if you call it from a limited user with UAC disabled. If you didn't get a UAC (or Run As) prompt at all, that could be the issue. But in that case, the function would exit the script because it wouldn't have admin rights, and the exe would never run.
 

Your script is running the function, running the exe, then checking if it has admin rights. Probably need to put that check at the top.

You are mistaken. The script is running the function first, which is then performing the check for admin. After the function returns (if the script hasn't exited), the exe is run.

Thatkookooguy
  • Members
  • 8 posts
  • Last active: Oct 04 2013 07:37 PM
  • Joined: 08 Sep 2013

I don't see how that's possible. When I run your script (after removing the args from %comspec%), it brings up a UAC prompt, then launches an "Administrator:" command prompt. This is exactly what I expected.

When the script is running as admin, anything it launches also runs as admin. It's actually complicated to run something NOT as admin.

 

You are absolutely right! After seeing your post I tried removing the args myself, and it did work!

 

So.... I tried to ran everything by hand and saw everything worked ok. The REAL problem was (and I'm kinda ashamed to say so) I accidentally saved the OLD boot logo upon the NEW boot logo so the boot didn't change and I assumed the problem was with the code....................... oops ^_^;;;;;;;;;;;;;

 

 

So now everything works as planned.

 

I needed this for a script that runs at start-up and randomly chooses (with no repeats) a different boot logo animation (and text) from a folder of boot logos for the next time you'll start your computer.

I did this for my arcade cabinet. Since I wanted to hide windows completely from it, I wanted the boot sequence to be unique and old 8-bit games related. On my arcade, every time I boot, there's a different 8-bit animation (donkey kong, super-mario, sonic, another world etc.).

 

If anyone wants  this script I'll be happy to share so just ask

 

Thanks for everybody here who helped!



oifii
  • Members
  • 2 posts
  • Last active: Aug 27 2014 11:18 PM
  • Joined: 24 Jan 2011

we have no Administrator password, is it possible to runas Administrator not specifying any password?