Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scheduling Win 2003 Terminal Server reboots

Status
Not open for further replies.
Jan 14, 2004
3
US
I am wondering if anyone has a good tool / technique for scheduling reboots of Windows 2003 servers running Terminal Services in application mode. I have tried schduling both shutdown.exe as well as tsshutdn.exe using 'at' commands / task scheduler with no luck. The only way that I have found to reboot these boxes remotely is to connect to the console session via RDP and manually run the shutdown /r command.

Your help is appreciated.
Thanks!
 
The times I've had this happen it has been because someone else still had a session open on the server. Have you checked that?
 
Put this code in a text file and give it a vbs extension. Then schedule it to run with AT using the /Interactive switch.


On Error Resume Next
mname = "."
Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//" & mname).ExecQuery("select * from Win32_OperatingSystem where Primary=true")
for each OpSys in OpSysSet
OpSys.Reboot()
next
 
Thank you both for the input.

1. No, there are no active sessions on that server.

2. It looks like that .vbs script starts to do something [I see some activity in Task Manager] but it doesn't reboot the server.

Any other ideas??

Thanks again.
 
OK, this script is a little beefier. Offers the choice for FORCE SHUTDOWN. Note the "notes" in the script. If the Screen Saver is PW enabled it does nto work, that might be what is causing your system not to reboot now with the script I gave you earlier.



'/'|| Remote Shutdown.vbs
'||
'|| Created by Harvey Hendricks, MCSE, A+,
'|| March 2001
'||
'|| email: hhendrks@aramco.com
'|| hhend@swbell.net
'||
'||
'|| Based on techniques and ideas from:
'|| SMS admin, SMS Installer, & WMI forums -> '|| Win32 Scripting -> '|| Microsoft Windows Script Technologies -> '|| Microsoft Online Library -> '|| Microsoft VBScript 5.5 documentation
'|| and Microsoft WMI SDK
'||
'||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'|| SCRIPT LOGIC FLOW:
'|| Collects computername from user, calls function to ping the computername
'|| to determine if it is accessible, if not then display message and exit
'|| otherwise continue.
'|| Collects desired action to perform from the user, does error checking on
'|| the input to determine if it is acceptable, if not then display message
'|| and exit otherwise continue.
'|| Set variables and output messages based on the action chosen. Calls
'|| Win32Shutdown with the appropriate variable. Displays success message
'|| and exits
'||
'|| Uses WMI Win32Shutdown method from the Win32_OperatingSystem class
'|| to perform different logoff / powerdown / reboot functions
'||
'|| Testing found the following values to be effective on Win32Shutdown:
'|| Action decimal binary
'|| Logoff 0 0000
'|| Force Logoff 4 0100
'|| Reboot 2 0010
'|| Force Reboot 6 0110
'|| Powerdown 8 1000
'|| Force Powerdown 12 1100
'||
'|| Notice that the third bit from the right appears to be the "FORCE" bit.
'||
'|| A value of 1 will do a shutdown, ending at the "It is safe to turn
'|| off your computer" screen. I have no use for this and did not test it.
'||
'||
'||NOTES: - tested under Windows 2000 Pro. with ACPI compliant systems -
'|| SHOULD work under Windows NT4 without modification IF the
'|| system has compatible versions of WSH / WMI / VBscripting
'||
'||Logoff / Powerdown / Reboot:
'|| Does not work if a password protected screen saver is active or
'|| there is data to save. Either way the system waits for user input.
'||
'||Force Logoff / Force Powerdown / Force Reboot:
'|| Does not work if a password protected screen saver is active, will wait
'|| for user input. Otherwise will close open applications without saving data.
'||
'\/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


'/\/\/\/\/\/\/\/\/\/\/\/\/\/\ start function /\/\/\/\/\/\/\/\/\/\/\/\/\/'\/\/\/\/\/\/\/\/\/\/\/\/\/\/\______________/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
function Ping(byval strName)
dim objFSO, objShell, objTempFile, objTS
dim sCommand, sReadLine
dim bReturn

