Solved

how can i monitor scheduled tasks on remote windows servers?

Posted on 2014-09-11
2
319 Views
Last Modified: 2014-09-29
Hello Experts.

how can I monitor scheduled tasks on remote windows servers?

I have a number of requirements as to the data I need to yield

Scheduled tasks where DaysOfTheWeek = Yesterday
Date/time stamp from last schedule execution
Status/Exit code from last schedule execution

Thank you for your help
0
Comment
Question by:Chris Portsmouth
2 Comments
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40317107
I think you can give this a shot. I have modified it to pull only Name of Task, Computer Name, Last Run Result and Last Run Time but only for yesterday.

The function is by Twon of An http://community.spiceworks.com/scripts/show/2094-get-scheduled-tasks-from-a-computer-remote-or-local

<#
.SYNOPSIS
	Get-ScheduledTasks will retrieve a list of scheduled tasks.
.DESCRIPTION
	Get-ScheduledTasks gets an exhaustive list of all scheduled tasks on the specified computer. It accomplishes this by created a schedule object and having that query the schedule service of the desired computer.
.PARAMETER ComputerName
	The name of the computer that you would like to gather information about. Defaults to the localhost.
.PARAMETER RootOnly
	When specified, the cmdlet will retrieve just the top level tasks. Usually the user specified ones.
.EXAMPLE
	Get-ScheduledTasks DC1 -RootOnly
	
	Actions        : WScript.exe C:\scripts\MyScript.vbs
	Path           : \Run My Script
	Enabled        : True
	Triggers       : CalendarTrigger
	Description    : The Scheduled task that runs my script.
	Author         : DOMAIN\User
	Name           : Run My Script
	NextRunTime    : 9/1/2013 3:00:00 AM
	LastRunTime    : 8/1/2013 3:00:00 AM
	LastTaskResult : 267014
.EXAMPLE
	Get-ADComputer -filter 'name -like "Workstation1*"' | select name | Get-ScheduledTasks
	
	This will return a long list of scheduled tasks from every computer in AD that meets the filter.
.INPUTS
.OUTPUTS
	Custom PSObject with properties: ComputerName,Actions,Path,Enabled,Triggers,Description,Author,Name,NextRunTime,LastRunTime,LastTaskResult
.NOTES
	Author: Twon of An
.LINK
	Schedule.Service
#>
Function Get-ScheduledTasks
{
	Param
	(
		[Alias("Computer","ComputerName")]
		[Parameter(Position=1,ValuefromPipeline=$true,ValuefromPipelineByPropertyName=$true)]
		[string[]]$Name = $env:COMPUTERNAME
		,
		[switch]$RootOnly = $false
	)
	Begin
	{
		$tasks = @()
		$schedule = New-Object -ComObject "Schedule.Service"
	}
	Process
	{
		Function Get-Tasks
		{
			Param($path)
			$out = @()
			$schedule.GetFolder($path).GetTasks(0) | % {
				$xml = [xml]$_.xml
				$out += New-Object psobject -Property @{
					"ComputerName" = $Computer
					"Name" = $_.Name
					"Path" = $_.Path
					"LastRunTime" = $_.LastRunTime
					"NextRunTime" = $_.NextRunTime
					"Actions" = ($xml.Task.Actions.Exec | % { "$($_.Command) $($_.Arguments)" }) -join "`n"
					"Triggers" = $(If($xml.task.triggers){ForEach($task in ($xml.task.triggers | gm | Where{$_.membertype -eq "Property"})){$xml.task.triggers.$($task.name)}})
					"Enabled" = $xml.task.settings.enabled
					"Author" = $xml.task.RegistrationInfo.Author
					"Description" = $xml.task.registrationInfo.Description
					"LastTaskResult" = $_.LastTaskResult
					"RunAs" = $xml.task.principals.principal.userid
				}
			}
			If(!$RootOnly)
			{
				$schedule.GetFolder($path).GetFolders(0) | % {
					$out += get-Tasks($_.Path)
				}
			}
			$out
		}
		ForEach($Computer in $Name)
		{
			If(Test-Connection $computer -count 1 -quiet)
			{
                Write-Error "`n$Computer`n"
				$schedule.connect($Computer)
				$tasks += Get-Tasks "\"
			}
			Else
			{
				Write-Error "Cannot connect to $Computer. Please check it's network connectivity."
				Break
			}
			$tasks
		}
	}
	End
	{
		[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
		Remove-Variable schedule
	}
}

# Sets the location for the Csv Report
$CsvReport = "C:\ScheduledTasksReport.csv"


(Get-ADComputer -Filter {Enabled -eq $True -and Operatingsystem -like "*server*"}).name | Get-ScheduledTasks -RootOnly | Select-Object Name,ComputerName,LastRunTime,LastTaskResult | Where-Object { ($_.LastRunTime -ge (get-date).AddDays(-1)) }| Export-CSV $CsvReport -NoTypeInformation

Open in new window

0
 

Author Closing Comment

by:Chris Portsmouth
ID: 40349673
solution provided solved my issue
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question