SchedulerTrigger.ps1
<#
.DESCRIPTION Script capable to trigger remote Scheduled Tasks and to get the last time execution of them .PARAMETER Taskname Name of the Scheduled Task .PARAMETER DClist Path to the file that contains the list of computers from where to get the Scheduled tasks info. .PARAMETER DomainControllers Determines that the list of computers is the Dommain Controllers from the current Domain .PARAMETER Check Checks the executime time of the scheduled taks in the last 24h .PARAMETER Execute Executes the the scheduled taks in all remote servers .EXAMPLE .\SchedulerTrigger.ps1 -Taskname "Sync DSRM Password" -DClist DClist.txt -Check Returns the time of the execution (in last 24h) of task "Sync DSRM Password", in all Servers included in DClist.txt file .EXAMPLE .\SchedulerTrigger.ps1 -Taskname "Sync DSRM Password" -DClist DClist.txt -Execute Execute task "Sync DSRM Password" in all Servers included in DClist.txt file .EXAMPLE .\SchedulerTrigger.ps1 -Taskname "Sync DSRM Password" -DomainControllers -Execute Execute task "Sync DSRM Password" in all Domain Controller in current domain #> <#PSScriptInfo .VERSION 0.11 .GUID 328814b7-ab50-495c-9bcb-35c90d3117bd .AUTHOR Raul Carboneras racarb@microsoft.com .COMPANYNAME Microsoft .COPYRIGHT Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. See LICENSE in the project root for license information. .TAGS Remote Scheduler Trigger Checker .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> Param ( [CmdletBinding(DefaultParameterSetName = 'DomainControllersCheck')] [Parameter(Mandatory = $true)] [string]$Taskname = "Sync DSRM Password", [Parameter(Mandatory = $true, ParameterSetName = 'DClistCheck')] [Parameter(Mandatory = $true, ParameterSetName = 'DClistExecute')] [string]$DClist, [Parameter(Mandatory = $true, ParameterSetName = 'DomainControllersExecute')] [Parameter(Mandatory = $true, ParameterSetName = 'DomainControllersCheck')] [switch]$DomainControllers, [Parameter(Mandatory = $true, ParameterSetName = 'DomainControllersCheck')] [Parameter(Mandatory = $true, ParameterSetName = 'DClistCheck')] [switch]$Check, [Parameter(Mandatory = $true, ParameterSetName = 'DomainControllersExecute')] [Parameter(Mandatory = $true, ParameterSetName = 'DClistExecute')] [switch]$Execute ) if ($DomainControllers) { $Computername = (Get-ADDomainController -Filter * -ErrorAction Stop).Hostname } elseif ($DClist) { $Computername = Get-Content -Path $DClist -ErrorAction stop } If ($PSCmdlet.ParameterSetName -match "Check") { #Check the execution of the task $resultsRAW = Invoke-Command -ComputerName $Computername { try { $script:task = Get-ScheduledTask -TaskName $using:taskname -ErrorAction Stop } catch { $script:task = "NotFound" } New-Object PSObject -Property @{ Computer = $Env:COMPUTERNAME Task = $task Last24hExecution = Get-WinEvent -FilterHashtable @{ Logname = "Microsoft-Windows-TaskScheduler/Operational" id = 201 StartTime = (Get-Date).AddHours(-24) } | where { ($_.Properties.value)[0] -eq "\$($using:taskname)" } } } -ErrorAction SilentlyContinue -ErrorVariable RemoteErrors $RemoteErrors | Export-Clixml RemoteErrors.xml $resultsRAW | Select-Object task, Computer, @{N = "Timeexecuted"; E = { $_.Last24hExecution.TimeCreated } } } else { #Trigger Scheduled task Invoke-Command -ComputerName $Computername { Write-Host "Executing task $using:taskname on server $env:COMPUTERNAME ..." -ForegroundColor Green Try { Get-ScheduledTask -TaskName $using:taskname -ErrorAction Stop | Start-ScheduledTask -Verbose Write-Host "OK" -ForegroundColor Green } catch { $_.Exception } } } |