-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemove-ScheduledRestartComputerJob.ps1
70 lines (61 loc) · 2.26 KB
/
Remove-ScheduledRestartComputerJob.ps1
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
<#
.Synopsis
Remove scheduled Restart-Computer job and script.
.DESCRIPTION
Remove scheduled Restart-Computer job and script.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 6/2/2017
.PARAMETER Name
The full name of the scheduled restart computer job if known.
.PARAMETER JobNamePrefix
The prefix of the scheduled restart computer job exists to differentiate
the restart jobs from other PowerShell scheduled jobs.
.PARAMETER ScriptFilePath
The path to the script file that is executed by the scheduled restart computer
job.
.EXAMPLE
Remove-ScheduledRestartComputerJob
.EXAMPLE
Remove-ScheduledRestartComputerJob -Name MyJob -JobNamePrefix 'RestartComputerJob' -ScriptFilePath 'C:\Scripts'
#>
#Requires -RunAsAdministrator
#Requires -Modules PSScheduledJob
function Remove-ScheduledRestartComputerJob
{
[CmdletBinding(SupportsShouldProcess=$True)]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true)]
[string]$Name,
[string]$JobNamePrefix = 'RestartComputer-',
[ValidateScript({ Test-Path $_ -PathType Container })]
[string]$ScriptFilePath="C:\Temp\"
)
Begin
{
}
Process
{
Write-Verbose -Message 'Gathering Restart jobs to remove.'
$RestartJobs = Get-ScheduledRestartComputerJob | Where-Object -FilterScript {$_.Name -like $Name}
foreach ($RestartJob in $RestartJobs) {
if ($RestartJob.JobStatus -eq 'Running') {
Write-Warning -Message "RestartJob $($RestartJob.Name) is currently $($RestartJob.JobStatus). Skipping removal of job."
}
else {
Write-Verbose -Message "RestartJob $($RestartJob.Name) is currently $($RestartJob.JobStatus). Proceeding to remove job."
Write-Verbose -Message "Unregistering RestartJob $($RestartJob.Name)"
Unregister-ScheduledJob -Id $RestartJob.Id
if (Test-Path -Path $RestartJob.JobScript) {
Write-Verbose -Message "Deleting RestartJob Script $($RestartJob.JobScript)"
Remove-Item $RestartJob.JobScript
}
}
}
}
End
{
}
}