Jump to content

Run WMIC command in elevated previlage


Recommended Posts

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
Link to comment
Share on other sites

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.

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

Can you query sysdriver without tripping UAC?

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

 

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

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

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.

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

Link to comment
Share on other sites

hmmmm...

what about 

driverquery /v

or

sc query type=all

im installing that class now as my curiosity is piqued

 

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

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

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

 

@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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
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
 Share

×
×
  • Create New...