Sign in to follow this  
Followers 0
VirtualRider

Capture STDIO from PSExec command

1 post in this topic

Greetings,

I have been trying to script a process of resetting the DSRM passwords on a large number of Domain Controllers and am getting stuck on capturing the STDIO output from the psexec commands being called via the run command (using that instead of runwait per one of the other forum posts on capturing STDIO). It has been nearly a year since I have done any dabbling in the STDIO area and have noticed that a few things have changed since then. Perhaps it is something simple that I am missing since I am not a developer or scripter by nature, just out of necessity. Idealy I would like to capture success messages and failure messages.

Here is the script that I am writing:

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

Dim $credentials, $username, $UserPWD, $serverfile, $line, $servername, $DSRMPassword, $splitter, $domain, $file, $folder, $drive, $folderpath, $STDIO, $Time

$STDIO = ""

$drive = ""

$folder = "c:\ntdsutil"

$folderpath = "ntdsutil"

$log = fileopen(@WorkingDir & "\NTDSUtil.log", 10)

$Time = @HOUR & ":" & @MIN & @TAB

FileWriteLine($log, "DSRM Password Reset Log")

FileWriteLine($log, "===========================")

FileWriteLine($log, "")

FileWriteLine($log, "DATE: " & @MON & "/" & @MDAY & "/" & @YEAR & @TAB & @HOUR & ":" & @MIN)

FileWriteLine($log, "")

FileWriteLine($log, "")

$credentials = InputBox ("Administrative Credentials Required!", "Please enter the administrative credentials required to run this program (DOMAIN\USERNAME format).")

if @error = "1" Then

MsgBox(0, "Aborting!", "Exiting due to the Cancel button has been pressed")

eject()

EndIf

