Managing Scheduled Tasks Remotely Using Powershell

The following Powershell functions allow you to manage querying, creating and removing scheduled tasks on one or more computers remotely.
The functions use schtasks.exe, which is included in Windows. Unlike the Win32_ScheduledJob WMI class, the schtasks online crestor.exe commandline tool will show manually created tasks, as well as script-created ones. The examples show some, but not all parameters in action. I think the parameter names are descriptive enough to figure it out, really. If not, take a look at schtasks.exe /?. One tip: try piping a list of computer names to foreach-object and into this function.


Enjoy.
Hugo

Function Get-ScheduledTask
	{
	param([string]$ComputerName = "localhost")
	Write-Host "Computer: $ComputerName"
	$Command = "schtasks.exe /query /s $ComputerName"
	Invoke-Expression $Command
	Clear-Variable Command -ErrorAction SilentlyContinue
	Write-Host "`n"
	}
	
# EXAMPLE: Get-ScheduledTask -ComputerName Server01
	
Function Remove-ScheduledTask
	{
	param(
	[string]$ComputerName = "localhost",
	[string]$TaskName = "blank"
	)
	If ((Get-ScheduledTask -ComputerName $ComputerName) -match $TaskName)
		{
		If ((Read-Host "Are you sure you want to remove task $TaskName from $ComputerName(y/n)") -eq "y")
			{
			$Command = "schtasks.exe /delete /s $ComputerName /tn $TaskName /F"
			Invoke-Expression $Command
			Clear-Variable Command -ErrorAction SilentlyContinue
			}
		}
	Else
		{
		Write-Warning "Task $TaskName not found on $ComputerName"
		}
	}
	
# EXAMPLE: Remove-ScheduledTask -ComputerName Server01 -TaskName MyTask
	
Function Create-ScheduledTask
	{
	param(
	[string]$ComputerName = "localhost",
	[string]$RunAsUser = "System",
	[string]$TaskName = "MyTask",
	[string]$TaskRun = '"C:\Program Files\Scripts\Script.vbs"',
	[string]$Schedule = "Monthly",
	[string]$Modifier = "second",
	[string]$Days = "SUN",
	[string]$Months = '"MAR,JUN,SEP,DEC"',
	[string]$StartTime = "13:00",
	[string]$EndTime = "17:00",
	[string]$Interval = "60"	
	)
	Write-Host "Computer: $ComputerName"
	$Command = "schtasks.exe /create /s $ComputerName /ru $RunAsUser /tn $TaskName /tr $TaskRun /sc $Schedule /mo $Modifier /d $Days /m $Months /st $StartTime /et $EndTime /ri $Interval /F"
	Invoke-Expression $Command
	Clear-Variable Command -ErrorAction SilentlyContinue
	Write-Host "`n"
	}
	
# EXAMPLE: Create-ScheduledTask -ComputerName MyServer -TaskName MyTask02 -TaskRun "D:\scripts\script2.vbs"
And sponsors.Yet in just a few weeks, the leaguewill go pink in the name of breast cancer awareness. I shimmy them around, then I use my fingers, push them up nice and tight. Fix the team chemistry: Arena said he doesn plan to make wholesale changes in the roster or the team style of play. It focuses on only the darker themes of Carroll’s work, and also all your favorite Alice characters and settings now look like this:. They ARE kids. That type of percentage is badcircle the wagons, watch your back type of bad. We had an opportunity to stop them on their side of the field and force a punt and kick a field goal to win, and we didn’t do that.”Cardinals get wild OT win over Packers to reach NFC title gameLast year’s NFC title game ended in similar fashion, with the Seattle Seahawks winning the coin http://www.fakeoakleysaler.com toss and driving for the winning score while Packers quarterback Aaron Rodgers who rallied the Packers with two desperation heaves late in regulation Saturday watched from the sideline.The NFL tweaked the rule prior to the 2012 season, moving from a strictly sudden death format to a system cheap oakleys Sunglasses Online in which both teams got the opportunity to possess the football unless the first team scored a touchdown. There are usually regulations cheap oakleys on different types of lifts and stunts simply for safety sake.. The Canadians were able to hold the Germans back until British cheap nfl jerseys reinforcements arrived. “You can do great things regardless of your age if Cheap Oakleys Sale you just believe and, you know, go for it,” she told ESPN at the time. His play speed is in the 4.45 4.48 area. Only eight teams that have been around since 1974 have yet to finish a game tied: Buffalo, Chicago, Dallas, New England, New Orleans, Oakland, San Diego and Tennessee.. Wouldn’t everybody like to know how to do that. So going down, reach, good. The veteran safety called Sunday game against the Seahawks a Super Bowl matchup earlier this week. Prescott is making the choice harder, week by week. The morning of the day when a vaginal plug is seen is designated as embryonic day 0.5 (E0.5). Tradition has been a mainstay for the Browns but then again, the Super Bowl has eluded them for their whole existence so maybe an update to the uniform is due.. cheap football jerseys This craft can be modified to fit any decor or style. He’s also inventive and cunning and http://www.cheapjerseys11.com not encumbered by the smallest wisp of ethics. Into play!online casinos any diseases, donated any money, saved any lives, or even sacrificed her own life for the greater good of anything!. Made my step mother nauseous being in the restaurant (a highlight of the trip for me!).. It’s all over now.

14 thoughts on “Managing Scheduled Tasks Remotely Using Powershell”

  1. Pingback: Getting Scheduled Tasks in Powershell « PowerShell Station
  2. Pingback: Bookmarks for August 18th from 09:22 to 16:00 | The Wahoffs.com
  3. This was very helpfull. I added a filter, that converts the returned data to objects:
    filter ConvertFrom-CmdList {

    $_ | foreach {if ($_ -match ‘^$’){ $newobj = New-Object Object
    $obj | foreach {$newobj | Add-Member NoteProperty $($_ -split ‘:’)[0] “$($_ -replace ‘^.*:[ ]+’)”}
    $newobj
    $obj = @()}
    if ($_ -notmatch ‘^$’){$obj += $_ }
    }
    }

    Function Get-ScheduledTask {

    param([string]$ComputerName = “localhost”)
    Write-Host “Computer: $ComputerName”
    schtasks.exe /query /s $ComputerName /FO List | ConvertFrom-CmdList
    }

  4. If you want prettier output from schtasks /query, you can easily convert it to an object:

    $Tasks = schtasks.exe /QUERY /V /FO:CSV /S:$ComputerName | ConvertFrom-Csv

    /V Gives you much more info
    /FO:CSV formats the output into csv so you can convert it right into an array of task objects. I tend to throw in a where statement too to get rid of the artifacts and built in tasks.

    $Tasks = schtasks.exe /QUERY /V /FO:CSV /S:$ComputerName | ConvertFrom-Csv | Where-object {$_.taskName -notlike '\Windows*' -and $_.taskname -notlike 'taskname'}

  5. Hi,
    I would like to know who invoked the scheduled task.

    I user one PS script to invoke an already configured scheduled task that is set to run with high privileges. As part of its execution, the scheduled tasks writes output to htmm file. Is there anyway we can get who has invoked the scheduled task using the firs PS script. I would like to add their name to the output of the scheduled task script.

  6. Hi, I am migrating schedule task from windows server 2003 to windows server 2008. One way is from Task scheduler GUI using export from and import to technique and then edit the arguments in Action. I am interested in knowing how to do it using a powershell script for automation using PS version 2.0. The version 3.0 has the cmdlets but i am not thinking of using it.

Leave a Reply