-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisable-ScheduledTasks.ps1
More file actions
120 lines (102 loc) · 4.08 KB
/
Disable-ScheduledTasks.ps1
File metadata and controls
120 lines (102 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<#
.SYNOPSIS
Disable one or more scheduled tasks.
.DESCRIPTION
Disable-ScheduledTasks.ps1 accepts task names (pipeline or parameter), or a file with one task name per line.
Uses ShouldProcess so -WhatIf and -Confirm work. Outputs objects describing result.
.EXAMPLE
.\Disable-ScheduledTasks.ps1 -TaskName "MyTask","AnotherTask"
.EXAMPLE
"TaskA","TaskB" | .\Disable-ScheduledTasks.ps1
.EXAMPLE
.\Disable-ScheduledTasks.ps1 -TaskListFile C:\temp\tasks.txt -TaskPath "\MyFolder\"
#>
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param(
[Parameter(Position=0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[string[]]$TaskName,
[Parameter(Position=1)]
[string]$TaskPath = '\',
[Parameter()]
[ValidateScript({ Test-Path $_ -PathType Leaf })]
[string]$TaskListFile,
[switch]$Force
)
Begin {
function Assert-Admin {
if (-not ([bool]([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator))) {
Write-Warning "Not running elevated. Disabling scheduled tasks may fail without administrative privileges."
}
}
Assert-Admin
$names = @()
if ($TaskListFile) {
try {
$fileNames = Get-Content -Path $TaskListFile -ErrorAction Stop | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne '' }
$names += $fileNames
} catch {
Write-Error "Failed to read task list file '$TaskListFile': $_"
return
}
}
}
Process {
Write-Host "Running script: $($MyInvocation.MyCommand.Name)`n"
if ($TaskName) { $names += $TaskName }
foreach ($n in $names) {
if ([string]::IsNullOrWhiteSpace($n)) { continue }
# Get matching tasks (supports wildcards)
try {
$matches = Get-ScheduledTask -TaskName $n -TaskPath $TaskPath -ErrorAction Stop
} catch {
Write-Warning "No scheduled task found matching Name='$n' Path='$TaskPath'. ($_ )"
continue
}
foreach ($task in $matches) {
$target = "$($task.TaskName) (Path: $($task.TaskPath))"
if ($PSCmdlet.ShouldProcess($target, 'Disable')) {
try {
# Disable-ScheduledTask supports -InputObject in modern Windows PowerShell / PSCore
if ($Force) {
Write-Host -ForegroundColor DarkGray "Forcing disable of task $target"
Disable-ScheduledTask -InputObject $task -ErrorAction Stop -Confirm:$false
} else {
Write-Host -ForegroundColor DarkGray "Disabling of task $target"
Disable-ScheduledTask -InputObject $task -ErrorAction Stop
}
[PSCustomObject]@{
TaskName = $task.TaskName
TaskPath = $task.TaskPath
State = 'Disabled'
Time = (Get-Date)
Success = $true
}
} catch {
Write-Warning "Failed to disable task ${target}: $_"
[PSCustomObject]@{
TaskName = $task.TaskName
TaskPath = $task.TaskPath
State = 'Error'
Time = (Get-Date)
Success = $false
Error = $_.Exception.Message
}
}
} else {
# If ShouldProcess returned $false (WhatIf), emit a preview object
[PSCustomObject]@{
TaskName = $task.TaskName
TaskPath = $task.TaskPath
State = 'WouldDisable'
Time = (Get-Date)
Success = $null
}
}
}
}
}
End {
if (-not $names -or $names.Count -eq 0) {
Write-Error "No task names provided. Use -TaskName, pipeline input, or -TaskListFile."
}
}