Skip to content

Commit 037e3f2

Browse files
committed
The sync check will now check for multiple status messages to determine if the SUP is still syncing.
1 parent 0381dcb commit 037e3f2

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

Invoke-DGASoftwareUpdateMaintenance/Invoke-DGASoftwareUpdateMaintenance.ps1

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Param(
188188

189189
#Force the script to run even if it was run recently.
190190
[Parameter(ParameterSetName='cmdline')]
191-
191+
192192
[Parameter(ParameterSetName='configfile')]
193193
[switch]$Force,
194194

@@ -403,9 +403,22 @@ Function Invoke-CMSyncCheck {
403403
Start-Sleep -Seconds (300)
404404
}
405405

406+
<#
407+
Source: http://eskonr.com/2015/01/download-sccm-configmgr-2012-r2-cu3-status-messages-documentation/
408+
6701 = WSUS Synchronization started.
409+
6702 = WSUS Synchronization done.
410+
6703 = WSUS Synchronization failed.
411+
6704 = WSUS Synchronization in progress. Current phase: Synchronizing WSUS Server.
412+
6705 = WSUS Synchronization in progress. Current phase: Synchronizing site database.
413+
6706 = WSUS Synchronization in progress. Current phase: Synchronizing Internet facing WSUS Server.
414+
6707 = Content of WSUS Server %1 is out of sync with upstream server %2.
415+
6708 = WSUS synchronization complete, with pending license terms downloads.
416+
#>
417+
$SynchronizingStatusMessages = @(6701,6704,6705,6706)
418+
406419
$Synchronizing = $False
407420
ForEach ($softwareUpdatePointSyncStatus in Get-CMSoftwareUpdateSyncStatus){
408-
If($softwareUpdatePointSyncStatus.LastSyncState -eq 6704){$Synchronizing = $True}
421+
If($softwareUpdatePointSyncStatus.LastSyncState -in $SynchronizingStatusMessages){$Synchronizing = $True}
409422
}
410423
} Until(!$Synchronizing)
411424

@@ -712,7 +725,7 @@ Function Get-WSUSDB{
712725
Add-TextToCMLog $LogFile "Successfully tested the connection to the ($($WSUSServerDB.DatabaseName)) database on $($WSUSServerDB.ServerName)." $component 1
713726
}
714727
Catch{
715-
Add-TextToCMLog $LogFile "Failed to connect to the ($($WSUSServerDB.DatabaseName)) database on $($WSUSServerDB.ServerName)." $component 3
728+
Add-TextToCMLog $LogFile "Failed to connect to the ($($WSUSServerDB.DatabaseName)) database on $($WSUSServerDB.ServerName)." $component 3
716729
Add-TextToCMLog $LogFile "Error ($($_.Exception.HResult)): $($_.Exception.Message)" $component 3
717730
Add-TextToCMLog $LogFile "$($_.InvocationInfo.PositionMessage)" $component 3
718731
Return
@@ -941,7 +954,7 @@ If (Test-Path -Path $lastRanPath -NewerThan ((get-date).AddHours(-24).ToString()
941954
}
942955
}
943956

944-
#Mark the last time the script ran. We do this now and at the end to avoid running multiple instances of the script at the same time.
957+
#Mark the last time the script ran. We do this now and at the end to avoid running multiple instances of the script at the same time.
945958
If (!$WhatIfPreference){Get-Date | Out-File $lastRanPath -Force}
946959

947960
#Check to make sure we're running this on a primary site server that has the SMS namespace.
@@ -1237,7 +1250,7 @@ If ($UseCustomIndexes){
12371250
#Disconnect from the database.
12381251
$SqlConnection.Close()
12391252
} #Connect-WSUSDB
1240-
1253+
12411254

12421255
}
12431256

