Jump to content
PramodR

Run WMIC command in elevated previlage

Recommended Posts

PramodR
Posted (edited)

Hello,

I have to execute below block of code in elevated previllage, as that particular wmic class will work only with admin previlage.

i am getting out put when i launch autoit as administrator,  but i will not be able to launch autoit as admin in this particular case but my user have admin rights.

is there any better way i can run those command as administrator.?

tried below steps:

  • i have tried #RequireAdmin but that creates a user prompt 
  • Tried using Runas command but gives error as wrong username or password , stuck with that step.

 

Quote

 

#include <MsgBoxConstants.au3>
#include <Constants.au3>
#include <MsgBoxConstants.au3>

$WmiCommand = ( _GetDOSOutput("wmic /namespace:\\root\dcim\sysman path dcim_biosenumeration where(attributename like '%%Microphone%%') get currentvalue") & @CRLF)
ConsoleWrite($WmiCommand)

Func _GetDOSOutput($sCommand)
    Local $iPID, $sOutput = ""
    $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $sOutput &= StdoutRead($iPID, False, False)
        If @error Then
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return $sOutput
 EndFunc   ;==>_GetDOSOutput

 

 

 

 

Edited by PramodR
removing word geek

Share this post


Link to post
Share on other sites
Subz

Have you tried adding #RequireAdmin to the top of your script?

Share this post


Link to post
Share on other sites
orbs

you are using WMI to query the system, not to operate on it, therefore you actually do not need admin rights. although WMI as a method may require admin rights in effect (regardless of the specific function), only querying in general does not. so consider other methods to retrieve the piece of information you need.

Share this post


Link to post
Share on other sites
PramodR

@Subs I have tried adding #RequireAdmin to the top of script but its keep giving me UAC pop up which i dont want to select manually.

@orbs Usually WMI get operation does not required any admin previllage but this particular class require admin previllage hence i need to launch auto it in administrator mode, this is a unique name space this functionality not available in other classes.

Share this post


Link to post
Share on other sites
Subz

Sorry just reread your post and see you had already tried #RequireAdmin, if you are going to launch Cmd as elevated you will always see the UAC prompt, for example, I'm an administrator on my machine, however if I run cmd.exe it will always start in user mode, I still require Run As Administrator which then show UAC.  The only way around this would be switch UAC off.

  • Like 1

Share this post


Link to post
Share on other sites
iamtheky

are you sure the dell omci class is the only place you can get this information from?


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

Share this post


Link to post
Share on other sites
TheXman
Posted (edited)

@PramodR

Depending on how much administrative control you have over your users' environment, you can suppress the UAC prompt for users that are local admins of their workstation by either using group policy or by modifying the registry.  The following link should give you all of the information that you need to make the modifications.  In your case, if the users are truly local admins, you want to direct your attention to the "ConsentPromptBehaviorAdmin" registry key or "User Account Control: Behavior of the elevation prompt for administrators in Admin Approval Mode" group policy.  If this is configured correctly, then local admins will not be prompted when you add the #RequireAdmin directive to your scripts.

User Account Control Group Policy and registry key settings

 

A very helpful UDF lib for getting and setting UAC-related registry settings is UAC.au3 submitted by @AdamUL

 

Edited by TheXman
  • Thanks 1

Share this post


Link to post
Share on other sites
iamtheky

Can you query sysdriver without tripping UAC?

wmic sysdriver get /format:list | find "mic"

 


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

Share this post


Link to post
Share on other sites
PramodR
Posted (edited)

@iamtheky  i get below out put for Mic.

 Caption=Dynamic Volume Manager
Description=Dynamic Volume Manager
DisplayName=Dynamic Volume Manager

Actually i there is other than microphone many other configuration which i will be able to access only from omci. 

 

Edited by PramodR

Share this post


Link to post
Share on other sites
iamtheky

so if you take off the find command, and scroll through the list, do you see the object you are after?

And are you running this as you would want to run it in the future...just so we are sure we are chasing plausible solutions.


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

Share this post


Link to post
Share on other sites
PramodR

@iamtheky no i could not find the object which i was looking , yes i am creating script so the same can be re run over different systems for test,

Share this post


Link to post
Share on other sites
PramodR

@TheXman I will definitely look for the possibilities to disable UAC in my environment. but i dont think that is the long time solution.

  

Share this post


Link to post
Share on other sites
iamtheky

hmmmm...

what about 

driverquery /v

or

sc query type=all

im installing that class now as my curiosity is piqued

 


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

Share this post


Link to post
Share on other sites
TheXman
2 minutes ago, PramodR said:

I will definitely look for the possibilities to disable UAC in my environment. but i dont think that is the long time solution

To be clear, I did not suggest disabling UAC.  I suggested, for local admins only, configuring UAC to automatically elevate without prompting.

Share this post


Link to post
Share on other sites
PramodR

@iamtheky install Dell Command Monitor only for your dell corporate client systems, This will let you set or get BIOS configuration.

 

@TheXman Sure....  

Share this post


Link to post
Share on other sites
iamtheky

i didnt even look at the enumeration command, i just say wmi and microphone and played videogames, eh ask questions on a holiday weekend you get spotty service :)


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

Share this post


Link to post
Share on other sites
TheXman

@PramodR

Is your primary issue being able to run privileged commands, for local admins,  without being prompted by UAC or is your primary issue being able to query for specific information on users' workstations?  Because you've been given a workable solution to the former, it appears that your issue is the latter.  If so, can you explicitly state what piece or pieces of information you are trying to retrieve?  Is it just a list of the microphones, as your original post would suggest, or are you after more information.  If so, what specifically?  Is it imperative that you use the DCIM WMI provider or is it okay to get the information some other way that may or may not require elevated privileges?  Your answers to these questions would go along way towards others trying to help you accomplish your goal.

Share this post


Link to post
Share on other sites
PramodR
Posted (edited)

 

@TheXman

Yes my primary issue is how to run privileged commands for local admins.

i am pretty sure these information is not available in other resource currently. im not only trying to retrieve microphone , microphone is just example there are many configurations. 

Its okay to get information from other source but currently not available as part of my research. 

 

Edited by PramodR

Share this post


Link to post
Share on other sites
TheXman
Posted (edited)

You can try the snippet below.  It is a different method of elevation that works if your users are local admins.  Because you haven't implement the suppressing of UAC prompts for local admins, it may trigger the prompt just as if you had added #RequireAdmin.  Try it out and see if it works for you. 

Basically, it checks to see if you are running the script with elevated privileges.  If not, it will re-launch itself using the ShellExecute function with the "runas" verb in order to request elevation. 

By the way, I didn't check it for errors so you may have to tweak it a tad.  :)

 

#include <Constants.au3>
#include <WinAPI.au3>

elevate_to_run_with_admin_token()
$WmiCommand = ( _GetDOSOutput("wmic /namespace:\\root\dcim\sysman path dcim_biosenumeration where(attributename like '%%Microphone%%') get currentvalue") & @CRLF)


;==========================================================================
; This assumes that the user is a local admin.
; Do NOT use #RequireAdmin if using this method of elevation
;==========================================================================
Func elevate_to_run_with_admin_token()

    Local $sErrorMsg = ""
    Local $iPid      = 0


    ;Run with "runas" verb in order request full Admin token (in Windows Vista and Higher - UAC-enabled OSes).
    If (Not IsAdmin()) And (Not StringRegExp(@OSVersion, "_(?:XP|2000|2003))")) Then
        $iPid = ShellExecute(@AutoItExe, $CmdLineRaw, @ScriptDir, "runas")
        If $iPid Then
            Exit
        Else
            $sErrorMsg = "ERROR: Unable to elevate to Admin due to UAC. " & _WinAPI_GetLastErrorMessage()
            MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR", $sErrorMsg)
            Exit -1
        EndIf
    EndIf

    MsgBox( _
        $MB_ICONINFORMATION + $MB_TOPMOST, _
        "INFO", _
        StringFormat("Elevated status = %s", (IsAdmin())?("TRUE"):("FALSE")) _
    )

    Return

