Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Logoff/Restart/Shutdown all machines in a file

Hi,

I have about 50 machine names in a file.Need to logoff/Restart/Shutdown all machines in the file.

Is there a script to do this.

Regards
Sharath
Avatar of mailtosinghs
mailtosinghs

if you are in a domain environment run

shutdown -i

fill in the information in the dialog box and click ok

you can add computers either by browsing AD or click add and paste system name each in separate line, you can also copy and paste the system name.
Avatar of Brian Pierce
Use the shutdown command in a batch file

SHUTDOWN -m \\Computer2 -s -f -t 30
SHUTDOWN -m \\Computer3 -s -f -t 30
SHUTDOWN -m \\Computer4 -s -f -t 30

etc

for full syntax see http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/shutdown.mspx?mfr=true

-s = shutdown
-r = restart
-l

Where the number after -t is the time in seconds before shut down. It cannot be set to zero as far as I know.
:: * This script require "Computers.txt" file on C: drive root from where it will pick computer names.
:: * You need to set 'Action' variables value to following
::      SET Action=-s  ::For shutdown
::      SET Action=-r  ::For Restart
::      SET Action=-l  ::For Logoff
:: * Copy and Paste following script into notepad and save it with any name having .bat extension.

:: SCRIPT START
@Echo Off
SETLOCAL
SET Action=-s

IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR %%R IN (Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\UProfiles.txt DEL /F /Q C:\UProfiles.txt

FOR /F "delims=#" %%c IN (C:\Computers.txt) Do (
      Echo Processing: %%c
      Echo SHUTDOWN -m \\%%c %Action% -f -t 30
)
:: Batch Script End

Goto EndScript
:ShowErr
Echo "C:\Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: SCRIPT END
Avatar of bsharath

ASKER

I get this.

Processing: indiasophos
SHUTDOWN -m \\indiasophos -l -f -t 30
Press any key to continue . . .
Sorry try following...

:: * This script require "Computers.txt" file on C: drive root from where it will pick computer names.
:: * You need to set 'Action' variables value to following
::      SET Action=-s  ::For shutdown
::      SET Action=-r  ::For Restart
::      SET Action=-l  ::For Logoff
:: * Copy and Paste following script into notepad and save it with any name having .bat extension.

:: SCRIPT START
@Echo Off
SETLOCAL
SET Action=-s

IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR %%R IN (Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\UProfiles.txt DEL /F /Q C:\UProfiles.txt

FOR /F "delims=#" %%c IN (C:\Computers.txt) Do (
      Echo Processing: %%c
      SHUTDOWN -m \\%%c %Action% -f -t 30
)
:: Batch Script End

Goto EndScript
:ShowErr
Echo "C:\Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: SCRIPT END
I get this.

Processing: indc01
Usage: SHUTDOWN [/i | /l | /s | /r | /a | /p | /h | /e] [/f]
    [/m \\computer][/t xxx][/d [p:]xx:yy [/c "comment"]]

    No args    Display help. This is the same as typing /?
    /?         Display help. This is the same as not typing any options
    /i         Display the graphical user interface (GUI).
               This must be the first option
    /l         Log off. This cannot be used with /m or /d option
    /s         Shutdown the computer
    /r         Shutdown and restart the computer
    /a         Abort a system shutdown.
               This can only be used during the time-out period
    /p         Turn off the local computer with no time-out or warning.
               This can only be used with /d option
    /h         Hibernate the local computer.
               This can only be used with the /f option
    /e         Document the reason for an unexpected shutdown of a computer
    /m \\computer Specify the target computer
    /t xxx     Set time-out period before shutdown to xxx seconds.
               The valid range is 0-600, with a default of 30
    /c "comment" Comment on the reason for the restart or shutdown.
               Maximum of 127 characters allowed
    /f         Force running applications to close without forewarning users
    /d [p:]xx:yy  Provide the reason for the restart or shutdown
               p indicates that the restart or shutdown is planned
               xx is the major reason number (positive integer less than 256)
               yy is the minor reason number (positive integer less than 65536)

Reasons on this computer:
(E = Expected U = Unexpected P = planned, C = customer defined)
Type    Major   Minor   Title

 U      0       0       Other (Unplanned)
E       0       0       Other (Unplanned)
E P     0       0       Other (Planned)
 U      0       5       Other Failure: System Unresponsive
E       1       1       Hardware: Maintenance (Unplanned)
E P     1       1       Hardware: Maintenance (Planned)
E       1       2       Hardware: Installation (Unplanned)
E P     1       2       Hardware: Installation (Planned)
  P     2       3       Operating System: Upgrade (Planned)
E       2       4       Operating System: Reconfiguration (Unplanned)
E P     2       4       Operating System: Reconfiguration (Planned)
  P     2       16      Operating System: Service pack (Planned)
        2       17      Operating System: Hot fix (Unplanned)
  P     2       17      Operating System: Hot fix (Planned)
        2       18      Operating System: Security fix (Unplanned)
  P     2       18      Operating System: Security fix (Planned)
E       4       1       Application: Maintenance (Unplanned)
E P     4       1       Application: Maintenance (Planned)
E P     4       2       Application: Installation (Planned)
E       4       5       Application: Unresponsive
E       4       6       Application: Unstable
 U      5       15      System Failure: Stop error
E       5       19      Security issue
 U      5       19      Security issue
E P     5       19      Security issue
E       5       20      Loss of network connectivity (Unplanned)
 U      6       11      Power Failure: Cord Unplugged
 U      6       12      Power Failure: Environment
  P     7       0       Legacy API shutdown
Press any key to continue . . .
you can use the interactive interface of shutdown.ex with -i flag, and select options interactively. you can also copy and paste all systems name in the computers list. so if you don't need to script this for scheduling you can go with my sugession.
SINTAX ERROR IN SHUTDOWN -L ...
Shutdown.exe
  /l  Log off. This cannot be used with /m or /d option

:: NEW VERSION
::      SET Action=S  ::For shutdown
::      SET Action=R ::For Restart
::      SET Action=L ::For Logoff
::
:: IF YOU LEAVE EMPTY (SET ACTION=) THE SCRIPT IS IN INTERACTIVE MODE
::
:: SCRIPT START
@Echo OFF
SETLOCAL
:------------------------------------------------
SET Action=
::------------------------------------------------
if +%Actions%+==++ (
rem Set the default actions if Action is empty
  set Action=L
rem ask to the user the action
  set /p Action=Shutdown/Restart/Logoff [s,r,L]?
)  
IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR  %%R IN  (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
FOR /F "delims=#" %%c IN ('TYPE C:\Computers.txt') Do (
      Echo Processing: %%c
      rem  /l  Log off. This cannot be used with /m or /d option
      if /i +%Action%+==+L+  ( c:\psexec.exe \\%%c -i -s shutdown -l -f -t 30
      ) else (  SHUTDOWN -m \\%%c -%Action% -f -t 30 )
)
Goto EndScript
:ShowErr
Echo "C:\Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: SCRIPT END
Ops! my mistake Thanks Mailtosinghs and Canali
Mailtosinghs, with shutdown -i  you can't logoff  computers but only shutdown or restart, logoff is  only for the local computer
thats right

you cannot log off the user remotely using shutdown.exe

infact, for loggig off I'm using in the script a psexec + shutdown
Bye Gas
canali

I get this.

Shutdown/Restart/Logoff [s,r,L]?L
Processing: indiasophos

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com


Usage: shutdown [/i | /l | /s | /r | /a | /p | /h | /e] [/f]
    [/m \\computer][/t xxx][/d [p:]xx:yy [/c "comment"]]

    No args    Display help. This is the same as typing /?
    /?         Display help. This is the same as not typing any options
    /i         Display the graphical user interface (GUI).
               This must be the first option
    /l         Log off. This cannot be used with /m or /d option
    /s         Shutdown the computer
    /r         Shutdown and restart the computer
    /a         Abort a system shutdown.
               This can only be used during the time-out period
    /p         Turn off the local computer with no time-out or warning.
               This can only be used with /d option
    /h         Hibernate the local computer.
               This can only be used with the /f option
    /e         Document the reason for an unexpected shutdown of a computer
    /m \\computer Specify the target computer
    /t xxx     Set time-out period before shutdown to xxx seconds.
               The valid range is 0-600, with a default of 30
    /c "comment" Comment on the reason for the restart or shutdown.
               Maximum of 127 characters allowed
    /f         Force running applications to close without forewarning users
    /d [p:]xx:yy  Provide the reason for the restart or shutdown
               p indicates that the restart or shutdown is planned
               xx is the major reason number (positive integer less than 256)
               yy is the minor reason number (positive integer less than 65536)

Reasons on this computer:
(E = Expected U = Unexpected P = planned, C = customer defined)
Type    Major   Minor   Title

 U      0       0       Other (Unplanned)
E       0       0       Other (Unplanned)
E P     0       0       Other (Planned)
 U      0       5       Other Failure: System Unresponsive
E       1       1       Hardware: Maintenance (Unplanned)
E P     1       1       Hardware: Maintenance (Planned)
E       1       2       Hardware: Installation (Unplanned)
E P     1       2       Hardware: Installation (Planned)
  P     2       3       Operating System: Upgrade (Planned)
E       2       4       Operating System: Reconfiguration (Unplanned)
E P     2       4       Operating System: Reconfiguration (Planned)
  P     2       16      Operating System: Service pack (Planned)
        2       17      Operating System: Hot fix (Unplanned)
  P     2       17      Operating System: Hot fix (Planned)
        2       18      Operating System: Security fix (Unplanned)
  P     2       18      Operating System: Security fix (Planned)
E       4       1       Application: Maintenance (Unplanned)
E P     4       1       Application: Maintenance (Planned)
E P     4       2       Application: Installation (Planned)
E       4       5       Application: Unresponsive
E       4       6       Application: Unstable
 U      5       15      System Failure: Stop error
E       5       19      Security issue
 U      5       19      Security issue
E P     5       19      Security issue
E       5       20      Loss of network connectivity (Unplanned)
 U      6       11      Power Failure: Cord Unplugged
 U      6       12      Power Failure: Environment
  P     7       0       Legacy API shutdown
shutdown exited on indiasophos with error code 1.
Press any key to continue . . .
Any help....
Try this

:: NEW VERSION
::      SET Action=S  ::For shutdown
::      SET Action=R ::For Restart
::      SET Action=L ::For Logoff
::
:: IF YOU LEAVE EMPTY (SET ACTION=) THE SCRIPT IS IN INTERACTIVE MODE
::
:: SCRIPT START
@Echo OFF
SETLOCAL
:------------------------------------------------
SET Action=
::------------------------------------------------
if +%Actions%+==++ (
rem Set the default actions if Action is empty
  set Action=L
rem ask to the user the action
  set /p Action=Shutdown/Restart/Logoff [s,r,L]?
)  
IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR  %%R IN  (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
FOR /F "delims=#" %%c IN ('TYPE C:\Computers.txt') Do (
      Echo Processing: %%c
      rem  /l  Log off. This cannot be used with /m or /d option
      if /i +%Action%+==+L+  ( c:\psexec.exe \\%%c LogOff
      ) else (  SHUTDOWN -m \\%%c -%Action% -f -t 30 )
)
Goto EndScript
:ShowErr
Echo "C:\Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: SCRIPT END
I get this.

Shutdown/Restart/Logoff [s,r,L]?L
Processing: in-bkp-srv02

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com


Could not logoff current session ID, Error code 1
Error [1]:Incorrect function.
LogOff exited on in-bkp-srv02 with error code 1.
Processing: in-bkp-srv01

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com


Could not logoff current session ID, Error code 1
Error [1]:Incorrect function.
LogOff exited on in-bkp-srv01 with error code 1.
:: NEW VERSION
::      SET Action=S  ::For shutdown
::      SET Action=R ::For Restart
::      SET Action=L ::For Logoff
::
:: IF YOU LEAVE EMPTY (SET ACTION=) THE SCRIPT IS IN INTERACTIVE MODE
::
:: SCRIPT START
@Echo OFF
SETLOCAL
:------------------------------------------------
SET Action=
::------------------------------------------------
if +%Actions%+==++ (
rem Set the default actions if Action is empty
  set Action=L
rem ask to the user the action
  set /p Action=Shutdown/Restart/Logoff [s,r,L]?
)  
IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR  %%R IN  (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
FOR /F "delims=#" %%c IN ('TYPE C:\Computers.txt') Do (
      Echo Processing: %%c
      rem  /l  Log off. This cannot be used with /m or /d option
      if /i +%Action%+==+L+  ( c:\psexec.exe \\%%c shutdown -l
      ) else (  SHUTDOWN -m \\%%c -%Action% -f -t 30 )
)
Goto EndScript
:ShowErr
Echo "C:\Computers.txt" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: SCRIPT END
I get this

Shutdown/Restart/Logoff [s,r,L]?L
Processing: in-bkp-srv02

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com


IN-BKP-SRV02: Incorrect function.(1)
shutdown exited on in-bkp-srv02 with error code 1.
Processing: in-bkp-srv01

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com


IN-BKP-SRV01: The system could not find the environment option that was entered.
(203)
shutdown exited on in-bkp-srv01 with error code 1.
Press any key to continue . . .
Try following post the results.

Click on Start -> Run -> Cmd.exe

PSShutdown \\IN-BKP-SRV02 -o

Note: PSShutdown comes with PSTools

PSExec.exe \\IN-BKP-SRV02 -i shutdown -l -f
PSExec.exe \\IN-BKP-SRV02 -i Logoff
i get this.

C:\>PSExec.exe \\IN-BKP-SRV02 -i Logoff

PsExec v1.63 - Execute processes remotely
Copyright (C) 2001-2005 Mark Russinovich
Sysinternals - www.sysinternals.com
Logoff exited on IN-BKP-SRV02 with error code -1073741510.
What about PSShutdown \\IN-BKP-SRV02 -o
PSShutdown \\IN-BKP-SRV02 -o

This logged me off from the machine.
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial