Most people use scheduled tasks to run a program on a schedule. In fact, most people don't even use scheduled tasks. If they do, they use it to run defrag (if they're using an older version of Windows). However, scheduled tasks can be very powerful if used properly because programs can be run locally on the box using either system credentials or a specific user's credentials. While scheduling a task might not seem to be the best way to run code remotely, there's a little known feature that actually makes this work wonderfully: schtasks. This command line utility allows for programmatic manipulation of scheduled tasks. The kicker is that this command line utility can be used to manipulate scheduled tasks on a remote machine!
Therein lies the entire strategy of running code remotely. First of all, Microsoft has put together a surprisingly helpful set of examples. Check it out to familiarize yourself with the commands.
So, the strategy is this:
- Package the code to be run on the remote computer into something that can be run silently (i.e. does not require any input from the user). This may mean writing your batch file or perl script.
- Copy the package to the remote computer. Obviously, you'll need RW access to put the script on the remote computer. This can be done remotely (and even recursively) by mapping a drive to the destination and copying the files to the mapped drive.
- Use schtasks to create a new task to run once in the past.
- Use schtasks to run the new task now
- Use schtasks to delete the task (optional)
I use this script:
I call the the update script like this:
>deploy.bat myservers.txt
The argument is the name of a text file containing the names of the servers I want to push the updated file to.
If you wanted to run the batch file once then remove all traces, I use the following script. The only problem with this one is that you have to wait for your script to finish before you can delete the scheduled task and the script. This script let's you indicate when it's safe to go ahead with the deletion by querying the scheduled tasks list on the remote computer(s). It will show 'Running' in the status column while the script is running and 'Ready' when it has finished.
That's about it. Happy hacking!
No comments:
Post a Comment