$splitter = StringSplit($credentials, "\", 1)

$domain = $splitter[1]

$username = $splitter[2]

$UserPWD = InputBox("Password required", "Please enter the password for this account.", "", "*")

if @error = "1" Then

MsgBox(0, "Aborting!", "Exiting due to the Cancel button has been pressed")

Eject()

EndIf

$DSRMPassword = InputBox("Set DSRM Password", "Please enter the new Directory Services Restore Mode (DSRM) password.", "")

if @error = "1" Then

MsgBox(0, "Aborting!", "Exiting due to the Cancel button has been pressed")

Eject()

EndIf

FileWriteLine($log, $Time & "Checking for necessary files.") ;Checking for necessary files to perform the DSRM password reset

FileWriteLine($log, $Time & "Checking for PSExec.exe in the " & @WorkingDir & " Folder.")

if not FileExists (@WorkingDir & "\psexec.exe") then

MsgBox (48, "Missing File!", "PSExec.exe is missing from the " & @WorkingDir & " Folder!" & @CRLF & "Please copy the file to this location and re-run this program.")

FileWriteLine($log, $Time & "PSExec.exe is missing. Stopping.")

Eject()

EndIf

FileWriteLine($log, $Time & "Checking for servers.txt in the " & @WorkingDir & " Folder.")

if not fileExists (@WorkingDir & "\servers.txt") then

MsgBox(48, "Missing File!", "The Servers.txt file is missing from the " & @WorkingDir & " Folder!" & @CRLF & "Please copy the file to this location and re-run this program.")

FileWriteLine($log, $Time & "The Servers.txt file is missing from the " & @WorkingDir & " Folder!" & @CRLF & "Please copy the file to this location and re-run this program.")

Eject()

EndIf

FileWriteLine($log, $Time & "Checking for SetPwd.exe in the " & @WorkingDir & " Folder.")

if not FileExists (@WorkingDir & "\setpwd.exe") then

MsgBox (48, "Missing File!", "SetPwd.exe is missing from the " & @WorkingDir & " Folder!" & @CRLF & "Please copy the file (Version 5.0.2195.6114) to this location and re-run this program." & @CRLF & @CRLF & "WARNING: If the SetPwd.exe file version is not 5.0.2195.6114, ensure that it can accept a password variable!")

FileWriteLine($log, $Time & "SetPwd.exe is missing from the " & @WorkingDir & " Folder!" & @CRLF & "Please copy the file (Version 5.0.2195.6114) to this location and re-run this program." & @CRLF & @CRLF & "WARNING: If the SetPwd.exe file version is not 5.0.2195.6114, ensure that it can accept a password variable!")

Eject()

Exit

EndIf

FileWriteLine($log, $Time & "File check is complete, starting to do some actual work.") ;Starting the work...

FileWriteLine($log, $Time & "Opening the Servers.txt file and getting the first server name.")

$serverfile = FileOpen("Servers.txt", 0)

If $serverfile = -1 Then

MsgBox(0, "Error", "Unable to open file " & @WorkingDir & "\" & $serverfile & ".")

FileWriteLine($log, $Time & "Unable to open file " & @WorkingDir & "\" & $serverfile & ".")

Eject()

EndIf

while $serverfile <> -1

$servername = FileReadLine($serverfile) ; Read in lines of text until the EOF is reached

If @error = -1 Then ExitLoop

FileWriteLine($log, $Time & "Initiating password change process on server " & $servername & ".")

FileWriteLine($log, $Time & "Creating directory " & $folder & " on " & $servername & ".")

$STDIO = Run("psexec.exe \\" & $servername & " -u " & $credentials & " -p " & $UserPWD & " cmd.exe /c mkdir " & $folder, @WorkingDir, @SW_HIDE, 2)

$errorcode = ProcessWaitClose($STDIO, 0)

While 1

if $STDIO then; if run was successful

$line = StdoutRead($STDIO, 0, 1)

If @error Then ExitLoop

if $line then $read = StdoutRead($STDIO)

FileWriteLine($log, $Time & "PSEXEC MKDIR Command results on server " & $servername & ": " & $line)

EndIf

Wend

FileWriteLine($log, $Time & "Hiding directory " & $folder & " on " & $servername & ".")

$STDIO = Run("psexec.exe \\" & $servername & " -u " & $credentials & " -p " & $UserPWD & " cmd.exe /c attrib " & $folder & ". +h", @WorkingDir, @SW_HIDE, 2)

$errorcode = ProcessWaitClose($STDIO, 0)

While 1

if $STDIO then; if run was successful

$line = StdoutRead($STDIO, 0, 1)

If @error Then ExitLoop

if $line then $read = StdoutRead($STDIO)

FileWriteLine($log, $Time & "PSEXEC ATTRIB Command results on server " & $servername & ": " & $line)

EndIf

Wend

FileWriteLine($log, $Time & "Copying setpwd.exe to " & $servername & ".")

$drive = DriveMapAdd("*", "\\" & $servername & "\C$\" & $folderpath, 0, $credentials, $UserPWD)

FileCopy(@WorkingDir & '\setpwd.exe', $drive)

if FileExists($drive & "\" & $folderpath & "\setpwd.exe") then ;The file exists on the destination server

FileWriteLine($log, $Time & "SetPwd.exe file copy has been verified on " & $servername & ".")

Else

FileWriteLine($log, $Time & "SetPwd.exe file did not copy to " & $servername & ".")

Eject()

EndIf

FileWriteLine($log, $Time & "Setting DSRM Password on " & $servername & ".")

$STDIO = Run("psexec.exe \\" & $servername & " -u " & $credentials & " -p " & $UserPWD & " " & $folder & "\setpwd.exe /s:" & $servername & " /p:" & $DSRMPassword, @WorkingDir, @SW_HIDE, 2)

$errorcode = ProcessWaitClose($STDIO, 0)

While 1

if $STDIO then; if run was successful

$line = StdoutRead($STDIO, 0, 1)

If @error Then ExitLoop

if $line then $read = StdoutRead($STDIO)

FileWriteLine($log, $Time & "PSEXEC SetPwd.exe Command results on server " & $servername & ": " & $line)

EndIf

Wend

WEnd

Complete()

MsgBox(0, "Complete", "Program Completed.")

Func Complete()

FileWriteLine($log, $Time & "Performing cleanup process.")

DeleteRemoteFolder()

RemoveMappedDrive()

Cleanup()

FileWriteLine($log, $Time & "Program Ending at " & @MON & "/" & @MDAY & "/" & @YEAR & @TAB & @HOUR & ":" & @MIN)

FileWriteLine($log, "")

FileWriteLine($log, "")

FileClose($file)

$log = ""

Exit

EndFunc

Func Eject()

FileWriteLine($log, $Time & "Failure detected, shutting down password change program.")

if $drive <> "" Then

DeleteRemoteFolder()

RemoveMappedDrive()

EndIf

Cleanup()

FileWriteLine($log, $Time & "Program Aborting at " & @MON & "/" & @MDAY & "/" & @YEAR & @TAB & @HOUR & ":" & @MIN)

FileWriteLine($log, "")

FileWriteLine($log, "")

FileClose($file)

$log = ""

Exit

EndFunc

Func Cleanup()

FileWriteLine($log, $Time & "Clearing variables.")

$credentials = ""

$domain = ""

$drive = ""

$DSRMPassword = ""

$errorcode = ""

$file = ""

$folder = ""

$folderpath = ""

$line = ""

$read = ""

$serverfile = ""

$servername = ""

$splitter = ""

$STDIO = ""

$username = ""

$UserPWD = ""

FileWriteLine($log, $Time & "All variables have been cleared.")

EndFunc

func RemoveMappedDrive()

FileWriteLine($log, $Time & "Checking for mapped drives.")

if not $drive = "" then

FileWriteLine($log, $Time & "Removing mapped drive.")

DriveMapDel ($drive)

FileWriteLine($log, $Time & "Mapped drive removed.")

Else

FileWriteLine($log, $Time & "Checked for mapped drives, none exist in relation to this program.")

EndIf

EndFunc

Func DeleteRemoteFolder()

FileWriteLine($log, $Time & "Removing directory " & $folder & " on " & $servername & ".")

$STDIO = Run("psexec.exe \\" & $servername & " -u " & $credentials & " -p " & $UserPWD & " cmd.exe /c RMDIR " & $folder & " /Q /S", @WorkingDir, @SW_HIDE, 2)

$errorcode = ProcessWaitClose($STDIO, 0)

While 1

if $STDIO then; if run was successful

$line = StdoutRead($STDIO, 0, 1)

If @error Then ExitLoop

if $line then $read = StdoutRead($STDIO)

FileWriteLine($log, $Time & "PSEXEC RMDIR Command results on server " & $servername & ": " & $line)

EndIf

Wend

EndFunc

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

Thanks in advance for any advice or suggestions that you may have.

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
Sign in to follow this  
Followers 0