-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCMRebootNodeIfNeeded.psm1
87 lines (78 loc) · 7.38 KB
/
LCMRebootNodeIfNeeded.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
[DscResource(RunAsCredential = 'NotSupported')]
class LCMRebootNodeIfNeeded
{
[DscProperty(Key)]
[string]$TaskName
[DscProperty(Mandatory)]
[bool]$RebootNodeIfNeeded
[void] Set()
{
$ScheduledTaskCommand = Get-Content (Join-Path -Path $PSScriptRoot -ChildPath "ScheduledTask\ScheduledTaskCommand.ps1")
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$TaskName', $($this.TaskName))
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$RebootNodeIfNeeded', "`$$($this.RebootNodeIfNeeded)")
if ($this.RebootNodeIfNeeded)
{
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$OldValue', 'RebootNodeIfNeeded = False;')
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$NewValue', 'RebootNodeIfNeeded = True;')
}
else
{
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$OldValue', 'RebootNodeIfNeeded = True;')
$ScheduledTaskCommand = $ScheduledTaskCommand.Replace('$NewValue', 'RebootNodeIfNeeded = False;')
}
[xml]$scheduledTaskXML = Get-Content (Join-Path -Path $PSScriptRoot -ChildPath "ScheduledTask\ScheduledTask.xml")
$scheduledTaskXML.Task.RegistrationInfo.Date = $(Get-Date -Format o).ToString()
$scheduledTaskXML.Task.RegistrationInfo.Description = "Sets the RebootNodeIfNeeded parameter in Local Configuration Manager to $($this.RebootNodeIfNeeded)."
$scheduledTaskXML.Task.Actions.Exec.Arguments = "-enc $([Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($ScheduledTaskCommand)))"
if (([Environment]::OSVersion.Version.Major -eq 6 -and [Environment]::OSVersion.Version.Minor -ge 2) -or ([Environment]::OSVersion.Version.Major -gt 6))
{
Register-ScheduledTask -Xml $scheduledTaskXML.OuterXml -TaskName $this.TaskName -Force
}
else
{
$fileName = [System.IO.Path]::GetTempFileName()
$scheduledTaskXML | Out-File -FilePath $fileName
Start-Process schtasks.exe -ArgumentList "/Create /XML $fileName /tn $($this.TaskName)" -NoNewWindow -Wait
Remove-Item $fileName -Force
}
}
[LCMRebootNodeIfNeeded] Get()
{
$this.RebootNodeIfNeeded = (Invoke-CimMethod -Namespace root/Microsoft/Windows/DesiredStateConfiguration –ClassName MSFT_DSCLocalConfigurationManager –MethodName GetMetaConfiguration).MetaConfiguration.RebootNodeIfNeeded
return $this
}
[bool] Test()
{
$currentRebootNodeIfNeeded = (Invoke-CimMethod -Namespace root/Microsoft/Windows/DesiredStateConfiguration –ClassName MSFT_DSCLocalConfigurationManager –MethodName GetMetaConfiguration).MetaConfiguration.RebootNodeIfNeeded
if (([Environment]::OSVersion.Version.Major -eq 6 -and [Environment]::OSVersion.Version.Minor -ge 2) -or ([Environment]::OSVersion.Version.Major -gt 6))
{
$task = Get-ScheduledTask -TaskName $this.TaskName -ErrorAction SilentlyContinue
}
else
{
$task = [xml](. schtasks.exe /Query /TN $this.TaskName /XML 2>$null)
}
if ($this.RebootNodeIfNeeded)
{
if ($currentRebootNodeIfNeeded -or ![string]::IsNullOrEmpty($task))
{
return $true
}
else
{
return $false
}
}
else
{
if (!$currentRebootNodeIfNeeded -or ![string]::IsNullOrEmpty($task))
{
return $true
}
else
{
return $false
}
}
}
}