Launching Applications and Scripts at Startup


If you have one or more programs that you start as soon as Windows XP fires up, you can save yourself the hassle of launching these programs manually by getting Windows XP to do it for you automatically at startup. Similarly, you can direct Windows XP to automatically launch scripts or batch files at startup. To get these applications or scripts started automatically when Windows XP starts up, you can use the Startup folder, the registry, or the Group Policy snap-in.

Using the Startup Folder

The Startup folder is a regular file folder, but Windows XP treats the contents of this folder uniquely: You can enjoy automatic startup for your desired program or script simply by adding a shortcut for that item to the Startup folder. (Adding shortcuts to the Startup folder is one of the Start menu customizations that is discussed in more detail in Chapter , “Customizing the Interface.”) Note that the Startup folder appears twice in the Windows XP interface:

  • Via the Start menu (click Start, select All Programs, Startup; or left- click Start, select Open).

  • Via Windows Explorer as a subfolder in d:\Documents and Settings (where d is the drive on which Windows XP is installed). Actually, three different subfolders are available here:

    • \user\Start Menu\Programs\Startup ere, user is the name of a user defined on the system (see Chapter , “Managing Logons and Users”). A shortcut placed in this folder will run automatically when this user logs on to the system.

    • \All Users\Start Menu\Programs\Startup A shortcut placed in this folder will run automatically when any user logs on to the system.

    • \Default User\Start Menu\Programs\Startup A shortcut placed in this folder will be copied automatically to a user’s Startup folder whenever you create a new user account. (Note that the Default User subfolder is hidden by default. To display it, select Tools, Folder Options in Windows Explorer, select the View tab, and then select the Show Hidden Files And Folders option.)

Note that only users with administrator-level rights can access all three of these subfolders. Users with lesser privileges can work only with their own Startup folders. (They can see the All Users version of the Startup folder, but Windows XP prevents them from adding files to it.)

Later in this chapter, we’ll show you how to use the System Configuration Utility to control the launching of individual Startup folder items. For now, though, let’s mention that you can prevent all of the Startup items from running by holding down the Shift key while Windows XP loads user-specific files (hold down Shift after logging on).

Using the Registry

The Startup folder method has two drawbacks: Users can easily delete shortcuts from their own Startup folders, and users can bypass Startup items by holding down the Shift key while Windows XP loads. You can avoid both issues by using the Registry Editor (regedit.exe) instead. (We cover the Registry Editor in Chapter , “Getting the Most Out of the Registry.”) Assuming you’re logged on as the user you want to work with, the registry offers two keys:

  • HKCU\Software\Microsoft\Windows\CurrentVersion\Run The values in this key run automatically each time the user logs on.

  • HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce The values in this key run only the next time the user logs on, and are then deleted from the key. (Note that this key may not be present in your registry. In that case, you need to add this key yourself.)

    If you want an item to run at startup no matter who logs on, use the following keys:

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run The values in this key run automatically each time any user logs on.

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce The values in this key run only the next time any user logs on, and are then deleted from the key. (Don’t confuse this key with the RunOnceEx key. This is an “extended” version of RunOnce that’s used by developers to create more robust startup items that include features such as error handling and improved performance.)

To create a startup item, add a string value to the appropriate key, give it whatever name you like, and then set its value to the full path of the executable file or script file that you want to launch at startup.

Caution

Placing the same startup item in both the HKCU and the HKLM hives will result in that item being started twice, once at initial boot and again at logon.

Insider Secret

If the program is in the %SystemRoot% folder, you can get away with entering only the name of the executable file. Also, if the program you want to run at startup is capable of running in the background, you can load it in this mode by appending /background after the path.

Using Group Policies

If you prefer not to edit the registry directly, or if you prefer to place a graphical user interface (GUI) between you and the registry, the Group Policy snap-in can help. (See Chapter , “Mastering Control Panel, Policies, and PowerToys,” for details on using this snap-in.) Note, however, that Group Policy doesn’t work directly with the Run keys in the HKLM and HKCU hives. Instead, those Run keys are considered to be legacy keys, meaning that they’re used mainly by older programs. The new keys (new as of Windows 2000, that is) are the following:

  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer\Run

  • HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run

Note that these keys do not appear in Windows XP by default. You see them only after you specify startup programs in the Group Policy Editor, as discussed in the next section. (Alternatively, you can add these keys yourself using the Registry Editor.)

Insider_Secret

The startup items run in the following order:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\Explorer\Run
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce
Startup folder (all users)
Startup folder (current user)

Adding Programs to the Run Keys

