If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Forced shutdown of a remote nt/2k server

When restarting a remote NT based server from the gui, NT stops any remote administration service (eg WinVNC/PC-Anywhere/BO etc) that you have running. If the machine fails to reboot cleanly (say a dialog box appears during shutdown) and your remote administration service has stopped you are fuxored. I work from home most of the time and so I object to having to get dressed to go and see sick servers .

To get around this problem I wanted to write a program to implicitly force a reboot from the command line. By 'force a reboot' I mean telling the system to shut down ignoring any dialog boxes, errors, tasks waiting to complete or other anomalies, and restart regardless. This was intended to be an application of last resort, but I have had it implemented on my own servers for a couple of months now and it has been so successful that I made it policy to do all server reboots using this command line app - rather than attempting a shutdown via the gui first.

Notes on the code

This program uses the windows APIs. Because of this you could write it in any number of languages, but the example below is given in Visual Basic. Also I can't take 100% credit for the code - A lot of the API stuff is cut and pasted from API reference sites such as www.vbapi.com, but I have stitched it all together, added comments and the command line interface.

If you're going to try to get this code working in VB you'll have to create a new project containing just one .bas module and add a reference to "Visual Basic for Applications". This is for the retrieval of command line parameters. Then copy and paste the code listed below into your module.

Retrieving the O/S type

This code is only meant to run on versions of WinNT. So first of all we have to have a little bit of sanity checking. Is this a WinNT box? We can use the GetVersionEx API to get the version of our target operating system. The IsWinNT() function populates the myos value (of OSVERSIONINFO type) with information about the target operating system. If the target system is an NT box the following statement (and therefore the IsWinNT() function) will return true:

Ok. Now we know that the target system is NT we need to set the priveledges on our application so that we can shut down the system. To do this we use the GetCurrentProcess, LookupPrivilegeValue and AdjustTokenPrivileges APIs. The EnableShutDown subroutine uses the GetCurrentProcess API to get the handle for the current process and assign that process with permission to shutdown the system. I won't go into this too much here because I document this rather heavily in the code.

Retreiving user input from the command line

Sub main splits command line parameters, which are stored in the command$ variable, into an array using space as the deliminator. The code checks for two parameters, the action (valid options are logoff/reboot/shutdown prefaced by either - or /) and a boolean force variable (similarly prefaced) that decides whether or not to force the selected action. This bit of the code requires a reference to Visual Basic for Application so as to access the script host.

Forcing a reboot

The ShutDownNT procedure accepts the Flags parameter that specifies the action to be taken. The actual action taken by the ExitWindowsEx API contained in the ShutDownNT procedure is defined by assigning the sum of the following constants to the passed Flags variable:

Whilst every care is taken to ensure that files on this FTP are virus and error free you are advised to scan any files you download. Usage is entirely at your own risk. If you use any other software from this server and intent to keep it please support it's creators by buying a valid licence.

impresive bit of coding....personally i am not creative to even think of some of the stuff u did with VB ....i personally dont need it but i was wondering how would u have ot tweak it for win2k serv or adv serv?...i am sure some ppl might need it...now adays win nt isnt used as much and maybe u should recode it for 2k

\"\"A weak mind is like a microscope, which magnifies trifling things but cannot receive great ones.\" — G.K. Chesterton, 19th-century English essayist and poet\"

This works with /all/ flavours of NT. That includes 2k, which is in actual fact NT 5. I don't use XP as yet - never 'buy' (and I use the term loosley here to encompass any transaction you walk away from with a new piece of software ) anything from M$ until /at least/ service pack 3 unless you really want to be a beta tester - but if XP uses the NT kernel (and I don't see why it wouldn't - if it ain't broke you don't fix it) it should work on XP aswell.

\"I may not agree with what you say, but I will defend to the death your right to say it.\"
Sir Winston Churchill.

Ok - apologies if my last post was a bit flippant - it's getting late. Upon refelection what I probably meant was: IMHO the code is worthwhile just for the sake of the code. There are some bits that would be useful for any number of apps so I figured it was worth posting.

\"I may not agree with what you say, but I will defend to the death your right to say it.\"
Sir Winston Churchill.

"shutdown /c /l /r /y" will do what you wanted(closes all programs, answers yes to any popups, and reboots the server).

Just confirming the earlier post about shutdown. Also, how does your system shutdown get logged on Win2K machines? IE., does the shutdown event in the system log show up as expected or unexpected? I have seen some instances where using shutdown from the Win toolkit causes the system to think the system shutdown was unexpected. This is not really a big issue as long as you know what to expect though.

There is also a command called "logoff" in Win2k. Comes in handy when using terminal server.

You can get a list of all users with open sessions on a machine by doing "query session." Then using the session ID provided from that you can "logoff X" where x is the session ID. Just throwing this in because I saw you had some variables for a logoff function.