Force Reboot

The computer is not local - several miles from ANYWHERE - in some instances we have to travel out to the location (not easily accessible in some weather) just to reboot the system. Most of the time you can ping the pc etc. I think to save time we need to be able to forece a reboot from within the program.

In any case there are (I beleive) reasonable circumstances. The people that travel to the location usually end up just hitting the switch anyway.
 
Theres no way built in to the .NET framework. Look up the ExitWindowsEx API instead.
 
Thats what I thought...
Beenworking with that - seems to work somethime - sometimes not. Never at all from a service??
 
From the help for ExitWindowsEx:
The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the callers logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.

If youre running this from a service youre probably not the interactive user (same as if you were to run this from a COM+ component with an Identity set) so the Shutdown may not work.

Did you specify the following flags: EWX_FORCE, EWX_FORCEIFHUNG, etc.?
There are some notes in the help when you may want to use these flags and you may need more than the two above.

Other than the above, I have no idea. Ive never tried rebooting programatically. It seems like youd only want to force a reboot when the machine was hung, but then your program or service isnt likely to succeed in rebooting. If something else is wrong, like the websites not responding, youd want to FIX instead of just rebooting. You could shell out to "iisreset" or something similar. If COM+ stops responding you could restart the services and optionally issue a "shutdown" command to each package. 99% of the time there will be something else wrong and you wont necessarily have to reboot.

We have some development servers that have been up for over 180 days. One runs SQL Server another runs our development website. Granted, we have to issue "iisreset" a couple of times a week after someone locks it up, but rebooting is usually something you do when you dont know enough to fix whats really wrong, IMO. Its a good oportunity to learn more about what your programs are doing possibly :)

-nerseus
 
Well, Dear if the system if miles away from you, how do you want to shut down it. Remotely or by sending some EXE to user and ask it to open file. if you mention the second one then ExitWindowsEx works but if the first one mean remotely then. first of all you need a make a client program with winsock which listen for commands and then the server program which sends commands to client.
if you need code i have it.
 
I have a snippet that listens to a port for the command to reboot.
It works now using the API call (of course still not in service - permissions should fix that.)

Note that this is a VERY RARE occurrence. I am going to do some more digging into directing it more toward starting and stopping services as needed though.

I appreciate all of the help and comments everybody has had on this subject :)
 
You can use WMI to reboot a remote PC. The below code calls the WMI reboot() method from VB.NET. It reboots the current PC ("."). If you substitute this for another machine name my changing strComputer and you have access to that machine, it should reboot the remote PC (although I havent tested this funcitonality).

Code:
    Sub WMIReboot()
        Dim strComputer As String = "."
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        Dim ms As New ManagementScope("\\" & strComputer & "\root\CIMV2", options)
        Dim q As New SelectQuery("SELECT * FROM Win32_OperatingSystem")
        Dim search As New ManagementObjectSearcher(ms, q)

         enum each entry for Win32_OperatingSystem and call WMI reboot method
        Dim info As ManagementObject
        For Each info In search.Get()
            info.InvokeMethod("Reboot", Nothing)
        Next
    End Sub

If you want to do this from a VBS script instead of a compiled EXE, checkout the examples in the MS script center: http://www.microsoft.com/technet/treeview/default.asp?url=/technet/scriptcenter/default.asp.
 
Back
Top