Jump to content

how to shutdown a remote pc


Recommended Posts

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
Link to comment
Share on other sites

  • 10 years later...

Hello friends,
I want to do;
If the program sends a shutdown request to the computer to be closed, a warning will appear, (Shutdown takes place? YES NO) If the user wants to continue working, the shutdown will not take place, if possible, a feedback will be sent to the sender with a warning.
I would be very happy if you could help with the change needed to ensure this process.
Thanks in advance.

#NoTrayIcon
#RequireAdmin
#include <inet.au3>
#include <File.au3>
#include <Misc.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

_Singleton(@ScriptName, 0)

Global $var, $msg, $sComputer, $iCreateLogFile, $ip, $iPIDShutdown, $sNamesOnlyErrorLogFileName, $iFoundComputerName, $hComputerNames, $sShutdownOutput
Global $sErrorLogMessage, $sErrorLogFileName, $iNotExecuted, $sErrorMsg
Global $sArgument = "s"
Global $sMessage = "Your computer shuts down remotely."
Global $gui = GUICreate("Remote Shutdown v1.2 _ank_",430,130,@DesktopWidth + (-430), @DesktopHeight + (-170), BitOR($WS_BORDER,$WS_EX_TOOLWINDOW))
GUISetBkColor(0xABCDEF)
$var = IniReadSection(@ScriptDir &"\Configrs.ini", "PC")
If @error Then MsgBox(4096, "", "An error occurred, probably no ini file.")

Global $menu = GUICtrlCreateLabel("Computer to Shut Down  :      IP Number      :           Status      :",5,5,400,25)
GUICtrlSetFont(-1, 12, 800, 0, "Arial Narrow")

Global $combo = GUICtrlCreateCombo("", 5, 30,160,30,BitOR($WS_VSCROLL, $CBS_DROPDOWNLIST,$ES_READONLY))
GUICtrlSetFont($combo, 12, 800, 0, "Arial Narrow")
Global $Statu = GUICtrlCreateLabel ("", 320,30,80,30)
Global $User = GUICtrlCreateLabel ($ip, 180,30,140,25)
GUICtrlSetFont($User, 14, 800, 0, "Arial Narrow")

Global $Close = GUICtrlCreateButton("SHUTDOWN COMPUTER",5,65,160,30)
GUICtrlSetFont($Close, 12, 800, 0, "Arial Narrow")
GUICtrlSetState ($Close,$GUI_DISABLE)
Global $Abort = GUICtrlCreateButton("CLOSE PROGRAM",260,65,160,30)
GUICtrlSetFont($Abort, 12, 800, 0, "Arial Narrow")


For $i = 1 To $var[0][0]
    GUICtrlSetData($combo, $var[$i][1])
Next

TCPStartup()
GUISetState()

While 1
    $msg = GUIGetMsg()
        Switch $msg
    Case $combo
        $sComputer = GUICtrlRead($combo)
        $ip = TCPNameToIP($sComputer)
        _Checkx()

    Case $Close
        If $sComputer <> "" Then
        Local $v1 = MsgBox(36, "ATTENTION", "Your network "&$sComputer&" you are shutting down the computer" &@CRLF& "Are you sure you want to shut down the computer?")
            If $v1 = 6 Then
                _RemoteShutdown($sComputer, $sArgument, 30, $sMessage, "a")
                ExitLoop
            ElseIf $v1 = 2 Then
                Exit
            EndIf
        Else
            MsgBox(48, "ERROR", "Didn't choose the computer to shut down?")
        EndIf

    Case $Abort
        Local $v2 = MsgBox(36, "WARNING", "You are closing the program" &@CRLF& "Are you sure you want to close the program?")
        If $v2 = 6 Then
            Exit
        ElseIf $v2 = 2 Then
        EndIf
EndSwitch
WEnd

Func _RemoteShutdown($sComputer, $sArgument, $iTimeOut = 0, $sMessage="", $sErrorLogMessage = Default, $sErrorLogFileName = Default)
    If $sErrorLogMessage = Default Then
        $iCreateLogFile = 0
    Else
        $iCreateLogFile = 1
        If Not IsString($sErrorLogMessage) Or $sErrorLogMessage = "" Then $sErrorLogMessage = "Shutdown does not work."
        If $sErrorLogFileName = Default Then $sErrorLogFileName = StringTrimRight(@ScriptDir, 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 Timeout argument.  "
    ElseIf $iTimeOut < 0 Then
        $iTimeOut = 0
    EndIf
    If $sShutdownOutput = "" Then
       Local 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.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

Func _Checkx()
    If $sComputer <> "" Then $var = Ping($sComputer)
    If not @error Then
        GUICtrlSetData($Statu, "PC ON")
        GUICtrlSetColor($Statu, $COLOR_GREEN)
        GUICtrlSetFont($Statu, 12, 800, 0, "Arial Narrow")
        GUICtrlSetData($User, $ip)
        GUICtrlSetColor($User, $COLOR_GREEN)
    Else
        GUICtrlSetData($Statu, "PC OFF")
        GUICtrlSetColor($Statu, $COLOR_RED)
        GUICtrlSetFont($Statu, 12, 800, 0, "Arial Narrow")
        GUICtrlSetData($User, $ip)
        GUICtrlSetColor($User, $COLOR_RED)
    Endif
    Dim $ssx = GUICtrlRead($Statu)
    If  $ssx <> "PC OFF" Then GUICtrlSetState($Close,$GUI_ENABLE)
    If  $ssx == "PC OFF" Then GUICtrlSetState($Close,$GUI_DISABLE)
EndFunc

 

Link to comment
Share on other sites

  • 2 weeks later...

@mucitbey, in this forum we do not encourage resurrecting old topics, even if your issue does have some similarity to the original topic. in the future, unless you have something to contribute to a recently-active topic, please start a new topic.

now, how about you try "shutdown.exe -i" and see how that works for you?

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

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