大网数据广告活动
Windows 系统接口

Reboot method of the Win32_OperatingSystem class

· 52CE 我爱测

The Reboot&\#8194;WMI class method shuts down the computer system, then restarts it.

The Reboot WMI class method shuts down the computer system, then restarts it.

This topic uses Managed Object Format (MOF) syntax. For more information about using this method, see Calling a Method.

Syntax

uint32 Reboot();

Parameters

This method has no parameters.

Return value

Returns zero (0) to indicate success. Any other number indicates an error. For error codes, see WMI Error Constants or WbemErrorEnum. For general HRESULT values, see System Error Codes.

Success (0)

Other (1 4294967295)

Remarks

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 Reboot method 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.

Examples

The following VBScript code sample invokes the Reboot method of the Win32_OperatingSystem class.

Note:
You must have the Shutdown privilege to successfully invoke the Shutdown method.

Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

for each OpSys in OpSysSet
 OpSys.Reboot()
next

The following Perl code invokes the Reboot method of the Win32_OperatingSystem class.

Note:
You must have the Shutdown privilege to successfully invoke the Shutdown method.

use Win32::OLE;
use strict;
my $OpSysSet;
eval { $OpSysSet = Win32::OLE->GetObject("winmgmts:{(Shutdown)}//./root/cimv2")->
 ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Primary=true"); };

if (!$@ && defined $OpSysSet)
{
 close(STDERR);
 foreach my $OpSys (in $OpSysSet)
 {
  my $RetVal = $OpSys->Reboot(); 
  if (!defined $RetVal || $RetVal != 0)
  {
   print Win32::OLE->LastError, "\n"; 
  }
 }
}
else
{
 print STDERR Win32::OLE->LastError, "\n";
}

The following VBScript invokes the Reboot method of the Win32_OperatingSystem class on a remote system. Fill in REMOTE_SYSTEM_NAME with the name of the remote system to reboot.

Note:
You must have the RemoteShutdown privilege to successfully invoke the Reboot method

Set OpSysSet = GetObject("winmgmts:{(RemoteShutdown)}//REMOTE_SYSTEM_NAME/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

for each OpSys in OpSysSet
 OpSys.Reboot()
next

he following Perl invokes the Reboot method of the Win32_OperatingSystem class on a remote system. Fill in REMOTE_SYSTEM_NAME with the name of the remote system to reboot.

Note:
You must have the RemoteShutdown privilege to successfully invoke the Reboot method.

use strict;
use Win32::OLE;

use constant REMOTE_SYSTEM_NAME => "MACHINENAME";
use constant USERNAME => "USER";
use constant PASSWORD => "PASSWORD";
use constant NAMESPACE => "root\\cimv2";
use constant wbemPrivilegeRemoteShutdown => 23;
use constant wbemImpersonationLevelImpersonate => 3;
close(STDERR);
my ($locator, $services, $OpSysSet);
eval {
  $locator = Win32::OLE->new('WbemScripting.SWbemLocator');
  $locator->{Security_}->{impersonationlevel} = wbemImpersonationLevelImpersonate;
  $services = $locator->ConnectServer(REMOTE_SYSTEM_NAME, NAMESPACE, USERNAME, PASSWORD);
  $services->{Security_}->{Privileges}->Add(wbemPrivilegeRemoteShutdown);
  $OpSysSet = $services->ExecQuery("SELECT * FROM Win32_OperatingSystem WHERE Primary=true");
 };

if (!$@ && defined $OpSysSet)
{
 foreach my $OpSys (in $OpSysSet)
 {
  $OpSys->Reboot();
 }
}
else
{
 print Win32::OLE->LastError, "\n";
 exit(1);
}

Requirements

Requirement Value
Minimum supported client Windows Vista
Minimum supported server Windows Server 2008
Namespace Root\CIMV2
MOF CIMWin32.mof
DLL CIMWin32.dll

See also

Operating System Classes

Win32_OperatingSystem

CIM_OperatingSystem.Shutdown method

WMI Tasks: Desktop Management

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 05/31/2018
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。