Jump to content

DOS Shutdown command


Recommended Posts

All,

I have been trying to better understand why the following piece is not working correctly. It runs without error, but does not reboot system. Thanks...

#include <process.au3>

$ipaddress="192.168.50.1"

run('shutdown -r -m \\'&$ipaddress&'')

Link to comment
Share on other sites

Same thing. Infinite loop. Just curious, were you able to run this on your side? Also, just as an isolation point, I do login to the remote machine, from the local before I run this script. Therefore, it should work.

Link to comment
Share on other sites

Same thing. Infinite loop. Just curious, were you able to run this on your side? Also, just as an isolation point, I do login to the remote machine, from the local before I run this script. Therefore, it should work.

I had not tried, but I did and this was the result (after a bit of waiting)

C:\Users\new user>shutdown -r -m \\192.168.50.1
192.168.50.1: The entered computer name is not valid or remote shutdown is not supported on the target computer. Check the name and then try again or contact your system administrator.(53)

Probably got something to do with the fact 192.168.*.* is local isn't it?

Link to comment
Share on other sites

Not sure what you mean.... Here is my setup

1. 192.168.50.1 (Remote System)

2. 192.168.50.2 (Local System)

From my local system, the first thing i do is go to Start->Run and type \\192.168.50.1 and it prompts me for username and password. I enter it and it logs me into the remote system.

Then I run the script and it keep running over and over and over, without rebootin the remote system. Like we said, in theory it should work because the actual command works in the DOS prompt. Ive been all over this and cant figure it out.

Link to comment
Share on other sites

@Mat

Yes, 192.168.*.* is a private networking block. The reserved private networking blocks are

The Internet Assigned Numbers Authority (IANA) has reserved the following three blocks of the IP address space for private internets:

* 10.0.0.0 - 10.255.255.255 (10/8 prefix)

* 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)

* 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)

As you found out, you cannot access these IPs outside of the private network.

@byoda

Does the account you are using to run the script on your local system have Admin rights on the remote PC? Here is a shutdown function that I wrote with error logging to a file. It uses the Windows Shutdown command. You need to see if the shutdown command is throwing any errors while running the script, and this function will log them.

#include <File.au3>

_RemoteShutdown("192.168.50.1", "r", 0, "", "Not Rebooted.")

Func _RemoteShutdown($sComputer, $sArgument, $iTimeOut = 0, $sMessage = "", $sErrorLogMessage = Default, $sErrorLogFileName = Default)
    ;Uses #include <File.au3>
    ;Use #include <Constants.au3> if $STDOUT_CHILD is used somewhere else in the script.

    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

When you speak of an "infinite loop", is that part of your script, or is it just the remote PC not responding? Also if you notice in the code for my function, I use the -f to force processes to close. That could be one of your problems with the machine not rebooting.

Adam

Link to comment
Share on other sites

All,

I got it to work with the following. I just specified the shutdown location.

run(@ComSpec & " /c c:\windows\system32\shutdown.exe r -m \\" & $ipaddress)

Yes it has admin rights. When I speak of the infinite loop, when I run the script on the local machine, it would continously pop up DOS windows until I rebooted the machine. Seems to be working now. I would like to know wtf was going on with it:)

Link to comment
Share on other sites

All,

I got it to work with the following. I just specified the shutdown location.

run(@ComSpec & " /c c:\windows\system32\shutdown.exe r -m \\" & $ipaddress)

Yes it has admin rights. When I speak of the infinite loop, when I run the script on the local machine, it would continously pop up DOS windows until I rebooted the machine. Seems to be working now. I would like to know wtf was going on with it:)

Ah right... I never knew shutdown was a program :mellow:

This is right then?

run("shutdown -r -m \\" & $ipaddress, @SystemDir)

Good to see it sorted :P

Mat

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