Sign in to follow this  
Followers 0
madmorgan

how to shutdown a remote pc

3 posts in this topic

hello all,

ive looked in the help file at shutdown command but how can i send this command to a remote computer.

thanks

Share this post


Link to post
Share on other sites



It's already possible with "shutdown.exe" command, or PsTools :(

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

madmorgan,

Here is a remote shutdown function that I wrote using the Windows built in shutdown function. This includes error logging to a file.

#include <File.au3>
;Example
_RemoteShutdown("ComputerName", "Shutdown", 30, "Display Message", "Not Shutdown.")

Func _RemoteShutdown($sComputer, $sArgument, $iTimeOut = 0, $sMessage = "", $sErrorLogMessage = Default, $sErrorLogFileName = Default)
    ;Uses #include <File.au3>

    If $sErrorLogMessage = Default Then
        $iCreateLogFile = 0
    Else
        $iCreateLogFile = 1
        If Not IsString($sErrorLogMessage) Or $sErrorLogMessage = "" Then $sErrorLogMessage = "Shutdown not run."
        If $sErrorLogFileName = Default Then $sErrorLogFileName = StringTrimRight(@ScriptFullPath, 4) & " Errors.log"

    EndIf

    $sErrorLogMessage = $sComputer & " : " & $sErrorLogMessage

    $sShutdownOutput = ""
    $iNotExecuted = 0

    If StringInStr($sArgument, "a") Then ;Abort
        $sArgument = "a"
    ElseIf StringInStr($sArgument, "r") Then ;Reboot
        $sArgument = "r"
    ElseIf StringInStr($sArgument, "s") Then ;Shutdown
        $sArgument = "s"
    Else
        $sShutdownOutput &= "Invalid shutdown type argument.  "
    EndIf

    If Not IsInt($iTimeOut) Then
        $sShutdownOutput &= "Invalid Time Out argument.  "
    ElseIf $iTimeOut < 0 Then
        $iTimeOut = 0
    EndIf

    If $sShutdownOutput = "" Then
        Const $STDOUT_CHILD = 2
        If $sArgument = "a" Then
            $iPIDShutdown = Run('shutdown -' & $sArgument & ' -m \\' & $sComputer, '', @SW_HIDE, $STDOUT_CHILD)
        Else
            If $sMessage = "" Then
                $iPIDShutdown = Run('shutdown -' & $sArgument & ' -f -t ' & $iTimeOut & ' -m \\' & $sComputer, '', @SW_HIDE, $STDOUT_CHILD)
            Else
                $iPIDShutdown = Run('shutdown -' & $sArgument & ' -f -t ' & $iTimeOut & ' -c "' & $sMessage & '" -m \\' & $sComputer, '', @SW_HIDE, $STDOUT_CHILD)
            EndIf
        EndIf

        ProcessWaitClose($iPIDShutdown)
        $sShutdownOutput = StringStripWS(StdoutRead($iPIDShutdown), 3)
    EndIf

    If $sShutdownOutput <> "" Then
        $iNotExecuted = 1
        $sErrorMsg = " : " & $sShutdownOutput
    EndIf

    If $iNotExecuted Then

        If $iCreateLogFile Then
            _FileWriteLog($sErrorLogFileName, $sErrorLogMessage & $sErrorMsg)
            $sNamesOnlyErrorLogFileName = StringTrimRight($sErrorLogFileName, 4) & "-Names only.log"
            $iFoundComputerName = 0
            If FileExists($sNamesOnlyErrorLogFileName) Then
                Local $aComputerNames
                _FileReadToArray($sNamesOnlyErrorLogFileName, $aComputerNames)
                For $iNameIndex = 1 To $aComputerNames[0] Step 1
                    If StringInStr($aComputerNames[$iNameIndex], $sComputer) Then $iFoundComputerName += 1
                Next
            EndIf

            If Not $iFoundComputerName Then
                $hComputerNames = FileOpen($sNamesOnlyErrorLogFileName, 1)
                FileWriteLine($hComputerNames, $sComputer)
                FileClose($hComputerNames)
            EndIf

        EndIf

        Return SetError(1, 0, 0)
    Else
        Return 1
    EndIf
EndFunc   ;==>_RemoteShutdown

Adam

Edit: Fixed a line in the function.

Edited by AdamUL

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