How to create a scheduled task using PowerShell

February 21, 2008

I found this insanely easy, thanks to some well written Microsoft documentation (shocking I know). If you need to create a scheduled task of an application in your PowerShell script you can call the Schtasks.exe application. Below is an example I used to create a scheduled task to run my exe every day at 1:00a.m. using the System account:

Write-Host “Creating Account Collector Scheduled Task”

Schtasks /create /sc DAILY /tn AccountCollector /tr “`”`”`”`”C:\Program Files\AMF\AMFCollector.exe`”`”`”`”” /s devsbvm01 /ru System /st 01:00:00

Write-Host “Scheduled Task Created”

That’s it, works painlessly on a local or remote server, which in my example, devsbvm01 is my remote server. Now, you might be wondering “whats with all the quotes?”. Well, I struggled for a long time trying to figure out why the scheduled task would be created but would never run. Turns out that the qoutes were being stripped out, thus causing the path to break. I know its sleazy, but turns out you need four sets of quotes in order to keep them from being stripped out! If anyone ever finds a better way, let me know but this seems to be it for now! So make sure you put those quotes in there!

Here is the link to the Microsoft tech net article that explains in great detail the Schtasks.exe application.



How to Install an Application on a Remote Machine Using PowerShell

January 16, 2008

Although this seems as it would be a common tasks for administrators and developers alike, I found very little documentation on the Internet on how to install an msi or exe on a remote machine using PowerShell. I even found comments saying it couldn’t be done at all! Bull Hugly, it can be done! I have provided two pieces of code that you can use to install either an msi package or exe on a remote server using PowerShell.

Installing an MSI

Using a short hand of WMI in PowerShell , you can execute the following code to install your msi package:

$box=”deviis01” #this is the name of your server

$product= [WMICLASS]”\\$box\ROOT\CIMV2:win32_Product”

Write-Host “Installing software on $box”

$product.Install(“c:\Setup \somesoftwarepackage.msi”)

The best part is it will install it silently so you don’t have to worry about command arguments to do that.

Installing an EXE

The exe was more of a pain and I wasn’t able to get it to work with the Install method. If someone else is able to get it to work, please post a comment to share with the rest of us because I would like to know if it is possible, however, here is what you can do to install your exe.

$box=”deviis01” #this is the name of your server

Write-Host “Installing software on $box”

([WMICLASS]”\\$box\ROOT\CIMV2:win32_process”).Create(“cmd.exe /c c:\Setup\somesoftware.exe /s /v`” /qn”)

This actually works by creating a new process on the remote server, calling the cmd.exe and passing it the arguments for installing your program. One thing to make note of is all the command switches I have at the end. You will want to check your exe to see what switches it takes; for example mine above is using an exe created with InstallShield so the command line options might be different, so just keep that in mind.