Jump to content

Can't get output of run command to a variable


JoelG
 Share

Recommended Posts

I'm trying to write a script that will check the password expiry of a user account.  I just can't find a way to get the output of the command to a variable so I can display it to the user in a MsgBox:

#include <Constants.au3>

; Request username
Local $UserID = InputBox ( "Password Expiry Checker", "Please enter the username you want to check.")

; Set Net User command
$NETCMD = 'NET USER ' & $UserID & ' /DOMAIN |FIND /I "Password expires"'

; Run Net User command
Local $CMD = Run(@ComSpec & " /c " & $NETCMD)
ProcessWaitClose($CMD)

; Output results
MsgBox (64, "Password Expiry Checker", $UserID & " " & $PassExpiry)

 

The above will run the command and display the data I'm looking for in a command prompt window (I'll hide that when I get the rest working).  I've tried using STDOUT but I just can't get it to output anything to a variable (I know that piece is missing here).  Can anyone suggest a way to output the $NETCMD to $PassExpiry variable that I can use in the MsgBox?

Thanks,

Joel

Link to comment
Share on other sites

Look up StdoutRead in the help file and look at its example.

Edited by TheXman
Link to comment
Share on other sites

I have, but it does not seem to work for this.

Two things I've noticed with StdOutRead:

As soon as I add , $STDOUT_CHILD to my command, the line runs far to quickly to have actually completed - ie. I get the msgbox immediately, not after 5 seconds like it takes for the cmd to return a result.

The StdOutRead ends up empty.

Joel

Link to comment
Share on other sites

Post the script where you tried to use stdoutread.  Use the "< >" tags in the editor ribbon when posting code.

Link to comment
Share on other sites

#include <Constants.au3>

; Request username
Local $UserID = InputBox ( "Password Expiry Checker", "Please enter the username you want to check.")

; Set Net User command
$NETCMD = 'NET USER ' & $UserID & ' /DOMAIN |FIND /I "Password expires"'

; Run Net User command
Local $CMD = Run(@ComSpec & " /c " & $NETCMD,@SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($CMD)

; Set PassExpiry from output of CMD
Local $PassExpiry = StdoutRead($CMD)

; Output results
MsgBox (64, "Password Expiry Checker", $UserID & " " & $PassExpiry)

 

Link to comment
Share on other sites

5 minutes ago, JoelG said:

Run(@ComSpec & " /c " & $NETCMD,@SW_HIDE, $STDOUT_CHILD)

 

You have @sw_hide where the working directory should be.  Try:

Run(@ComSpec & " /c " & $NETCMD, "", @SW_HIDE, $STDOUT_CHILD)
If @error Then Exit MsgBox($MB_ICONERROR, "ERROR", "An error occurred executing command - @error = " & @error)

You should also add error checking.  If you had checked @error, you would have seen that you had a problem with the statement.

Edited by TheXman
Link to comment
Share on other sites

Yup, that was it.  Sorry I didn't realize that was a required variable as I'm not really referencing the path in the command.  Stupid mistake on my part.  The following script is working as expected:

#include <Constants.au3>
Local $FilePath = @ScriptDir

; Request username
Local $UserID = InputBox ( "Password Expiry Checker", "Please enter the username you want to check.")

; Set Net User command
$NETCMD = 'NET USER ' & $UserID & ' /DOMAIN |FIND /I "Password expires"'

; Run Net User command
Local $CMD = Run(@ComSpec & " /c " & $NETCMD,$FilePath, @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($CMD)

; Set PassExpiry from output of CMD
Local $PassExpiry = StdoutRead($CMD)

; Output results
MsgBox (64, "Password Expiry Checker", $UserID & " " & $PassExpiry)

 

Link to comment
Share on other sites

17 minutes ago, JoelG said:

Thank you for your help BTW, meant to add that in my last reply.

You're welcome!  :thumbsup:

Link to comment
Share on other sites

Updated script with MsgBox in the event the account does not exist, in case this is useful form someone else:

#include <Constants.au3>
Local $FilePath = @ScriptDir

; Request username
Local $UserID = InputBox ( "Password Expiry Checker", "Please enter the username you want to check.")

; Set Net User command
$NETCMD = 'NET USER ' & $UserID & ' /DOMAIN |FIND /I "Password expires"'

; Run Net User command
Local $CMD = Run(@ComSpec & " /c " & $NETCMD, $FilePath, @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($CMD)

; Set PassExpiry from output of CMD
Local $PassExpiry = StdoutRead($CMD)

; Output results
If $PassExpiry = "" then
   MsgBox (64, "Password Expiry Checker", "The " & $UserID & " account does not exist")
   Else
   MsgBox (64, "Password Expiry Checker", "The " & $UserID & " account " & $PassExpiry)
EndIf

 

Link to comment
Share on other sites

  • 4 weeks later...

I had to read the output of a python script and could not manage.

So, workaround was to have python dump it to text, set a flag and then get AutoIt to read the text and delete the flag. The "deleted flag" was the trigger for the next loop.

Primitive, slow, very effective.

Skysnake

Why is the snake in the sky?

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