EndFunc

Func _GetDOSOutput($sCommand)
    Local $iPID, $sOutput = ""
    $iPID = Run('"' & @ComSpec & '" /c ' & $sCommand, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    While 1
        $sOutput &= StdoutRead($iPID, False, False)
        If @error Then
            ExitLoop
        EndIf
        Sleep(10)
    WEnd
    Return $sOutput
EndFunc   ;==>_GetDOSOutput

 

Edited by TheXman
  • Like 1

Share this post


Link to post
Share on other sites
TheXman
Posted (edited)

I just noticed you are launching your command using RUN.  I would either change it to use ShellExecuteWait or use COM. 

** Updated **

Ignore the statement above out changing to ShellExecuteWait or COM.  Just use whatever you are most comfortable with. :)

Edited by TheXman

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

    • ur
      By ur
      When I compiled my script or any script to exe using 32 bit compiler and ran on 32 bit machine, I am getting below error.
       

       
      But it is 32 bit only.
       
      And, for testing, I disabled smart screen also, but same issue.
      Please suggest.
       
    • gahhon
      By gahhon
      Hi,
      I have a program that is read data from the .txt file, since the .txt file is only readable, viewable and editable for the admin user.
      But how could I lock it with password, or some other technique that can helps to achieve this?
      Thanks for the advanced information.
    • gahhon
      By gahhon
      Hi,
      How can I the trigger the another button functions without waiting the previous function to finish execute?
      Any advise?
      I couldn't find any relevant topics via google.
      Thanks a lot.
    • SharpDressedMan
      By SharpDressedMan
      Hi all,
      I need to use ControlMove() on some controls of a hidden GUI window.
      This works properly on a GUI window created with default style, but does not work on a GUI window created with style $WS_OVERLAPPEDWINDOW
      func GUItest($bOverlapped) $m = GUICreate("test", 200, 100, -1, -1, $bOverlapped ? $WS_OVERLAPPEDWINDOW : -1) $g = GUICtrlCreateButton("test", 0, 0) ControlMove($m, "", $g, 30, 30) GUISetState() endfunc GuiTest(false) ; button 'test' properly moved to (30,30) GuiTest(true) ; button 'test' not moved and still sitting at (0,0) Any reason for this unexpected behavior ?
      Any workaround to fix this ?
      Thanks for any help
    • 2Toes
      By 2Toes
      Hi all..
      I'm having slight issue that has be a bit puzzled.
      I have an Input control.. After a certain function runs, it Disables that Input control, and sets it to ReadOnly.
      When running another certain function, it re-Enables that Input control, and sets the style back to Default.
      Which does work, however, when typing into the Input control after re-Enabling it, the text appears as a light gray color, rather than its original black color.
      I'm sure there's a simple solution here.. But what that is, appears to be beyond me.
      Here is a simplified example code:
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 245, 215, 192, 124) Global $disableBTN = GUICtrlCreateButton("Disable", 32, 64, 75, 25) Global $enableBTN = GUICtrlCreateButton("Enable", 128, 64, 75, 25) Global $Input1 = GUICtrlCreateInput("", 32, 120, 169, 21) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $disableBTN disable() Case $enableBTN enable() EndSwitch WEnd Func disable() GUICtrlSetState($Input1, $GUI_DISABLE) GUICtrlSetStyle($Input1, $ES_READONLY) EndFunc Func enable() GUICtrlSetState($Input1, $GUI_ENABLE) GUICtrlSetStyle($Input1, $GUI_SS_DEFAULT_INPUT) EndFunc  
      Any help would be greatly appreciated
      Thank you in advance!
×