Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DarkAllien/255803667ad6c764ab3397a103d9e961 to your computer and use it in GitHub Desktop.

Select an option

Save DarkAllien/255803667ad6c764ab3397a103d9e961 to your computer and use it in GitHub Desktop.
Function Check_MW_Go {
#getting maintenance windows of type 1 (All deployments)
#https://msdn.microsoft.com/library/jj155419.aspx
$CCMServiceWindows = Get-WmiObject -Namespace root\ccm\clientsdk -Class CCM_ServiceWindow -Filter 'Type=1'
$Checker = $false
#parsing maintenance windows to determine if we are in one
foreach ($CCMServiceWindow in $CCMServiceWindows) {
$StartTime = $CCMServiceWindow.StartTime.Substring(0, 12)
$EndTime = $CCMServiceWindow.EndTime.Substring(0, 12)
$StartMW = [datetime]::ParseExact($StartTime, "yyyyMMddHHmm", $null)
$EndMW = [datetime]::ParseExact($EndTime, "yyyyMMddHHmm", $null)
$c_time = get-date
# checking MW interval
if ($c_time -ge $StartMW -and $c_time -lt $EndMW) {
$Checker = $true
"$time - Found MW: $StartMW - $EndMW" | out-file "$env:windir\Logs\CB.SQL.Servers.Updates.log" -Append
}
}
return $Checker
}
$time = Get-Date
if (Check_MW_Go) {
"$time - CI.SQL.Servers.Restart - Remediation - MW OK - Pending SQL updates requested reboot Found - Rebooting" | out-file "$env:windir\Logs\CB.SQL.Servers.Updates.log" -Append
"$time - CI.SQL.Servers.Restart - Remediation - MW OK - Creating Application Evaluation Schedulled Task" | out-file "$env:windir\Logs\CB.SQL.Servers.Updates.log" -Append
# Add regkey to Control Restart (0 initial, 1 needs restart)
Push-Location
Set-Location HKLM:
$registryPath = "SYSTEM\SCCM"
$Group = "SQL_Restart"
$Group_value = "0"
New-Item -Path $registryPath
New-ItemProperty -Path $registryPath -Name $Group -Value $Group_value -PropertyType String -Force
Pop-Location
Unregister-ScheduledTask -TaskName SCCMAppEval2 -Confirm:$False -ErrorAction Ignore
$taskname = "SCCMAppEval2"
$taskdescription = "SCCMAppEval2"
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument "-Noprofile -Windowstyle Hidden -command & {Invoke-WmiMethod -Namespace root\ccm -Class sms_client -Name TriggerSchedule -ArgumentList '{00000000-0000-0000-0000-000000000121}'}"
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay (New-TimeSpan -minutes 10)
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 5) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskname -Description $taskdescription -Settings $settings -User "System" -RunLevel Highest
Restart-Computer -Force
}
else {
"$time - CI.SQL.Servers.Restart - Remediation - MW NOT OK - Pending SQL updates requested reboot Found - NOT Rebooting" | out-file "$env:windir\Logs\CB.SQL.Servers.Updates.log" -Append
"$time - CI.SQL.Servers.Restart - Remediation - MW NOT OK - NOT Creating Application Evaluation Schedulled Task" | out-file "$env:windir\Logs\CB.SQL.Servers.Updates.log" -Append
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment