Sign in to follow this  
Followers 0
Tumulus

Use Elevated CMD

7 posts in this topic

I need to create a new administrator account on a number of computers, so figured I would make a script to do it. 

You can only add/modify admin profile from an elevated command prompt (administrator command prompt). So I tried writing that into my short script. I thought that my "RunAs" command below would do the trick, but it opens a standard cmd window through the admin profile.

Anyone able to tell me how to get it to open the elevated cmd?

RunAs("administrator",@ComputerName,$sAdminPassword,0,@ComSpec)
    Sleep (500)

    Send("net user /ADD " & $sUsername & " " & $sPassword)
    Send("{ENTER}")

    Send("Y")
    Send("{ENTER}")

    Send("net user " & $sUsername & " /passwordchg:no")
    Send("{ENTER}")

    Send("net localGroup administrators " & $sUsername & " /ADD")
    Send("{ENTER}")

    WinClose("C:\Windows\system32\cmd.exe")

 

Share this post


Link to post
Share on other sites



@Tumulus if you are doing this on a "number of computers", why are you not using Active Directory to automate it?

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

You can use the Local Account UDF to work with local accounts.  Here is an example.  

 

Adam

Share this post


Link to post
Share on other sites

That is a really great question @JLogan3o13. I hadn't even thought to do that. A team lead asked if we could make a script to do it, but it would be much easier in active directory. I'll ask him about that today. 

Also, @AdamUL... That is great! Much easier process for local account management. I can avoid the cmd altogether with those commands right?

However, for the sake of learning, how do I get the cmd to open in an elevated box within an auto it script? We do a lot of scripting and many of our scripts need to operate with administrator permissions. Forgetting the rest of my code (if that is alright), why does this command open a standard cmd window instead of the administrator cmd prompt? 

RunAs("administrator",@ComputerName,$sAdminPassword,0,@ComSpec)

 

Share this post


Link to post
Share on other sites

That is correct, you do not need the command window at all.  Also, with the Local Account UDF, you can access remote computers, and setup local accounts.  Just have a look at the function headers.  Also, look in the thread, as other users have submitted other useful functions not included in the UDF.  

RunAs does not give the runas user the Admin Token.  You have to use a workaround to run as a full admin with the Admin Token as a different user.  Here is an example script that shows one way to do it.  The script is wrote to work uncompiled or compiled.  

 

 

Adam

Share this post


Link to post
Share on other sites

I did the script with the UDF and it was awesome! nifty little scripting there (shout out to @engine for the great work).

The example to run an Admin prompt was really helpful too. I got it working on some other scripts. Thanks again!

Share this post


Link to post
Share on other sites

Your welcome.  I'm glad I could help.  

 

Adam

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • BBs19
      By BBs19
      Features:
      Create modern looking borderless and resizable GUIs with control buttons (Close,Maximize/Restore,Minimize, Fullscreen, Menu) True borderless, resizeable GUI with full support for aerosnap etc. Many color schemes/themes included. See MetroThemes.au3 for more details. 3 type of Windows 8/10 style buttons. Modern checkboxes, radios, toggles and progressbar. All buttons, checkboxes etc. have hover effects! Windows 10 style modern MsgBox. Windows 10/Android style menu that slides in from left. Windows 10 style right click menu  
      Credits: @UEZ, for the function to create buttons with text using GDIPlus. @binhnx for his SSCtrlHover UDF
       
      Changelog:
      Download UDF with example:
       
       
    • keelaji
      By keelaji
      Hello,
      first of all i searched in the forum but i didn't found any solution to my problem.
      I am using windows 10.
      I made a script that is supposed to change the title to the active window, resize it to the smallest size possible (should be around 800 x 600) and move to (0, 0).
      I can change the title but i fail to do the other 2 tasks.
      i use wingettitle [active] copied from the F1 help tool (and this works), i i put the title in a variable and than i use
      winsettitle ($variable, "one") (this works too)
      winmove ("one", 0, 0, 800, 600) (doesn't do anything)
      i also tried
      local $x = winget ( "one")
      msgbox (0, "", $x) (i get a completely white box so i suppose winget can't have any data about my window ??)
      any suggestion ?
    • sree161
      By sree161
      Hi,
      i have a code which generates two numeric outputs.i have to save them in excel.
      Is there any way to export them to specific cells in excel???
    • TotalAuto
      By TotalAuto
      Howdy All
      I've gone around and around in circles wasting hours trying to fix this problem to no avail so I thought I would try posting for help given there are some real smart people in this forum.
      Background
      I have 5 separate .au3 scripts that all do different basic tasks. For example:
      1.au3 = opens Chrome browser to obtain information from a specific website then closes
      2.au3 = opens an Excel workbook to run a macro then saves and closes
      3.au3 = Open two seperate workbooks and transfers data from one to the other then saves and closes
      4.au3 = .......etc
      5.au3 = .......etc
      These 5 separate scripts, doing completely different tasks, need to each run once per day at 5 different designated times within a day and then close. For example:
      1.au3 = 10am
      2.au3 = 12pm
      3.au3 = 2pm
      4.au3 = 4pm
      5.au3 = 5pm
      Problem
      I can not get these different scripts to reliably run at their designated times. When I manually run these scripts individually by using 'Go F5' they work perfectly. If I convert these scripts to an .exe and manually run each one they work about 98% of time.
      How can I schedule these different .au3 files or associated .exe files to reliably run each day at different times?
      Attempts To Fix The Issue
      1. I have tried converting each .au3 script into their own .exe file and use Window's Task Scheduler to run these .exe files at their designated time but the success rate  of running each .exe in full, to completion, without error is very low at about 30%. Things that commonly go wrong include a browser not opening but movement still continues on the desk top. Other times things just do not happen at all.
      2. I have Windows 7 64 bit and have tried creating and running both (x86) and (x64) .exe files but again reliability problems exist;
      3. I have also tried moving each .exe file to the My Documents folder as I read someone that keeping your files on your desktop ties your files to individual Windows user profiles which can cause AutoIt operating errors. Not sure if this is correct but thought I would try it anyway. It did not fix my issue.
      4. I installed a program called AlwaysUp which supposedly always reliably runs in the background and will reliably execute your AutoIt script or .exe files. Still have significant reliability issues.
      Therefore, can someone please provide advice on how I can get my 5 different .au3 files to run at 5 designated times through the day, every day, without fail? Overtime I will be creating more and more different .au3 files to run certain tasks throughout the day and do not want to create one large AutoIt file that continually runs and attempts to do all the different tasks itself.
      Any help is very appreciated.
      Thanks
      Dan
       
       
    • hugomito
      By hugomito
      Hi all,
      I built an AutoIt application that interacts with several other applications/software. Now, it's turn to interfact with Visual Studio Team Services (VSTS).
      Does anyone has a blog, demo, script to share?
      Thanks!