As mentioned, you can add values to these keys either via the Registry Editor or via the Group Policy snap-in. In the Group Policy Editor, you have two choices:

  • To work with startup programs for all users, select Computer Configuration, Administrative Templates, System, Logon. The items here will affect the registry keys in the HKLM (all users) registry hive.

  • To work with startup programs for the current user, select User Configuration, Administrative Templates, System, Logon. The items here will affect the registry keys in the HKCU (current user) hive.

    Either way you’ll see at least the following three items:

  • Run These Programs At User Logon Use this item to add or remove startup programs using the \Policies\Explorer\Run keys in the registry. To add a program, double-click the item, select the Enabled option, and then click Show. In the Show Contents dialog box, click Add, enter the full path of the program or script you want to run at startup, and then click OK.

  • Do Not Process The Run Once List Use this item to toggle whether Windows XP processes the RunOnce registry keys (which we discussed in the previous section). Double-click this item and then select the Enabled option to put this policy into effect (that is, programs listed in the RunOnce key are not launched at startup).

  • Do Not Process The Legacy Run List Use this item to toggle whether Windows XP processes the legacy Run key. Double-click this item and then select the Enabled option to put this policy into effect (that is, programs listed in the legacy Run key are not launched at startup).

Specifying Startup and Logon Scripts

You also can use the Group Policy snap-in to specify script files to run at startup or at logon. You can specify script files at two places:

  • Computer Configuration, Windows Settings, Scripts (Startup/ Shutdown) Use the Startup item to specify one or more script files to run each time the computer starts (and before the user logs on). Note that by default, if you specify two or more scripts, Windows XP runs them synchronously. That is, Windows XP runs the first script, waits for it to finish, runs the second script, waits for it to finish, and so on.

  • User Configuration, Windows Settings, Scripts (Logon/ Logoff) Use the Logon item to specify one or more script files to run each time any user logs on. By default, logon scripts are run asynchronously.

    Finally, note that Windows XP has policies that dictate how these scripts run. For example, you can see the startup script policies by selecting Computer Configuration, Administrative Templates, System, Scripts. Three items reside there that affect logon and startup scripts:

  • Run Logon Scripts Synchronously If you select this item, Windows XP runs the logon scripts at the same time that it loads the user interface.

  • Run Startup Scripts Asynchronously If you select this item, Windows XP runs the startup scripts at the same time.

  • Run Startup Scripts Visible If you select this item, Windows XP makes the startup script commands visible to the user in a command window.

    Caution

    Logon scripts are supposed to execute before the Windows XP interface is displayed to the user. However, Windows XP’s new Fast Logon Optimization can interfere with that by displaying the interface before all the scripts are done. (The Fast Logon Optimization feature runs both the computer logon scripts and the user logon scripts asynchronously, which greatly reduces the logon time since no script has to wait for another to finish.) To prevent this interference, select Computer Configuration, Administrative Templates, System, Logon and choose the Always Wait For The Network At Computer Startup And Logon setting.

Note that for logon scripts, a similar set of policies appears in the User Configuration, Administrative Templates, System, Scripts section. However, any setting for Run Logon Script Synchronously in Computer Configuration takes precedence over the value for that policy in User Configuration.

Note

Yet another way to set up a program or script to run at startup is to use the Scheduled Tasks folder (select Start, All Programs, Accessories, System Tools, Scheduled Tasks; select Scheduled Tasks from Control Panel; or use Windows Explorer to display the %SystemRoot%\Tasks folder). When you create a new task, two of the options you’ll see are:

  • When My Computer Starts Choose this option to run the program when your computer boots, no matter which user logs on. Note that only someone logged on under the Administrator account can select this option. (The tasks will run otherwise, but they won’t be displayed.)

  • When I Log On Choose this option to run the program only when the user logs on. This is the option to use for accounts other than Administrator.

Tip

Note that a bug in Windows XP prevents it from running a scheduled task if your user account doesn’t have a password. You need to assign a password to your account and then add the password to the scheduled task.

Some Startup Scripts

In this section, we’ll take you through a few sample scripts that you can implement to improve your Windows XP startups.

Adding a Network Printer

One of the hassles faced by mobile users is the need to access resources when they return to the office and connect to the network. For example, the user may have to connect to a network printer. If you face this or a similar chore, you can take some of the drudgery out of it by using a script that automates the process. Here’s some VBScript code that asks the user whether he or she wants to connect to the network printer. (We discuss scripting in Chapter , “Programming Windows XP with Scripts.”) If the user clicks Yes in the popup, the script connects the printer and sets it as the default.

