Solved

how can i monitor scheduled tasks on remote windows servers?

Posted on 2014-09-11
2
314 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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…
Note: This is the second blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   Every month t…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

786 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