-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wait-DSCNodeConvergence.ps1
141 lines (120 loc) · 3.27 KB
/
Wait-DSCNodeConvergence.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function Wait-DSCNodeConvergence
{
[CmdletBinding()]
[OutputType([array])]
param
(
[string[]]$ComputerName,
[PScredential]$Credential,
[datetime]$DateTime = (Get-Date),
[Int]$StatusWaitIntervalSeconds = '5'
)
# Prepare a New-PSSession parameters object
$NewSessionparam = @{
ComputerName = ""
}
if ($Credential)
{
$NewSessionparam.Add("Credential", $Credential)
}
# Build our computer objects to monitor
[System.Array]$Output = @()
foreach ($Computer in $ComputerName)
{
$DSCNode = New-Object -TypeName PSCustomObject -Property @{
ComputerName = $Computer
ConvergeAfterTime = $DateTime
StartTime = (Get-Date)
CapturedEndTime = ""
Status = ""
DurationMinutes = ""
SessionInfo = ""
}
$Output += $DSCNode
}
# Make sure every node has an open session so the release is coordinated
do
{
foreach ($DSCNode in $Output)
{
if ($DSCNode.SessionInfo.State -ne "Opened")
{
$NewSessionparam.ComputerName = $DSCNode.ComputerName
$DSCNode.SessionInfo = (New-PSSession @NewSessionparam -ErrorAction SilentlyContinue)
}
}
}
until (($Output.Where{
$PSitem.SessionInfo.State -eq "Opened"
}.Count) -eq $Output.Count)
do
{
# Here is where we run the Invoke-Command to DSCConfigurationStatus and update the object
Write-Verbose -Message "Waiting for for Status Updates ..."
Start-Sleep -Seconds $StatusWaitIntervalSeconds
foreach ($DSCNode in $Output)
{
if (($DSCNode.Status -ne "Success") -and ($DSCNode.StartDate -lt $DateTime))
{
if ($DSCNode.SessionInfo.State -eq "Opened")
{
$StatusData = Invoke-Command -Session $DSCNode.SessionInfo -ScriptBlock {
Get-DscConfigurationStatus
} -ErrorAction SilentlyContinue # CONSIDER ERROR VARIABLE SO IF SERVER REBOOTED NEED TO REBUILD SESSION
if ($StatusData)
{
# Update $DSCNode with $StatusData attributes
# Compare the time to the ConvergedAfterTime, if later then update the data, if not then "Waiting"
if ($StatusData.StartDate -lt $DateTime)
{
$DSCNode.Status = "Waiting"
}
else
{
$DSCNode.Status = $StatusData.Status
}
if ($StatusData.Status -eq "Success")
{
if ($StatusData.RebootRequested -eq $true)
{
$DSCNode.Status = "Awaiting Reboot"
}
else {
# Set the End time and average time, etc.
$DSCNode.CapturedEndTime = Get-Date
$DSCNode.DurationMinutes = ($DSCNode.CapturedEndTime - $DateTime).Minutes
}
}
}
}
else
{
$DSCNode.Status = "Not Reachable"
do
{
$NewSessionparam.ComputerName = $DSCNode.ComputerName
$RebuiltSession = New-PSSession @NewSessionparam -ErrorAction SilentlyContinue
}
until ($RebuiltSession)
$DSCNode.SessionInfo = $RebuiltSession
}
}
}
# Rudimentary output for experimentation
Write-Output ""
Write-Output ""
Write-Output $Output | Format-Table -AutoSize
Write-Output ""
Write-Output ""
}
until (($Output.Where{
$PSitem.Status -eq "Success"
}.Count) -eq $Output.Count)
# Close all sessions
foreach ($DSCNode in $Output)
{
Remove-PSSession -Session $DSCNode.SessionInfo
}
Write-Verbose -Message "Convergence is complete"
return $output
}