Export (0) Print
Expand All

Restarting a Computer

Microsoft® Windows® 2000 Scripting Guide

The ability to programmatically restart a computer allows administrators to perform many computer management tasks remotely. For example, if you create a script to install software or make a configuration change that requires restarting a computer, you can include the restart command in the script and perform the entire operation remotely.

The Win32_OperatingSystem class includes a Reboot method that can be used to restart a computer. Like the Win32Shutdown method, the Reboot method requires the user whose security credentials are being used by the script to possess the Shutdown privilege. In addition, the Shutdown privilege must be explicitly included in the script moniker.

Scripting Steps

Listing 8.25 contains a script that restarts a computer. To carry out this task, the script must perform the following steps:

  1. Use a GetObject call to connect to the Win32_OperatingSystem class. This call returns a collection consisting of the operating system currently in use on the computer.

    Because restarting a computer requires the Shutdown privilege, this privilege must be included as part of the moniker.

  2. For each operating system in the collection, use the Reboot method to restart the computer.

Listing 8.25 Restarting a Computer


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
 strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft