Shutting down, restarting or logging out via shortcuts, batch files and scripts – Part 2

Attachments: Shutdown scripts Shutdown Scripts
This article has multiple attachments

Introducing PsShutdown.exe

In the previous episode, we saw Shutdown.exe, a command-line tool that could perform shutdown, restart or logout. We also saw that the earlier version of Shudown.exe that is included in Windows XP suffers from certain discrepancies that severely limit its usage scenarios.

In addition to Shutdown.exe, a freeware called PsShutdown.exe is also available from Microsoft that features additional capabilities over Shutdown.exe in Windows XP. It can hibernate a computer or lock computer. (Locking a computer returns the user to logon screen without closing his programs. User can return back to work by typing his password and logging back on. An extension of this feature is also known as Switch User.)

However, as of 2 August 2010, the latest version of this tool (2.52.0.0) also suffered from a crippling shortcoming: Only users with administrative privileges could use it. Hence, I will no longer elaborate on it. In fact, if you are using Windows 7 or Windows Server 2008, you are already in possession of the improved version of Shutdown.exe which should be more than enough to handle automatic shutdowns of your local machine. If you are using Windows XP, you still do not need to bicker with Shutdown.exe or PsShutdown.exe: You can create your own shutdown tool!

Creating our own shutdown tool

If you are using Windows XP or Windows Server 2003, you do not have to cope with the discrepancies of Shutdown.exe: You can create your own shutdown tool. If you are using Windows 7 however, you can still create your own shutdown tool, if you like. To do so, you use Windows Script Host (WSH), a Windows component that allows you to run scripts (= simple computer programs) that you yourself have created in JScript and VBScript.

Composing the scripts

Familiarize yourself with the icon of VBScript script files. These files have .VBS file name extension.

You don’t need to create an entire script by yourself: I have already written the scripts for you. Follow the following instructions to import them to your computer.

  1. Start Notepad. You should find it in the Start Menu, under Accessories.
  2. Look below and find the script for the task (shutdown, restart or log out) that you need. (Some tasks may have two scripts written for them. You can choose either of them.)
  3. Copy the entire script into clipboard. To do so, select the script with your mouse. Then, right-click on you selection and from the menu that appears, select Copy.
  4. Paste the entire script into Notepad. To do so, right-click somewhere in the Notepad and from the menu that appears, select Paste.
  5. Save the entire script under a name of your liking but with .vbsextension. If you do not know what I mean, follow these instructions carefully:
    1. In Notepad menu bar, click File, and then select Save As…
    2. Browse to the location in which you’d like to save the file. For example, Desktop.
    3. Click inside File Name field.
    4. Type a single quotation mark (“).
    5. Type a name for your script. Anything will do, but I suggest you type a suitable name.
    6. Type: .vbs (Do not forget the dot.)
    7. Type another quotation mark (“).
    8. Optional: Make sure that Encoding field contains either ANSI or Unicode. It should not contain UTF-8 or Unicode Big Endian.
    9. Press Save button.
  6. Prepare to test the script. To do so, close all your open applications and save any unsaved changes.

Your script should now be ready. If you have named it correctly, it should look like as displayed in the picture. Repeat the steps above to create other scripts that perform other tasks.

Executing the scripts

You can now execute these scripts as you execute normal software applications: Just double-click on them! Make sure you test your scripts.

If you wish, you can place script somewhere outside the immediate eyesight (such a folder in C:\Program Files) and then create a shortcut to those scripts and expose the shortcuts. By doing so, users which have a standard user account may not accidentally modify those files. However, the main advantage of doing so is that you can assign shiny appealing or contextually-meaningful icons to your shortcuts. Yes, appealing icons! Good look and beauty is one of the things that human looks for. You can even find a suitable free third-party icon for the scripts or make one yourself using IcoFX, a free software application. (But I am not going to elaborate on how.)

You might also like to ask other software to run these scripts. For example, I have a 3D rendering application that lets me run an application when the rendering is complete. Sometimes, rendering a piece of 3D work takes a long time, so it might be a good idea to ask this application to use these scripts to shutdown the system after the work is complete. That way, I can leave the system alone.

To execute these scripts from within other software however, you may encounter trouble: Unlike Windows shell, Windows Command Prompt and Windows PowerShell, third-party software may not support direct execution of programs which are not Windows executables, i.e. programs that do not have a .exe file extension. However, there is a workaround for this issue: You must tell such software to run Windows Script Host and pass the path and file name of your script to Windows Script Host. For example, if your shutdown scripts called Shutdown.vbs is saved in C:\Program Files\Admin Scripts, you can tell your third-party software to run this command:

“%systemroot%\system32\wscript.exe” “%ProgramFiles%\Admin Scripts\Shutdown.vbs”

Script codes

Shutdown

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Power-off via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(POWEROFF)
Next

Shutdown (Alternative)

This scripts does exactly what the one above does, only using a different code.

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Shutdown via script"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Shutdown()
Next

Restart

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Reboot via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(REBOOT)
Next

Restart (Alternative)

Like the script above, the following script also restarts the computer:

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Shutdown via script"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
Next

Log off

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, "Log-off via script"

Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const POWEROFF = 8

Const LOGOFF_FORCE = 4
Const SHUTDOWN_FORCE = 5
Const REBOOT_FORCE = 6
Const POWEROFF_FORCE = 12

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
 ObjOperatingSystem.Win32Shutdown(LOGOFF)
Next

Further reading

For more information about these scripts, see:

  1. “Shutting Down Computers and Logging Off Users”. Microsoft TechNet Scripting Center. Microsoft corporation.
  2. “Shutting Down a Computer”. Microsoft TechNet Scripting Center. Microsoft corporation.
  3. “Restarting a Computer”. Microsoft TechNet Scripting Center. Microsoft corporation.
  4. “Win32Shutdown Method of the Win32_OperatingSystem Class”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.
  5. “Reboot Method of the Win32_OperatingSystem Class”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.
  6. “Shutdown Method of the Win32_OperatingSystemClass”. Microsoft Developers Network (MSDN) Library. Microsoft corporation. 9 September 2010.

What’s next?

I promised to blog about Windows PowerShell cmdlets that allows system administrators to perform shutdown and restart. I will leave that to another blog post.

Advertisements

Posted on 16 September 2010, in Windows Administration and tagged , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: