Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for shutdown.vbs

(view source code of shutdown.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim strComputer, strQuery
  4.  
  5. ' Check command line arguments
  6. If WScript.Arguments.Named.Count > 0 Then Syntax
  7. Select Case WScript.Arguments.Unnamed.Count
  8. 	Case 0
  9. 		' Default is local computer if none specified
  10. 		strComputer = "."
  11. 	Case 1
  12. 		If InStr( WScript.Arguments.Unnamed(0), "?" ) > 0 Then
  13. 			' A "?" in the argument means display help
  14. 			Syntax
  15. 		Else
  16. 			strComputer = WScript.Arguments.Unnamed(0)
  17. 		End If
  18. 	Case Else
  19. 		' No more than 1 argument allowed
  20. 		Syntax
  21. End Select
  22.  
  23. ' Connect to computer
  24. strQuery     = "SELECT * FROM Win32_OperatingSystem WHERE Primary=True"
  25. Set OpSysSet = GetObject( "winmgmts:{(Shutdown)}//" & strComputer & "/root/cimv2" ).ExecQuery( strQuery )
  26.  
  27. ' Actual shutdown command
  28. For Each OpSys In OpSysSet
  29. 	OpSys.Shutdown()
  30. Next
  31.  
  32.  
  33. Sub Syntax
  34. 	Dim strMsg
  35. 	strMsg = "Shutdown.vbs,  Version 1.10" & vbCrLf _
  36. 	       & "Shutdown any WMI enabled computer on the network." _
  37. 	       & vbCrLf & vbCrLf _
  38. 	       & "Usage:  " & UCase( WScript.ScriptName ) _
  39. 	       & "  [ computer_name ]" & vbCrLf & vbCrLf _
  40. 	       & "Where:  ""computer_name""  is the name of the " _
  41. 	       & "computer to be shut down (no" & vbCrLf _
  42. 	       & "                         leading backslashes); " _
  43. 	       & "default is the local computer." & vbCrLf & vbCrLf _
  44. 	       & "Based on a post by Alex Angelopoulos on www.developersdex.com." _
  45. 	       & vbCrLf & vbCrLf _
  46. 	       & "Written by Rob van der Woude" & vbCrLf _
  47. 	       & "http://www.robvanderwoude.com"
  48. 	Wscript.Echo( strMsg )
  49. 	Wscript.Quit 1
  50. End Sub
  51.  

page last uploaded: 2017-07-06, 12:37