set objShell = WScript.CreateObject("Wscript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

'Set default return value
bReturn = false

'Create command line to ping and save results to a temp file
sCommand = "cmd /c ping.exe -n 3 -w 1000 " & strName & " > temp.txt"

'Execute the command
objShell.run sCommand, 0, true

'Get the temp file
set objTempFile = objFSO.GetFile("temp.txt")
set objTS = objTempFile.OpenAsTextStream(1)

'Loop through the temp file to see if "reply from" is found,
'if it is then the ping was successful
do while objTs.AtEndOfStream <> true
sReadLine = objTs.ReadLine
if instr(lcase(sReadLine), &quot;reply from&quot;) > 0 then
bReturn = true
exit do
end if
loop

'Close temp file and release objects
objTS.close
objTempFile.delete
set objTS = nothing
set objTempFile = nothing
set objShell = nothing
set objFSO = nothing

'Return value
Ping = bReturn
end function
'/\/\/\/\/\/\/\/\/\/\/\/\/\/\ end function /\/\/\/\/\/\/\/\/\/\/\/\/\/'\/\/\/\/\/\/\/\/\/\/\/\/\/\/\______________/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



'/\/\/\/\/\/\/\/\/\/\/\ Start Main body of script /\/\/\/\/\/\/\/\/\/\/\/'\/\/\/\/\/\/\/\/\/\/\/\_________________________/\/\/\/\/\/\/\/\/\/\/\/\/
'Get computer name to operate on
ComputerName=InputBox(&quot;Enter the Machine name of the computer&quot; & vbCRLF _
& &quot;you wish to Shutdown / Reboot / Logoff&quot;, _
&quot;Remote Shutdown / Reboot / Logoff&quot;, _
&quot;ComputerName&quot;)

'if Cancel selected - exit
If (ComputerName = &quot;&quot;) Then Wscript.Quit

'change the name to uppercase
ComputerName=UCase(ComputerName)

'ping the computername to see if it is accessible
bPingtest = ping(Computername)

If bPingtest = FALSE Then
y = msgbox (&quot;'&quot; & ComputerName & &quot;' is not accessible!&quot; & vbCRLF _
& &quot;It may be offline or turned off.&quot; & vbCRLF _
& &quot;Check the name for a typo.&quot; & vbCRLF, _
vbCritical, ComputerName & &quot; NOT RESPONDING&quot;)
Wscript.Quit
end IF

'Get the action desired
Action=InputBox( _
&quot;Select Action to perform on &quot; & ComputerName & vbCRLF & vbCRLF _
& &quot; 1 - Logoff&quot; & vbCRLF _
& &quot; 2 - Force Logoff ( NO SAVE )&quot; & vbCRLF _
& &quot; 3 - Powerdown&quot; & vbCRLF _
& &quot; 4 - Force Powerdown ( NO SAVE )&quot; & vbCRLF _
& &quot; 5 - Reboot&quot; & vbCRLF _
& &quot; 6 - Force Reboot ( NO SAVE )&quot; & vbCRLF & vbCRLF _
& &quot;NOTE:&quot; & vbCRLF _
& &quot; Using Force will close windows&quot; & vbCRLF _
& &quot; without saving changes!&quot;, _
&quot;Select action to perform on &quot; & ComputerName, &quot;&quot;)

'if Cancel selected - exit
If (Action = &quot;&quot;) Then Wscript.Quit

'error check input
If (INSTR(&quot;1234567&quot;,Action)=0) OR (Len(Action)>1) then
y = msgbox(&quot;Unacceptable input passed -- '&quot; & Action & &quot;'&quot;, _
vbOKOnly + vbCritical, &quot;That was SOME bad input!&quot;)
Wscript.Quit
end if

' set flag to disallow action unless proper input is achieved, 1 => go 0 => nogo
flag = 0

'set variables according to computername and action
Select Case Action
Case 1 'Logoff
x = 0
strAction = &quot;Logoff sent to &quot; & ComputerName
flag = 1
Case 2 'Force Logoff
x = 4
strAction = &quot;Force Logoff sent to &quot; & ComputerName
flag = 1
Case 3 'Powerdown
x = 8
strAction = &quot;Powerdown sent to &quot; & ComputerName
flag = 1
Case 4 'Force Powerdown
x = 12
strAction = &quot;Force Powerdown sent to &quot; & ComputerName
flag = 1
Case 5 'Reboot
x = 2
strAction = &quot;Reboot sent to &quot; & ComputerName
flag = 1
Case 6 'Force Reboot
x = 6
strAction = &quot;Force Reboot sent to &quot; & ComputerName
flag = 1
Case 7 'Test dialog boxes
y = msgbox(&quot;Test complete&quot;, vbOKOnly + vbInformation, &quot;Dialog Box Test Complete&quot;)
flag = 0
Case Else 'Default -- should never happen
y = msgbox(&quot;Error occurred in passing parameters.&quot; _
& vbCRLF & &quot; Passed '&quot; & Action & &quot;'&quot;, _
vbOKOnly + vbCritical, &quot;PARAMETER ERROR&quot;)
flag = 0
End Select

'check flag
' if equal 1 (TRUE) then perform Win32Shutdown action on remote PC
' and display a confirmation message
' if not equal 1 (FALSE) then skip the action and script ends
if flag then
Set OpSysSet=GetObject(&quot;winmgmts:{(Debug,RemoteShutdown)}//&quot; _
& ComputerName & &quot;/root/cimv2&quot;).ExecQuery( _
&quot;Select * from Win32_OperatingSystem where Primary=true&quot;)
for each OpSys in OpSysSet
OpSys.Win32Shutdown(x)
y = msgbox(strAction,vbOKOnly + vbInformation,&quot;Mission Accomplished&quot;)
next
end If

'Release objects
set OpSys = nothing
set OpSysSet = nothing
 
Thanks again for your help!

I am able to run /schedule either of these 2 helpful scripts against other machines. However I am still having no luck scheduling them to run against my T/S 2003 servers.

I have just downloaded a trial of a program designed to handle rebooting terminal servers. I will see how that works out for me.

 
Try using the IISRESET /REBOOT /TIMEOUT:0 to forcefully reboot a server. Seems to work well in cases where other instances fail.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top