Option Explicit Dim wshShell, wshNetwork, strPrinterName, strDriverName ‘ First ask the user if he or she wants to connect the printer Set wshShell=CreateObject("WScript.Shell") If wshShell.Popup("Do you want to connect to the network printer?", ,  Logon", vbYesNo + vbQuestion) = vbYes Then          ’ If so, use the Network object to connect to the network printer     Set wshNetwork = CreateObject("WScript.Network")     ’ Store the UNC path and driver     strPrinterName = "\\ComputerName\PrinterName"     strDriverName = "DriverName"          ’ Connect the printer and set it to the default     wshNetwork.AddWindowsPrinterConnection strPrinterName, strDriverName     wshNetwork.SetDefaultPrinter strPrinterName  End If

Note that when you’re setting the strPrinterName value, you need to change ComputerName to the name of the network computer that hosts the printer, and you need to change PrinterName to the shared named of the printer. Similarly, for the strDriverName value, change DriverName to the name of the printer driver.

Synchronizing the System Time

Computer clocks are often unreliable beasts, sometimes gaining or losing minutes in a day. To keep your system time accurate, create a startup item that synchronizes your machine’s time with the time on a server.

If your computer is part of an Active Directory (AD) domain, the Windows Time Service will automatically synchronize your system time with the time of the domain controller. If your computer is part of a non-AD domain or a workgroup, and the network has a computer that keeps accurate time, include the following line in a startup batch file and replace ComputerName with the name of the network computer that keeps the accurate time:

NET TIME //ComputerName /SET

The NET TIME command used with the /SET switch synchronizes your system time with the system time on the computer specified by ComputerName.

How do you ensure your network has a computer that holds accurate time? The best way is to synchronize the computer with an external source, such as an Internet host that offers public access to a time server. Windows XP should already be set up to do this. (To check, open Control Panel’s Date And Time icon, select the Internet Time tab, and make sure the Automatically Synchronize With An Internet Time Server check box is selected.) To sync up with another external time server, open the Windows XP command prompt and enter the following two commands:

W32TM /CONFIG /MANUALPEERLIST:peerlist /SYNCFROMFLAGS:MANUAL
W32TM /CONFIG /UPDATE

W32TM

This is the command-line version of the Windows Time service, which synchronizes the system date and time.

/CONFIG

This switch specifies that the time service is being reconfigured.

/MANUALPEERLIST:peerlist 

This switch specifies the location of a time server. Replace peerlist with the Domain Name System (DNS) name of the time server. You can also use the Internet Protocol (IP) address, but these addresses often change, so the DNS name is better.

SYNCFROMFLAGS:MANUAL

This switch tells the time service to synchronize the time, based on the server named in the peerlist.

UPDATE

This switch tells the time service to put the new configuration into effect.

For example, you can synchronize with the National Institute of Standards and Technology time server—time.nist.gov—by running the following commands:

W32TM /CONFIG /MANUALPEERLIST:time.nist.gov /SYNCFROMFLAGS:MANUAL
W32TM /CONFIG /UPDATE

You can place these commands in a startup batch file to ensure your computer is always synchronized with the external server. The addresses of other time servers can be found at the following sites:

http://www.boulder.nist.gov/timefreq/service/time-servers.html

http://www.ntp.org/

Configuring Services to Display an Alert When They Fail to Start

Most Windows XP services will display an alert if they fail to start, but not all do. Here’s a script that uses Windows Management Instrumentation (WMI) to get a list of the services that don’t display failure alerts and reconfigures them so that they will display an alert:

Option Explicit Dim objWMIService, objService, colServiceList Dim strServices, errReturn Const NORMAL_ERROR_CONTROL = 2 ‘ Connect to WMI on the local computer Set objWMIService=GetObject(“winmgmts:{impersonationLevel=impersonate}_     ” & “!\\.\root\cimv2”) ‘ Query WMI to get the collection of services  ‘ that don’t issue alerts when they fail to start Set colServiceList = objWMIService.ExecQuery _     (“Select * from Win32_Service where ErrorControl = ’Ignore’”) ‘ Run through the collection of services strServices = “" For Each objService in colServiceList          ’ Configure the service to issue an alert if it fails to start     errReturn = objService.Change( , , , NORMAL_ERROR_CONTROL)     strServices = strServices & objService.DisplayName & vbCrLf Next ‘ Display the list of services that were changed WScript.Echo “Alerts activated for the following services:” & _ vbCrLf & vbCrLf & strServices




Insider Power Techniques for Microsoft Windows XP
Insider Power Techniques for Microsoft Windows XP (Bpg-Other)
ISBN: 0735618968
EAN: 2147483647
Year: 2005
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net