Jump to content
Sign in to follow this  
byoda

DOS Shutdown command

Recommended Posts

byoda

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&'')

Share this post


Link to post
Share on other sites
AuToItItAlIaNlOv3R

For reboot system use autoit function :

Shutdown

For reboot use this code :

Shutdown(2)

Remark :

The shutdown code is a combination of the following values:
0 = Logoff
1 = Shutdown
2 = Reboot
4 = Force
8 = Power down
16= Force if hung
32= Standby
64= Hibernate

Hi!

Share this post


Link to post
Share on other sites
byoda

Sounds good, but does it handle remote reboots? I am trying to reboot a remote machine.

Share this post


Link to post
Share on other sites
Mat

Running it like that will try to run a file called "shutdown.exe" in the working dir. You need to run cmd with it as a command line argument.

run(@ComSpec & " /c shutdown -r -m \\" & $ipaddress)

Share this post


Link to post
Share on other sites
byoda

I tried this, but the command continues in a infinite loop and never reboots the remote system. Thanks.

Share this post


Link to post
Share on other sites
Mat

If you go into the windows run dialog and type (or paste) this: "cmd /c shutdown -r -m \\192.168.50.1" and run it then what happens?

Share this post


Link to post
Share on other sites
byoda

The remote machine shutsdown. When I run the script, it goes into infinite loop and machine does not shutdown.

Share this post


Link to post
Share on other sites
Mat

Kk... I don't know then. They *should* be the same in theory...

What about:

run(@ComSpec & " /c shutdown /r /m \\" & $ipaddress)

Mat

Share this post


Link to post
Share on other sites
byoda

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.

Share this post


Link to post
Share on other sites
Mat

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?

Share this post


Link to post
Share on other sites
byoda

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.

Share this post


Link to post
Share on other sites
engjcowi

hi

my code would be slightly diffenerent. just substitute ipconfig for what u need

Run(@ComSpec & " /c " & 'ipconfig.exe', "", @SW_HIDE)


Drunken Frat-Boy Monkey Garbage

Share this post


Link to post
Share on other sites
AdamUL

@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

Share this post


Link to post
Share on other sites
byoda

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:)

Share this post


Link to post
Share on other sites
Mat

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

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  

×