How to reboot a Windows System using WMI ?

If you know the administrator password of a system that you are authorized to use, you can use this function to reboot it for some automation needs. please do not try this on systems that you are not authorized to use. I have shown only the function definition here. You can use this in a QTP function library.


Sub Reboot_System(ByVal strTargetSystem, ByVal strUserName, ByVal strPassword)

Const FORCED_REBOOT = 6
Dim objLocator
Dim objWMIService

wbemImpersonationLevelImpersonate = 3
wbemAuthenticationLevelPktPrivacy = 6
Set objLocator = CreateObject(“WbemScripting.SWbemLocator”)
On Error Resume Next
Set objWMIService = objLocator.ConnectServer _
(strTargetSystem, “root\cimv2”, strUserName, strPassword)
objWMIService.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
objWMIService.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPrivacy

Set colOS = objWMIService.ExecQuery(“SELECT * FROM Win32_OperatingSystem”)

For Each objOS In colOS ‘only one
intReturn = objOS.Win32Shutdown(FORCED_REBOOT)
If intReturn 0 Then
Reporter.ReportEvent micFail, “Error: Unable to reboot.” & VbCrLf & _
“Return code: ” & intReturn
else
Reporter.ReportEvent micPass, “Successfully rebooted” & VbCrLf & _
“Return code: ” & intReturn
End If
Next

End Sub

Leave a comment