Jump to content

How can I take the output of a WMIC "get" query and feed that into the value of a variable?


Recommended Posts

I feel like I'm missing something stupid easy here. But basically, I want to be able to run the following command and have the "True" or "False" value feed into a variable (for this example we'll call it $passwordexpires):

command: wmic useraccount where name="usernamehere" get passwordexpires

If I run this from a command window I get a "True" or "False" value. But my dumb self apparently can't figure out how to have that value dump into the variable $passwordexpires.

I thought it would be something like this, but this clearly doesn't work:

Local $PasswordExpires = ShellExecute("wmic.exe", "useraccount where name='usernamehere' get passwordexpires")

I know this doesn't work because when I then generate a MsgBox to display the value of $PasswordExpires it keeps displaying what looks to be a random number (which in and of itself is also confusing me).

Link to comment
Share on other sites

One of possible ways

#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $sUsername = "administrator" ; target username
Local $sOutput = ""

; build your wmic query
Local $sWMICquery = 'wmic useraccount where name="' & $sUsername & '" get passwordexpires /format:VALUE'

; run your query hidden and with redirected I/O streams
Local $hPid = Run($sWMICquery, '', @SW_HIDE, $STDERR_MERGED)
Do
    Sleep(100)
    $sOutput &= StdoutRead($hPid) ; get wmic output from redirected stream
Until @error ; error occurs when command has finished

$sOutput = StringStripWS($sOutput, $STR_STRIPALL) ; remove all @cr and spaces from output

MsgBox(0, 'Debug', $sOutput) ; show result

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Another using win management services...

Local $strComputer = "."
Local $wmi = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
Local $col = $wmi.ExecQuery("Select * from Win32_useraccount where name = 'tom'")

For $item In $col
    ConsoleWrite($item.name & @CRLF)
    ConsoleWrite($item.passwordexpires & @CRLF)
Next

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

44 minutes ago, Chimp said:

One of possible ways

#include <AutoItConstants.au3>
#include <StringConstants.au3>

Local $sUsername = "administrator" ; target username
Local $sOutput = ""

; build your wmic query
Local $sWMICquery = 'wmic useraccount where name="' & $sUsername & '" get passwordexpires /format:VALUE'

; run your query hidden and with redirected I/O streams
Local $hPid = Run($sWMICquery, '', @SW_HIDE, $STDERR_MERGED)
Do
    Sleep(100)
    $sOutput &= StdoutRead($hPid) ; get wmic output from redirected stream
Until @error ; error occurs when command has finished

$sOutput = StringStripWS($sOutput, $STR_STRIPALL) ; remove all @cr and spaces from output

MsgBox(0, 'Debug', $sOutput) ; show result

 

That works, but now why can I not  seem to build an If Then statement around the results of $sOutput?

 

For example, this code always displays the "It's True" box, no matter if the value of $sOutput is True or False.

 

If $sOutput = True Then
    MsgBox(0, "It's True", $sOutput)
EndIf
If $sOutput = False Then
    Msgbox(0, "It's False", $sOutPut)
EndIf

 

Link to comment
Share on other sites

This might make it clearer...

Local $strComputer = "."
Local $wmi = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
Local $col = $wmi.ExecQuery("Select * from Win32_useraccount where name = '" & @UserName & "'")

if $col.count > 1 then exit msgbox(17,'Oooops!','More than one user named ' & @username)

For $item In $col
    $passwordexpires = $item.passwordexpires
Next

ConsoleWrite($passwordexpires & @CRLF)

 

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

8 minutes ago, kylomas said:

This might make it clearer...

Local $strComputer = "."
Local $wmi = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
Local $col = $wmi.ExecQuery("Select * from Win32_useraccount where name = '" & @UserName & "'")

if $col.count > 1 then exit msgbox(17,'Oooops!','More than one user named ' & @username)

For $item In $col
    $passwordexpires = $item.passwordexpires
Next

ConsoleWrite($passwordexpires & @CRLF)

 

Okay, that seems to make more sense.  Thanks a lot!

Link to comment
Share on other sites

You could search the Example scripts forum for "Scriptomatic".
That's a script that generates AutoIt code for all kinds of WMI queries :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...