-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Manage-DailyUpgradeJob.psm1
67 lines (58 loc) · 2.95 KB
/
Manage-DailyUpgradeJob.psm1
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
Import-Module PSScheduledJob
Import-Module -DisableNameChecking "$PSScriptRoot\..\Title-Templates.psm1"
# Adapted from: https://blogs.technet.microsoft.com/heyscriptingguy/2013/11/23/using-scheduled-tasks-and-scheduled-jobs-in-powershell/
function Register-DailyUpgradeJob() {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory)]
[String] $PackageManagerFullName,
[String] $Time,
[ScriptBlock] $UpdateScriptBlock
)
Begin {
$JobName = "$PackageManagerFullName Daily Upgrade"
$ScheduledJob = @{
Name = $JobName
ScriptBlock = $UpdateScriptBlock
Trigger = New-JobTrigger -Daily -At $Time
ScheduledJobOption = New-ScheduledJobOption -RunElevated -MultipleInstancePolicy StopExisting -RequireNetwork
}
$ScheduledJobsPath = "\Microsoft\Windows\PowerShell\ScheduledJobs\"
}
Process {
Write-Status -Types "@", $PackageManagerFullName -Status "Creating a daily task to automatically upgrade $PackageManagerFullName packages at $Time."
If ((Get-ScheduledTask -TaskPath $ScheduledJobsPath -TaskName $JobName -ErrorAction SilentlyContinue) -or (Get-ScheduledJob -Name $JobName -ErrorAction SilentlyContinue)) {
Write-Status -Types "@", $PackageManagerFullName -Status "The ScheduledJob '$JobName' already exists!" -Warning
Write-Status -Types "@", $PackageManagerFullName -Status "Re-Creating with the command:"
Write-Host " { $("$UpdateScriptBlock".Trim(' ')) }`n" -ForegroundColor Cyan
Stop-ScheduledTask -TaskPath $ScheduledJobsPath -TaskName $JobName
Unregister-ScheduledJob -Name $JobName
Register-ScheduledJob @ScheduledJob | Out-Null
} Else {
Write-Status -Types "@", $PackageManagerFullName -Status "Creating Scheduled Job with the command:"
Write-Host " { $("$UpdateScriptBlock".Trim(' ')) }`n" -ForegroundColor Cyan
Register-ScheduledJob @ScheduledJob | Out-Null
}
}
}
function Unregister-DailyUpgradeJob() {
[CmdletBinding()]
param (
[Alias('ScheduledJobName')]
[Parameter(Position = 0, Mandatory)]
[String] $Name
)
Begin {
$ScheduledJobsPath = "\Microsoft\Windows\PowerShell\ScheduledJobs\"
}
Process {
Write-Status -Types "@", "Scripted Job" -Status "Removing the Scheduled Job $Name."
If ((Get-ScheduledTask -TaskPath $ScheduledJobsPath -TaskName $Name -ErrorAction SilentlyContinue) -or (Get-ScheduledJob -Name $Name -ErrorAction SilentlyContinue)) {
Write-Status -Types "@", "Scripted Job" -Status "ScheduledJob: $Name FOUND!"
Stop-ScheduledTask -TaskPath $ScheduledJobsPath -TaskName $Name
Unregister-ScheduledJob -Name $Name
} Else {
Write-Status -Types "@", "Scripted Job" -Status "Scheduled Job $Name was not found." -Warning
}
}
}