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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to shutdown the exchange server faster -- help 2

Status
Not open for further replies.

bptech

Technical User
Joined
Jul 18, 2004
Messages
2
Location
US
Hi,

I am illiterated in vbscript, I save this script as test.bat and run it in windows 2003 cmd line --> error?? the script is as below.

Please help to instruct of how to run this one.
------------------------------------------------------
servername = "2kserver"
set wmi = getobject("winmgmts://" & servername)
StopService ("Microsoft Exchange System Attendant")
StopService ("IIS Admin Service")
StopService ("Netlogon")

Sub StopService (ServiceName)
wql = "select state from win32_service " _
& "where displayname='"& ServiceName & "'"
set results = wmi.execquery(wql)
for each service in results
if service.state = "Running" then
service.stopService
end if
next
End Sub

---------------------------------------------------------
Bptech
--------------------------------------------------------
 
Hello bptech,

For a script as such, you need to save it as somename.vbs. (Extension .vbs not .bat. It is not obligatory, but it is a general practice using default association.) To run it at commandprompt, the most basic would be to enter:
cscript somename.vbs
with cscript within the search path.

regards - tsuji
 
Hi bptech,

Nice script. I have a suggestion for taking this to the next level.

Code:
'==========================================================================
'
' NAME: ShutdownRebootExchangeServer.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 7/19/2004
'
' COMMENT: 
'
'==========================================================================
On Error Resume Next

'start with getting the local computer name
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem",,48)

For Each objItem in colItems
    strComputer = objItem.CSName
Next

'now stop the services
Set objWMI = getobject("winmgmts://" & strComputer)
StopService ("Microsoft Exchange System Attendant")
StopService ("IIS Admin Service")
StopService ("Netlogon")

'now we are ready to actually shutdown or reboot the server
Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//" & strComputer).ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each OpSys in OpSysSet
	'prompt for action to take.  Put in lower case in the event someone actually types shutdown or reboot.
	action = lcase(InputBox("Shutdown or Restart?" & vbCrLf & "Shutdown=1" & vbCrLf & "Reboot=2", "Action to take?"))
		
		Select Case action
		Case "1" 
			OpSys.Shutdown()
		Case "2" 
			OpSys.Reboot()
		'support for the actual words shutdown and reboot in case someone did not enter a 1 or 2
		Case "shutdown" 
			OpSys.Shutdown()
		Case "reboot" 
			OpSys.Reboot()
		Case Else
			MsgBox("Incorrect input, exiting script",,"Incorrect Input")
			WScript.quit
		End Select
Next

'sub to stop the services
Sub StopService (ServiceName)
         queryString = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = objWMI.execquery(queryString)
         for each service in results
            if service.state = "Running" then
             service.stopService
            end if
          next
End Sub


I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
Mark,

Thank for the inside.

You make me learn vbscrip more and more.
Will try to use it and get back to you.

Great board -- thank everybody -- will try to contribute soon.

bptech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top