-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-ScheduledRestartComputerJob.ps1
More file actions
90 lines (77 loc) · 3.13 KB
/
Get-ScheduledRestartComputerJob.ps1
File metadata and controls
90 lines (77 loc) · 3.13 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
<#
.Synopsis
Get scheduled Restart-Computer job and associated script.
.DESCRIPTION
Get Scheduled Restart-Computer job and associated script.
.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.
.NOTES
Created by: Jason Wasser @wasserja
Modified: 6/2/2017
.EXAMPLE
Get-ScheduledRestartComputerJob
.EXAMPLE
Get-ScheduledRestartComputerJob -JobNamePrefix 'RestartComputerJob' -ScriptFilePath 'C:\Scripts'
#>
#Requires -RunAsAdministrator
#Requires -Modules PSScheduledJob,ScheduledTasks
function Get-ScheduledRestartComputerJob
{
[CmdletBinding()]
Param
(
[string]$Name,
[string]$JobNamePrefix = 'RestartComputer-',
[ValidateScript({ Test-Path $_ -PathType Container })]
[string]$ScriptFilePath="C:\Temp\"
)
Begin
{
}
Process
{
# Forcing the import of ScheduledTasks to avoid conflict with Carbon\Get-ScheduledTask
Import-Module ScheduledTasks
Write-Verbose -Message 'Gathering existing Restart Jobs.'
if ($Name) {
$RestartJobs = Get-ScheduledJob | Where-Object -FilterScript {$_.Name -like "*$Name*"}
}
else {
$RestartJobs = Get-ScheduledJob | Where-Object -FilterScript {$_.Name -like "$JobNamePrefix*"}
}
foreach ($RestartJob in $RestartJobs) {
$IsJobExpired = $false
Write-Verbose "Checking state of RestartJob $($RestartJob.Name)."
$RestartJobStatus = Get-ScheduledTask -TaskPath '\Microsoft\Windows\PowerShell\ScheduledJobs\' -TaskName $RestartJob.Name
Write-Verbose "RestartJob $($RestartJob.Name) is currently $($RestartJobStatus.State)."
foreach ($JobTrigger in $RestartJob.JobTriggers) {
if ($JobTrigger.At -lt $(Get-Date)) {
Write-Verbose "JobTrigger for $($RestartJob.Name) has expired."
$IsJobExpired = $true
}
}
$RestartJobScriptPath = $ScriptFilePath + $RestartJob.Name + ".ps1"
$ScheduledRestartComputerJobProperties = [ordered]@{
'Id' = $RestartJob.Id
'Name' = $RestartJob.Name
'JobTrigger' = $RestartJob.JobTriggers[0].At
'JobStatus' = $RestartJobStatus.State
'JobScript' = $RestartJobScriptPath
'IsJobExpired' = $IsJobExpired
'Enabled' = $RestartJob.Enabled
'JobScriptSource' = if (Test-Path $RestartJobScriptPath) {Get-Content -Path $RestartJobScriptPath}
}
$ScheduledRestartComputerJob = New-Object -TypeName PSCustomObject -Property $ScheduledRestartComputerJobProperties
$ScheduledRestartComputerJob
}
}
End
{
}
}