When you’re automating various Windows activities, sometimes it’s hard to know when you’re finished with one step and can move on to the next one. Case in point: I’m using a deployment product called Altiris to set up Windows on my servers. After Windows setup is complete, I need to make some configuration changes and install some software.
Unfortunately, Altiris tries to do this while Windows installation is still going on. As part of the installation, Altiris installs its agent software, and when the GUI portion of setup begins, this agent is loaded. This causes the rest of the process to fail.
While puzzling out how to work around this, I noticed that once Windows setup is finished but before the job completes, a number of programs are run automatically. Turns out there’s a section of the unattend.txt files called [GuiRunOnce] that specified command lines to be run when a user logs on to Windows for the first time, and if you’re using the AutoLogon parameter and a plain-text password, you can convince Windows to log on automatically.
Wouldn’t it be great (I said to myself) if there was a way I could run a batch file during this phase to create a small flag file on the root of the C: drive so that I could tell setup was complete? And it would have been easy, too, except that the last command rebooted the server. I wanted to create this flag file after the last reboot. As the Grinch said, “But how?”
As it happens it’s pretty simple. There’s a command-line tool called schtasks.exe that allows you to create scheduled tasks, including tasks that run at system start. Just use this syntax:
schtasks /create /tn "TaskName" /tr <task command line> /sc ONSTART /ru system
I hope everything except the /ru parameter should be self-explanatory. That paramter specifies the user to run as, and by using the reserved word “system” you instruct the task to run as the system account.
So I added this command to the [GuiRunOnce] command and added a batch file called SetDoneFlag.bat to my Windows setup files (this is left as an exercise for the reader). I don’t want that batch file run every time the system boots, so I just add this command as the last line:
schtasks /delete /tn "TaskName"
And voila! Now I can check for the existence of C:\DoneFlag.txt, and if it’s there, I know that Windows setup is complete, and I can continue on with the configuration.