Skip to content

Commit 6305e54

Browse files
committed
Initial ScheduledTasksChecker
See SYNOPSIS/DESCRIPTION
1 parent d19ded6 commit 6305e54

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
Function ScheduledTasksChecker {
2+
<#
3+
.SYNOPSIS
4+
ScheduledTasksChecker allows you to perform various actions on Windows scheduled tasks.
5+
6+
.DESCRIPTION
7+
The function will perform the specified action on the scheduled task, and it uses Job Cmdlets for job processing, modify as you wish.
8+
9+
.PARAMETER Action
10+
Mandatory - specifies the action to be performed on the scheduled task, it accepts one of the following strings: "Start", "Stop", "GetStatus", "Enable", "Disable", "GetInfo", "Export".
11+
.PARAMETER TaskName
12+
Mandatory - mandatory parameter that specifies the name of the scheduled task to be acted upon, it must be a non-null and non-empty string.
13+
.PARAMETER TaskPath
14+
NotMandatory - path of the scheduled task to be acted upon, if not provided, the task will be assumed to be in the root folder of the task scheduler.
15+
.PARAMETER ExportPath
16+
NotMandatory - optional parameter that is only used when the $Action is set to "Export", it specifies the path where the exported scheduled task should be saved.
17+
.PARAMETER AsJob
18+
NotMandatory - specifies whether the action should be executed as a background job or not, if specified, the action will be executed as a background job, allowing the script to continue executing while the job runs in the background.
19+
20+
.EXAMPLE
21+
$TaskName = "SilentCleanup"
22+
$TaskPath = "\Microsoft\Windows\DiskCleanup\"
23+
ScheduledTasksChecker -Action Start -TaskName $TaskName -TaskPath $TaskPath -AsJob
24+
ScheduledTasksChecker -Action Stop -TaskName $TaskName -TaskPath $TaskPath -AsJob
25+
ScheduledTasksChecker -Action GetStatus -TaskName $TaskName -TaskPath $TaskPath -AsJob
26+
ScheduledTasksChecker -Action Disable -TaskName $TaskName -TaskPath $TaskPath -AsJob
27+
ScheduledTasksChecker -Action Enable -TaskName $TaskName -TaskPath $TaskPath -AsJob
28+
ScheduledTasksChecker -Action GetInfo -TaskName $TaskName -TaskPath $TaskPath -AsJob
29+
ScheduledTasksChecker -Action Export -TaskName $TaskName -TaskPath $TaskPath -ExportPath "$env:USERPROFILE\Desktop"
30+
31+
.NOTES
32+
v1.0
33+
#>
34+
[CmdletBinding()]
35+
param (
36+
[Parameter(Mandatory = $true)]
37+
[ValidateSet("Start", "Stop", "GetStatus", "Enable", "Disable", "GetInfo", "Export")]
38+
[string]$Action,
39+
40+
[Parameter(Mandatory = $true)]
41+
[ValidateNotNullOrEmpty()]
42+
[string]$TaskName,
43+
44+
[Parameter(Mandatory = $false)]
45+
[ValidateNotNullOrEmpty()]
46+
[string]$TaskPath,
47+
48+
[Parameter(Mandatory = $false, ParameterSetName = "ExportTask")]
49+
[ValidateNotNullOrEmpty()]
50+
[string]$ExportPath,
51+
52+
[switch]$AsJob
53+
)
54+
BEGIN {
55+
$TaskAction = $null
56+
switch ($Action) {
57+
"Start" { $TaskAction = "Start-ScheduledTask" }
58+
"Stop" { $TaskAction = "Stop-ScheduledTask" }
59+
"GetStatus" { $TaskAction = "Get-ScheduledTask" }
60+
"Enable" { $TaskAction = "Enable-ScheduledTask" }
61+
"Disable" { $TaskAction = "Disable-ScheduledTask" }
62+
"GetInfo" { $TaskAction = "Get-ScheduledTaskInfo" }
63+
"Export" { $TaskAction = "Export-ScheduledTask" }
64+
}
65+
}
66+
PROCESS {
67+
if ($TaskPath) {
68+
$Task = "$TaskAction -TaskName $TaskName -TaskPath $TaskPath"
69+
}
70+
else {
71+
$Task = "$TaskAction -TaskName $TaskName"
72+
}
73+
if ($AsJob) {
74+
$SchTaskJob = Start-Job -ScriptBlock { Invoke-Expression $Using:Task } -Name "SchTaskJob"
75+
do {
76+
Write-Host "Waiting to complete the jobs..."
77+
$CheckTask = (Get-ScheduledTask -TaskName $TaskName -TaskPath $TaskPath)
78+
if (($CheckTask.State -ne "Ready") -and ($CheckTask.Result -ne "0x0")) {
79+
Write-Warning -Message "Task is still not in Ready state, nor Last Run Result is success!"
80+
}
81+
Start-Sleep -Seconds 10
82+
}
83+
until($($SchTaskJob.State) -eq "Completed")
84+
Write-Host "Job completed, continuing..."
85+
foreach ($Job in $SchTaskJob.ChildJobs) {
86+
Write-Host "Result on: $($SchTaskJob.Location)" -ForegroundColor Cyan
87+
Get-Job -Name "SchTaskJob" | Wait-Job | Receive-Job -Keep -Verbose
88+
}
89+
}
90+
else {
91+
Invoke-Expression $Task
92+
if ($Action -eq "Export") {
93+
$Task = Get-ScheduledTask -TaskName $TaskName
94+
$Task | Export-ScheduledTask -Verbose | Out-File -FilePath "$($ExportPath)\$($TaskName).xml" -Verbose
95+
}
96+
}
97+
}
98+
END {
99+
Write-Host "Finishing, output of Scheduled Task($TaskName):..."
100+
if ($AsJob) {
101+
Get-Job -Name * -Verbose | Remove-Job -Force -ErrorAction SilentlyContinue
102+
}
103+
Write-Output -InputObject $CheckTask | Select-Object *
104+
}
105+
}

0 commit comments

Comments
 (0)