Posted by: Colin Smith
Microsoft Windows, Powershell
I have a couple Powershell Scripts that I have written that do something useful for me, the problem is that these are useful to me because I am asleep when I usually want them to run. I thought it would be a good Idea to write another script that would put these into the Windows Task Scheduler for me. This way I do not have to go log into the server and schedule them manually every time I want them to run. Also I have the ability to run Powershell on my PDA, so it will be nice to be able to do this from anywhere using my phone.
I dug into this and found that I certainly could use Powershell and WMI to look at jobs, create new jobs, modify jobs, and delete jobs. I thought this is great and I started coding. First I just wanted to pull a list of the jobs that are currently set up. When I did this I got no results. I started looking into this and found that Powershell cannot see jobs that were created with the Windows GUI and that the GUI can not see jobs that have been created or modified by Powershell. This is not cool and I am still not happy with this. I did, however find a way to complete my task.
Microsoft has another tool called schtasks.exe that will do the trick for us. If you need some more information on how to use this tool check out this TechNet Article about it. I decided that I would use this in my Powershell scripts to get my tasks set up. Here is what I did:
schtasks /delete /s pni-vmdbasql /TN “Job Name”
$st = Read-Host “`nWhat time, in 24:00:00 format, would you like to disable monitoring?”
$sd = Read-Host “`nWhat Date would you like to run this? mm\dd\yyy format please”
schtasks /create /TN “Job Name” /S remote-servername /RU domain\username /RP user password /SC Once /ST $st /SD $sd /TR Job to run
schtasks /query /S Remote-servername
That is really all you need to do in order to set up a job to run once at a given time. After I complete setting the job up I query the tasks in order to verify that what I have just done is actually set up. Hope this will help someone out. If you have any questions or comments please let me know by leaving a comment here or check out my site at sysadminsmith.com.