Jump to content

Recommended Posts


 I am currently trying to create a tool that will automate some task we need to preform at work. The task that I am trying to automate are mostly powershell based. I did a search on the forum and I came across sapien.activexposh COM object. A perfect match to create a GUI in autoit and a clean way to push powershell command for execution.


But i hit a roadblock and I hope that someone can help me solve it.

-       before you can use this sapien activexposh object you must register it using RegAsm.exe.

-       The command to register the object is: “C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase {directory}\ActiveXPoshV3.dll”

-       The command prompt needs to be elavated prompt


The problem:

When I run [start -> cmd -> right click -> run as administrator] its seems to open a elevated prompt. But this prompt doesn’t have enough privilege to register a object in regasm.exe (RegAsm : error RA0000)


The weird stuff:

When I [create cmd shortcut to desktop -> right click -> run as administrator] it get a elevated prompt with enough permission to register a object with regasm.exe. (using the same account and laptop!)


There seems to be a clear difference when opening the cmd prompt in the way I explained above, see screenshot



Is there way to programmatic start the Administrator:cmd window? BTW runas doesn’t seem to work for me…




Edited by aghering

Share this post

Link to post
Share on other sites

I would have made that @ComSpec in stead of cmd. :)


Share this post

Link to post
Share on other sites

If they are 'mostly powershell' then i would skip cmd/comspec altogether.




,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post

Link to post
Share on other sites

Thanks you guys for your input, all the commands seems to open CMD as "Administrator" 
I had some problems with pushing the commands to the CMD console but i finaly managed to programmaticly register the component. (Jeej)

@Boththose: i prefer to work with the activexposh.dll. after registering the object you will be able to execute PS commands, read/clear output, multiline powershell commands without using semicolons, stdinwrite, stdoutread, etc...

Edited by aghering

Share this post

Link to post
Share on other sites

I wrote a function that comprehends all the working solutions mentioned earlier in this thread:

#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>

; Call Function: f_RunAsAdmin_CMD

; Define Function: f_RunAsAdmin_CMD
Func f_RunAsAdmin_CMD($iMethod = 0, $iDebugFlag = 0, $sShowFlag = @SW_SHOW)
; This function returns an administrator CMD window based on the method chosen:
;   $iMethod = 0 : use ShellExecute with RunAs
;   $iMethod = 1 : use #RequireAdmin and Run("cmd")
;   $iMethod = 0 is Default Option
; Include: Add the following two lines at the top of your script
;   #include <MsgBoxConstants.au3>
;   #include <AutoItConstants.au3>

    Local $sMessage = "Window State: "

    If Not($iMethod = 1) Then
        $iMethod = 0 ; Default method

    Switch $sShowFlag
        Case @SW_SHOW
            $sMessage &= "Visible"
        Case @SW_HIDE
            $sMessage &= "Hidden"
        Case @SW_MINIMIZE
            $sMessage &= "Minimized"
        Case @SW_MAXIMIZE
            $sMessage &= "Maximized"
        Case Else ; Default
            $sShowFlag = @SW_SHOW
            $sMessage &= "Visible"

    If ($iDebugFlag = 1) Then
        ConsoleWrite("Method Used: " & String($iMethod)  & @LF & $sMessage & @LF)
        MsgBox(0,"Run CMD as Admin","Method Used: " & String($iMethod) & @LF & $sMessage ,1)

    If ($iMethod = 0) Then
        ShellExecute(@ComSpec, "", "", "RunAs",$sShowFlag)



I hope this will be helpful for future discussions. You could also consider using RunAsWait (replace RunAs) and ShellExecuteWait (replace ShellExecute) to wait until the CMD window is launched and finishes spawning.



Edited by techsray

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

  • Similar Content

    • By Burgs
        I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines.  I was thinking of something similar to this:
      Local $iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if $iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if $hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active... $sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW) $sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer. 
      Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
    • By MadhaN
      Hi all,
      I have a csv file as below, I wand to find srno from csv and send corresponding ip and pass to commend cmd prompt. 
      Please guide me to create script .
    • By nacerbaaziz
      Hi dear
      With this script you can print Unicode text in the CMD screen
      the script is  easy to use
      just you  write the text that contains Unicode in the first input
      and the script automatically reflect the code in the second input
       you can copy the text to the clipboard
      or you can try printing the text in the CMD window
      I apologize to everyone for colors and shape if not appropriate
      I'm a blind man and I do not see
      Thank you for your understanding
      Greetings to all of youCmdUtM.au3
    • By Simpel
      Thanks to this post (https://www.autoitscript.com/forum/topic/189553-writing-to-cmd/?do=findComment&comment=1361142) I can now write a helpfile to the cmd when for instance passing parameter -h or -help at cmd. But then cmd is blocked by the script (I had to free it with CTRL+BREAK):
      #include <WinAPI.au3> _WinAPI_AttachConsole() $hConsole = _WinAPI_GetStdHandle(1) _WinAPI_WriteConsole($hConsole, "Print helpfile................" & @CRLF) Inside MSDN Library then I found this sentence: A process can use the FreeConsole function to detach itself from its console.
      But I don't find something like _WinAPI_FreeConsole(). How can I do it in another way?
      Regards, Conrad
    • By TheAutomator
      A fullscreen console with custom commands!

      Hi everyone!
      This funny project started as a question in the help section:
      I'd like to share this script with everyone that is interested. 
      Why would I want it?
      You like the old style fullscreen console (like in the old day's), You can add custom commands, You can customize the font a lot more compared to cmd.exe, You can share ideas or add tweaks to the script.  
      Still to do:
      Write a simple custom programming language to implement this tool. Writing a little help file / pdf to describe my little programming language. Add little sound effects like a beep if there is a syntax error (optional). Clean up and modify Console.Au3 content. Add an option to have to type a login password (optional). Make an optional installer that also gives scripts for this tool a custom icon and open with command. ...Call Neo?  
      Thanks to:
      xxaviarxx: debugging, some ideas. jguinch: debugging, adding a bunch of tweaks and ideas. kylomas: debugging, new ideas.  
      Edits and updates:
      Added usage of tab key in edit control Edit has focus now on startup I'm currently rewriting a simple custom programming language to implement this tool.  
      UDF download: Console.au3
  • Create New...