Jump to content

Recommended Posts

Posted

My CreateUser and RemoveUser functions work, my problem is when I run the ListUsers function, sometimes it returns the user names on the remote pc and sometimes is does not? Think it has something to do with the delay when psexec.exe is doing its little thing. Any ideas? Thanks

#include <Array.au3>
#include <Constants.au3>
#include <GUIConstants.au3>
#include <Process.au3>

$toolsDir = "\tools\"
$exe = @ScriptDir & $toolsDir & "psexec.exe "

$GUI = GUICreate("", 600, 400)

GUISetState(@SW_SHOW)

ListUsers("Service", "Admin", "pass")

Do
    $msg = GUIGetMsg()
    
Until $msg = $GUI_EVENT_CLOSE

Func CreateUser($computer, $username, $password, $adminName, $adminPass)
    $output = ""
    
    If $adminPass = "" Then $adminPass = Chr(34) & Chr(34)
    
    $psCmd = $exe & "-u " & $adminName & " -p " & $adminPass & " \\" & $computer
    $netUserCmd = "NET USER " & $username & " " & $password & " " & "/ADD"
            
    $PID = Run($psCmd & " " & $netUserCmd, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    $output = GetConsoleOutput($PID)
    
    MsgBox(0, "", $output)
EndFunc

Func RemoveUser($computer, $username, $adminName, $adminPass)
    $output = ""
    
    If $adminPass = "" Then $adminPass = Chr(34) & Chr(34)
    
    $psCmd = $exe & "-u " & $adminName & " -p " & $adminPass & " \\" & $computer
    $netUserCmd = "NET USER " & $username & " " & "/DELETE"
    
    $PID = Run($psCmd & " " & $netUserCmd, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    $output = GetConsoleOutput($PID)
    
    MsgBox(0, "", $output)
EndFunc

Func ListUsers($computer, $adminName, $adminPass)
    Dim $users
    
    If $adminPass = "" Then $adminPass = Chr(34) & Chr(34)
    
    $psCmd = $exe & "-u " & $adminName & " -p " & $adminPass & " \\" & $computer
    $netUserCmd = "NET USER"
    
    $PID = Run($psCmd & " " & $netUserCmd, "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)
    $output = GetConsoleOutput($PID)
    
    $users = ParseUserOutput($output)
    
    Return $users
EndFunc


Func ParseUserOutput($text)
    MsgBox(0, "", $text)
    Dim $array
    
    $startToken = ""
    $endToken = ""
    
    Return 0
EndFunc

Func EncloseStrInChar($str, $char)
    Return $char & $str & $char
EndFunc

Func GetConsoleOutput($PID)
    $output = ""
        
    While 1
        $line = StdoutRead($PID)
        If @error Then ExitLoop

        $output &= $line
    Wend
    
    Return $output
EndFunc
Posted

No sorry, just this part

Func GetConsoleOutput($PID)
    $output = ""
       
    While 1
        $line = StdoutRead($PID)
        If @error Then ExitLoop

        $output &= $line
    Wend
   
    Return $output
EndFunc
oÝ÷ ØÊ,¶íëbè­­ën®{*ºjºZÚ-êìÚÞ¶êç³Z¶+l¥v)æÉƬzØ^ºÇ«©²íêÞ^¥Ì!zr&q©ex-çîËb¢râ²Õ,0'!q©e²Ø^~éܶ*'/x'£­³
+)àmçºÇ©±ì^qì^{­³­z®º+²+lë-¶­¬zØ^¢ëiºÛh¶¢{(çë¢jl{µ©²Ú(à¶¶)í¢Ø^²ÚÞjj+²¶§X¤zØZµÆ®²)à¶zШÊ%xë­¦ë_ºw-í¢·­º¹Þj¹rÂ+aªê-ªê-{-y§h~Ø^ºÇ«©±*jÈjYr";¬·§¶Ú0j+nØ¥¦Ç±yÊÞ¶êç²Ø^u«Z#§¶Êq©éºØ­{a{Mú¢ëiºÛÚ®&â-®'jëh×6
Func GetConsoleOutput($PID)
    $output = ""
       
    Do
        $line = StdoutRead($PID)
        If @error Then ExitLoop

        $output &= $line
    Until $output <> ""
   
    Return $output
EndFunc
]

Did not work

Posted

I cannot see your script checking the exitcode to know if things have worked ok. Since your Run() function is setup to read the StdErr, then I would suggest reading it in case some error is released from the stream.

Try this

Func GetConsoleOutput($PID)
    Local $line
    Do
        $line &= StdoutRead($PID)
    Until @error
    Do
        $line &= StdErrRead($PID)
    Until @error
    Return $line
EndFunc

:lmao:

Posted (edited)

Ok sometimes it still returns no data. I know psexec is executing correctly beacuse:

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com

Connecting to Service...
                                                                              

Starting PsExec service on Service...
                                                                              

Connecting with PsExec service on Service...
                                                                              

Starting NET on Service...
                                                                              


NET exited on Service with error code 0.

Is displayed in the scite console window down bottom weather StdoutRead returns the data or not.

Now if no data is read by StdoutRead the program it hangs and I get a message box with a blank string.

If StdoutRead reads the data. The message box displays:

User accounts for \\Service

==================

Administrator

Guest etc.

[\CODE]

So it seems it works only 50% of the time, I tried adding a sleep before calling StdoutRead with no luck

Edited by creeping
Posted

Looks like it will have to be that way, thanks for the suggestion. I'm sure its just a psexec thing because Ive used stdout quite a few times with no problems.

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
×
×
  • Create New...