I had the following problem : I needed to rerun a program from the sccm distribution share on the server. When you create a scheduled task with schtasks.exe there is no way to configure the working directory for the program to run. The update I wanted to run creates temporary files in the working directory. The permissions on the SCCM distribution share are read-only. So to run the update i needed a different working directory for the scheduled task. The only solution I found until now was create a temporary batch file in the C:\temp of the computer and create a scheduled task for that batch file.
So this scripts creates a temporary batch file on the remote computer and creates a scheduled task for it. Because the schedule is once and the start date is in the past it will be deleted once it has run.
#*************************************************************************** #** Script: Sched-Task.ps1 #** Version: 1.0 #** Created: 02/10/2011 10:09AM #** Author: Adriaan Westra #** E-mail: #** #** Purpose / Comments: #** Schedule a task on a remote computer #** #** #** Changelog : #** 02/06/2011 10:09AM : Initial version #** #*************************************************************************** # Clear the screen Clear-Host $Text = "\\asussrv01\smspkgd$\P00002A9\update.EXE /q" # text to write to file $computername = "asuspc01" # Computername to schedule task on $title = "ConfigUpdate" # Scheduled task name $user = "westphil\admin" # User for scheduled task $pass = "password" # password for scheduled task $schedule = "once" # schedule $start = "06:00:00" # Start time $date = "02/03/2011" # Start date $cmdfile = "runtask.cmd" # Command file name $batchfile = "\\" + $computername + "\c$\temp\" + $cmdfile # Complete path for command file #*************************************************************************** # Create a batch file on the remote computer in the temp directory set-content -path $Batchfile $text $program = "c:\temp\" + $cmdfile #*************************************************************************** # Create the command line to schedule a task $command = "schtasks.exe /create /s $computername /ru $user /rp $pass" $command = $command + " /sc $schedule /tn $title /tr $program /st $start /sd $date" #*************************************************************************** # Run the command line invoke-expression $command #*************************************************************************** # Create the command line to run the scheduled task now $command = "schtasks.exe /Run /s $computername /tn $title" #*************************************************************************** # Run the command line invoke-expression $command #*************************************************************************** # Exit the script Exit 0