Reboot Dialog 500 bones for a code sample

To all my expert friends,

I need a code snippet for vb.net that reboots the client machine.

Mydasx
LVL 5
mydasxAsked:
Who is Participating?
 
heintalusCommented:
David, thats an old way of using WMI, in DotNet you should be using the System.Management namespace classes.

Here's an example, taken from my FAQ on WMI at VBCity  
http://www.vbcity.com/forums/faq.asp?fid=30&cat=General&#TID90733

Another useful article on WMI in Dotnet is
http://www.devcity.net/Articles/144/1/article.aspx

Public Enum Win32ShutdownFlag
        [Logoff] = 0
        [Shutdown] = 1
        [Reboot] = 2
        [ForceLogOff] = 4
        [ForceReboot] = 6
        [PowerDown] = 8
        [ForcePowerDown] = 12
End Enum

Private Sub RebootPC(ByVal flg As Win32ShutdownFlag)
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        options.Username = _AdminUser
        options.Password = _AdminPassword
        Dim myScope As New ManagementScope("\\" & _MachineName & "\root\cimv2", options)
        Dim oQuery As New SelectQuery("SELECT * FROM win32_operatingsystem")
        Dim oResults As New ManagementObjectSearcher(myScope, oQuery)
        Dim oItem As ManagementObject
        Dim oProperty As PropertyData
        Dim myParams() As Object = {flg, 0}

        For Each oItem In oResults.Get()
            oItem.InvokeMethod("Win32Shutdown", myParams)
        Next
End Sub

HTH
Andy
0
 
davidrichardsonCommented:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
mydasxAuthor Commented:
neither of these are very straight forward.  For 500 bones i'm looking for a code snippet that calls reboot, i dont want a form, i dont want to parse through a bunch of other code.  What's the api call for reboot (non debug) what considerations do i need to make for different windows versions... etc.
0
 
bchoorCommented:
If you're using Windows XP

' to shutdown
System.Diagnostics.Process.Start("Shutdown", "-s")

' to reboot
System.Diagnostics.Process.Start("Shutdown", "-r")

' to force shutdown
System.Diagnostics.Process.Start("Shutdown", "-s -f")

to get more arguments for the windows shutdown command, in command prompt type: shutdown /?

HTH
~BC
0
 
davidrichardsonCommented:
this is the code to reboot

        Dim t As Single
        Dim objWMIService, objComputer As Object
        'Now get some privileges
        objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
        For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")
            t = objComputer.Win32Shutdown(2 + 4, 0)
            If t <> 0 Then
                MsgBox("Error occurred!!!")
            Else
            End If
        Next
0
 
bchoorCommented:
0
 
mydasxAuthor Commented:
David:  I get a message that option strict doesnt like late binding how do i work around that...  i cannot turn option strict off...
0
 
davidrichardsonCommented:
0
 
mydasxAuthor Commented:
andy,  will this require that i know an admin user and its pass, as this is for a dialog post install of an msi where i am needing reboot after install.  thus i dont know the users credentials.  Windows has built in users for this case right?
0
 
heintalusCommented:
If it's from an msi then there's a flag in there that you can set to tell the install that a reboot is required & you don't have to do it from code.  It's so long since I've built an msi that I can't remember exactly what it is, but believe it's a variable within the msi that you have to set.

The code I posted above is for rebooting a remote machine where you'd have to send in credentials of an administrator.  If however you were a domain admin trying to reboot a remote machine then you wouldn't need to send in the credentials so could leave out the ConnectionOptions.  The same would apply if you were doing it on the local machine & you had admin rights.

However I suggest you look at the flag within the msi as thats the easiest way of doing it.

HTH
Andy

0
 
mydasxAuthor Commented:
thanx dude.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.