Tip: Get a Windows VM to run a batch file at boot

With the virt-win-reg tool built on top of libguestfs and hivex it’s now relatively straightforward to modify a Windows virtual machine so that it runs a batch file, script or program at next boot.

Note: The Windows VM must be shut down before you attempt this.

The plan is that we upload the batch script to some place in the VM, and then add a “RunOnce” key in the Windows Registry (explained in this MSDN article and this article). First let’s just take a look at what’s in the key. In most cases it will be empty:

# virt-win-reg Windows7x64 \

Now we’ll prepare our batch file and upload it:

# cat test.bat
# guestfish -i Windows7x64

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for a list of commands
      'man' to read the manual
      'quit' to quit the shell

><fs> upload test.bat /test.bat
><fs> ^D

And finally we modify the RunOnce registry key:

# virt-win-reg --merge Windows7x64

One potential gotcha: You must be running hivex ≥ 1.2.2.

Now you can boot your Windows guest and check that the script runs after the user logs in. Look for the file C:\TEST.LOG:

><fs> cat /TEST.LOG

Because we’re using the RunOnce key, the script will run just one time. If you want it to run every time, use the Run key.

Now, how do we make the script work without the user needing to log in? (Clue: The answer is not RunServicesOnce — this does not work in Windows 7). What’s surprising (coming from a Linux background) is the huge amount of incomplete, contradictory and simply false information contained in MSDN about this topic.

About these ads


Filed under Uncategorized

8 responses to “Tip: Get a Windows VM to run a batch file at boot

  1. Yaniv

    Not sure if it would run if a non-admin would log in to the VM.

  2. Pingback: Tip: Install a service in a Windows VM « Richard WM Jones

  3. Yuval Kashtan

    Where have you seen false or contradictory information in the MSDN?
    One should read the MSDN very carefully, every word there is meaning full. sometimes the most important info (such as what applies to what OS) is listed in a table at the end.
    Reading the remarks is MOST important (but as I said you really need to read everything..)

    But IMHO, the amount of incorrect information there is very little. and usually someone would point that out in the comments.

  4. Pingback: Tip: Install a device driver in a Windows VM « Richard WM Jones

  5. Paolo Bonzini

    I agree that MSDN can be confusing, but I think I hardly ever found something _incorrect_ there.

    The main problem with RunOnce is that some services don’t allow you to access the console and only give access to Remote Desktop. If the RunOnce program is necessary to give you network access, for example, this won’t work. Installing a service using RHSrvAny could be a good idea for this.

    • rich

      Try finding out which Windows versions support RunServicesOnce, using only MSDN.

      • Morrison

        I second Rich on this. For information coming from the authoritative source, just on the point above – I at least expect to have KB pages that clearly state which (and ALL) versions registry settings apply to (Which I rarely see). As an example of the ambiguity, this (http://support.microsoft.com/kb/314488) page is about XP, but at the bottom it has “For more information about the Run and RunOnce registry keys, click the following article number to view the article in the Microsoft Knowledge Base:
        179365 INFO: Run, RunOnce, RunServices, RunServicesOnce and Startup “. On that page (http://support.microsoft.com/kb/179365) it states “Under Microsoft Windows 95, Windows 98, and Windows Millennium Edition (Me)…” (which seems to exclude XP+) only to go onto say “Because of different system configurations (such as a computer that is configured to automatically log on), any application that is dependant upon other applications that are executed under these keys having completed must be prepared to wait until these applications are complete. Other than this exception, the above description applies to Microsoft Windows NT 4.0, Windows 2000, and Windows XP. ” which seems to suggest that all the keys on the page (Including RunServices & RunServicesOnce) are also supported on NT4.0, 2000 and XP, but that ¿on NT4.0, 2000, and XP system design means that login can happen alongside the *Services* key command’s execution? (Despite the *Services* keys working for anything but 9x/ME being in contradiction to many other postings on different websites). Lastly, where is the comparable document of order of key loading for later Windows OS’s?

Leave a Reply

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