May 30, 2009

Powershell Scheduled Task or Windows Service

I have many powershell scripts that do reporting, alerting, or even gathering of statistical data. I needed a way to have those scripts running either all the time or at intervals. Here is a simple way to do this in powershell:

Scheduled Task:
  • Create the Task and the Time
  • Run as Administrator
  • powershell.exe -nologo -command "&{c:\MyTask.ps1}"
One note is that powershell.exe must be in your environment variables. If not, then just put the full path in to powershell.exe (C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

Run as a Windows Service
  • Download RunAsService
  • Run through the basic installation and set up the user to run the service (or local if that is all that is needed)
  • In the .config used by RunAsService
<service.settings>
<service>
<name>POSh Service</name>
<executable>powershell</executable>
<parameters>-file "C:\MyPOShServices\runmon.ps1"</parameters>
</service>
</service.settings>
</pre>
The only way I know how to do this currently is with some open source. Please let me know if there is another way.

No comments:

Post a Comment