HighSpeed556

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

9 posts in this topic

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

Share this post


Link to post
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

 


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Okay, that works.  Now...why can't I build an "if then" statement around this.  For testing purposes, why does this not work?

 

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

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Because the value of $soutput is not true or false.  Run the example again and look at the messagebox.


Forum Rules         Procedure for posting code

Share this post


Link to post
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

Share this post


Link to post
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!

Share this post


Link to post
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 (2016-08-18 - Version 1.4.6.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2016-07-20 - Version 1.2.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

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