@@ -1319,7 +1332,7 @@ If($FirstRun){
13191332
$LocalUpdateIdRows = Invoke-SQLCMD $SqlConnection "Use $($WSUSServerDB.DatabaseName);Select UpdateID from tbUpdate Where LocalUpdateID = $($ObsoleteUpdates.Rows[$i][0])"
13201333
If ($LocalUpdateIdRows.Rows.Count -gt 0){
13211334
$LocalUpdateId = $LocalUpdateIdRows.Rows[0][0]
1322-
1335+
13231336
$UpdateTitleRows = Invoke-SQLCMD $SqlConnection "Use $($WSUSServerDB.DatabaseName);Select DefaultTitle from PUBLIC_VIEWS.vUpdate Where UpdateId = '$LocalUpdateId'"
13241337
If ($UpdateTitleRows.Rows.Count -gt 0){
13251338
$UpdateTitle = $UpdateTitleRows.Rows[0][0]
@@ -1450,7 +1463,7 @@ If ($DeleteDeclined -or $DeclineSuperseded -or $DeclineByTitle -or $DeclineByPlu
14501463
Else{
14511464
$DeclinedUpdateData.Set_Item($Update.Id.UpdateId,(Get-Date))
14521465
}
1453-
1466+
14541467
} #ForEach DeclinedUpdates
14551468
} #Deletedeclined
14561469

Invoke-DGASoftwareUpdatePointSync.ps1

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Function Add-TextToCMLog {
4545
##########################################################################################################
4646
<#
4747
.SYNOPSIS
48-
Log to a file in a format that can be read by Trace32.exe / CMTrace.exe
48+
Log to a file in a format that can be read by Trace32.exe / CMTrace.exe
4949
5050
.DESCRIPTION
5151
Write a line of data to a script log file in a format that can be parsed by Trace32.exe / CMTrace.exe
@@ -67,7 +67,7 @@ Function Add-TextToCMLog {
6767
Add-TextToCMLog c:\output\update.log "Application of MS15-031 failed" Apply_Patch 3
6868
6969
This will write a line to the update.log file in c:\output stating that "Application of MS15-031 failed".
70-
The source component will be Apply_Patch and the line will be highlighted in red as it is an error
70+
The source component will be Apply_Patch and the line will be highlighted in red as it is an error
7171
(severity - 3).
7272
7373
#>
@@ -96,7 +96,7 @@ Param(
9696

9797

9898
#Obtain UTC offset
99-
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
99+
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
100100
$DateTime.SetVarDate($(Get-Date))
101101
$UtcValue = $DateTime.Value
102102
$UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21)
@@ -148,7 +148,7 @@ ForEach ($status in $SyncStatus){
148148
'WSUSServerName'=$status.WSUSServerName;
149149
'WSUSSourceServer'=$status.WSUSSourceServer}
150150

151-
$Results+= New-Object TypeName PSObject Prop $properties
151+
$Results+= New-Object TypeName PSObject Prop $properties
152152
}
153153

154154

@@ -180,24 +180,37 @@ Function Invoke-SyncCheck {
180180

181181
$WaitInterval = 0 #Used to skip the initial wait cycle if it isn't necessary.
182182
Do{
183-
183+
184184
#Wait until the loop has iterated once.
185185
If ($WaitInterval -gt 0){
186186
Add-TextToCMLog $LogFile "Waiting $TimeToWait minutes for lead time to pass before executing." $component 1
187-
Start-Sleep -Seconds ($WaitInterval)
188-
}
187+
Start-Sleep -Seconds ($WaitInterval)
188+
}
189189

190190
#Loop through each SUP and wait until they are all done syncing.
191191
Do {
192192
#If syncronizing then wait.
193193
If($Syncronizing){
194-
Add-TextToCMLog $LogFile "Waiting for software update points to stop syncing." $component 1
195-
Start-Sleep -Seconds (300)
194+
Add-TextToCMLog $LogFile "Waiting for software update points to stop syncing." $component 1
195+
Start-Sleep -Seconds (300)
196196
}
197197

198+
<#
199+
Source: http://eskonr.com/2015/01/download-sccm-configmgr-2012-r2-cu3-status-messages-documentation/
200+
6701 = WSUS Synchronization started.
201+
6702 = WSUS Synchronization done.
202+
6703 = WSUS Synchronization failed.
203+
6704 = WSUS Synchronization in progress. Current phase: Synchronizing WSUS Server.
204+
6705 = WSUS Synchronization in progress. Current phase: Synchronizing site database.
205+
6706 = WSUS Synchronization in progress. Current phase: Synchronizing Internet facing WSUS Server.
206+
6707 = Content of WSUS Server %1 is out of sync with upstream server %2.
207+
6708 = WSUS synchronization complete, with pending license terms downloads.
208+
#>
209+
$SynchronizingStatusMessages = @(6701,6704,6705,6706)
210+
198211
$Syncronizing = $False
199212
ForEach ($softwareUpdatePointSyncStatus in Get-CMSoftwareUpdateSyncStatus){
200-
If($softwareUpdatePointSyncStatus.LastSyncState -eq 6704){$Syncronizing = $True}
213+
If($softwareUpdatePointSyncStatus.LastSyncState -in $SynchronizingStatusMessages){$Syncronizing = $True}
201214
}
202215
} Until(!$Syncronizing)
203216

@@ -215,7 +228,7 @@ Function Invoke-SyncCheck {
215228
}
216229
}
217230

218-
231+
219232
#Calculate the remaining time to wait for the lead time to expire.
220233
$TimeToWait = ($syncTimeStamp.AddMinutes($SyncLeadTime) - (Get-Date)).Minutes
221234

@@ -274,14 +287,14 @@ Function Get-SiteCode {
274287
}
275288

276289
#If PSDrive exists then get the site code from it. Otherwise, try to create the drive.
277-
If ($PSDriveExists) {
290+
If ($PSDriveExists) {
278291
$SiteCode = Get-PSDrive -PSProvider CMSite
279292
} Else {
280293
#Try to determine the site code if none was passed in.
281294
If (-Not ($SiteCode) ) {
282295
If (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\SMS\Identification" -Value "Site Code"){
283296
$SiteCode = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\SMS\Identification" | Select-Object -ExpandProperty "Site Code"
284-
} ElseIf (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client" -Value "AssignedSiteCode") {
297+
} ElseIf (Test-RegistryValue -Path "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client" -Value "AssignedSiteCode") {
285298
$SiteCode = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\SMS\Mobile Client" | Select-Object -ExpandProperty "AssignedSiteCode"
286299
} Else {
287300
Return
@@ -303,7 +316,7 @@ $LogFile = "filesystem::$($LogFile)"
303316
$component = (Split-Path $PSCommandPath -Leaf).Replace(".ps1", "")
304317

305318
#If the log file exists and is larger then the maximum then roll it over.
306-
If (Test-path $LogFile -PathType Leaf) {
319+
If (Test-path $LogFile -PathType Leaf) {
307320
If ((Get-Item $LogFile).length -gt $MaxLogSize){
308321
Move-Item -Force $LogFile ($LogFile -replace ".$","_") -WhatIf:$False
309322
}
@@ -323,7 +336,7 @@ If (Test-Path -Path $lastRanPath -NewerThan ((get-date).AddHours(-24).ToString()
323336
}
324337

325338
#Make sure the last Patch Tuesday was less than a week away.
326-
If ($WeekOfPatchTuesday){
339+
If ($WeekOfPatchTuesday){
327340
#Care of: http://www.madwithpowershell.com/2014/10/calculating-patch-tuesday-with.html
328341
$BaseDate = ( Get-Date -Day 12 ).Date
329342
$PatchTuesday = $BaseDate.AddDays( 2 - [int]$BaseDate.DayOfWeek )
@@ -362,11 +375,11 @@ If (!$SiteCode){$SiteCode = Get-SiteCode}
362375
If (! (Test-Path "$($SiteCode):")) {
363376
Try{
364377
Add-TextToCMLog $LogFile "Trying to create the PS Drive $($SiteCode)" $component 1
365-
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root "." -WhatIf:$False | Out-Null
378+
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root "." -WhatIf:$False | Out-Null
366379
} Catch {
367380
Add-TextToCMLog $LogFile "The site's PS drive doesn't exist nor could it be created." $component 3
368381
Add-TextToCMLog $LogFile "Error: $($_.Exception.Message)" $component 3
369-
Add-TextToCMLog $LogFile "$($_.InvocationInfo.PositionMessage)" $component 3
382+
Add-TextToCMLog $LogFile "$($_.InvocationInfo.PositionMessage)" $component 3
370383
Return
371384
}
372385
}
@@ -376,7 +389,7 @@ $OriginalLocation = Get-Location
376389

377390
#Set and verify the location.
378391
Try{
379-
Add-TextToCMLog $LogFile "Connecting to site: $($SiteCode)" $component 1
392+
Add-TextToCMLog $LogFile "Connecting to site: $($SiteCode)" $component 1
380393
Set-Location "$($SiteCode):" | Out-Null
381394
} Catch {
382395
Add-TextToCMLog $LogFile "Could not set location to site: $($SiteCode)." $component 3
@@ -396,10 +409,8 @@ If ((Get-CMSite).Version -lt $cmSiteVersion){
396409
Write-Warning "$($ModuleName) requires configuration manager cmdlets $($cmSiteVersion.ToString()) or greater."
397410
}
398411

399-
Invoke-SyncCheck
400-
401412
#Try to initiate an update sync.
402-
Try
413+
Try
403414
{
404415
Sync-CMSoftwareUpdate -FullSync:$FullSync
405416
If ($FullSync){
@@ -411,12 +422,12 @@ Try
411422
If ($Wait){
412423
Start-Sleep -Seconds 30
413424
Add-TextToCMLog $LogFile "Initiating a sync check." $component 1
414-
Invoke-SyncCheck -SyncLeadTime 0
425+
Invoke-SyncCheck -SyncLeadTime 0
415426
}
416427
}
417428
Catch [System.Exception]
418429
{
419-
Add-TextToCMLog $LogFile "Failed to initiate an update sync.)" $component 3
430+
Add-TextToCMLog $LogFile "Failed to initiate an update sync.)" $component 3
420431
Add-TextToCMLog $LogFile "Error: $($_.Exception.Message)" $component 3
421432
Add-TextToCMLog $LogFile "$($_.InvocationInfo.PositionMessage)" $component 3
422433
}

0 commit comments

Comments
 (0)