Last active
December 15, 2017 21:43
-
-
Save Chirishman/e4cdbf6223e6be2bb9ed08f612b6881e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Configuration UpdateAndScheduleReboot{ | |
Param( | |
[string]$NodeName='localhost', | |
[datetime]$TargetRebootTime | |
) | |
Import-DscResource –ModuleName ’PSDesiredStateConfiguration’ | |
Import-DSCResource -ModuleName xPendingReboot | |
Import-DSCResource -ModuleName xWindowsUpdate | |
$TargetRebootTime = $AllNodes.TargetRebootTime | |
Script RebootEnableJob { | |
GetScript = { | |
return @{Result = "HelloWorld"} | |
} | |
TestScript = { | |
$ScheduledEnableJob = Get-ScheduledJob | ?{$_.Name -eq 'EnableReboot'} | |
if ($ScheduledEnableJob){ | |
if ($true <#Check for time match#>){ | |
Write-Verbose "Matching job found" | |
return $true | |
} else { | |
Write-Verbose "Matching job found - Stale Trigger Time" | |
return $false | |
} | |
} else { | |
Write-Verbose "No matching job found" | |
Return $false | |
} | |
} | |
SetScript = { | |
$ScheduledEnableJob = Get-ScheduledJob | ?{$_.Name -eq 'EnableReboot'} | |
if ($ScheduledEnableJob){ | |
Write-Verbose "Modifying Existing Job" | |
$ScheduledEnableJob | Set-ScheduledJob -Trigger (New-JobTrigger -Once -At $Using:TargetRebootTime) | |
} else { | |
Write-Verbose "Creating New Scheduled Job with target trigger time $($Using:TargetRebootTime)" | |
Write-Verbose "User: $([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)" | |
Write-Verbose "Current User: $env:UserName" | |
$EnableJob = @{ | |
Name = 'EnableReboot' | |
ScriptBlock = { | |
configuration RebootSetting | |
{ | |
Param( | |
[string[]]$NodeName = 'localhost' | |
) | |
LocalConfigurationManager { | |
RebootNodeIfNeeded = $true | |
} | |
} | |
$ConfigPath = "C:\temp\RebootSetting" | |
& ([scriptblock]::Create("RebootSetting -OutputPath $ConfigPath")) | |
Set-DscLocalConfigurationManager -Path $ConfigPath | |
} | |
Trigger = New-JobTrigger -Once -At $Using:TargetRebootTime | |
ScheduledJobOption = New-ScheduledJobOption -RunElevated -DoNotAllowDemandStart | |
ErrorAction = 'Stop' | |
} | |
Register-ScheduledJob @EnableJob | |
} | |
} | |
} | |
Script RebootDisableJob { | |
GetScript = { | |
return @{Result = "HelloWorld"} | |
} | |
TestScript = { | |
$ScheduledDisableJob = Get-ScheduledJob | ?{$_.Name -eq 'DisableReboot'} | |
if ($ScheduledDisableJob){ | |
if ($true <#Check for time match#>){ | |
Write-Verbose "Matching job found" | |
return $true | |
} else { | |
Write-Verbose "Matching job found - Stale Trigger Time" | |
return $false | |
} | |
} else { | |
Write-Verbose "No matching job found" | |
Return $false | |
} | |
} | |
SetScript = { | |
$ScheduledDisableJob = Get-ScheduledJob | ?{$_.Name -eq 'DisableReboot'} | |
if ($ScheduledDisableJob){ | |
Write-Verbose "Modifying Existing Job" | |
$ScheduledDisableJob | Set-ScheduledJob -Trigger (New-JobTrigger -Once -At ($Using:TargetRebootTime).AddHours(1.5)) | |
} else { | |
Write-Verbose "Creating New Scheduled Job with target trigger time $Using:TargetRebootTime" | |
$DisableJob = @{ | |
Name = 'DisableReboot' | |
ScriptBlock = { | |
configuration RebootSetting | |
{ | |
Param( | |
[string[]]$NodeName = 'localhost' | |
) | |
LocalConfigurationManager { | |
RebootNodeIfNeeded = $False | |
} | |
} | |
$ConfigPath = "C:\temp\RebootSetting" | |
"RebootSetting -OutputPath $ConfigPath" | |
Set-DscLocalConfigurationManager -Path $ConfigPath | |
} | |
Trigger = New-JobTrigger -Once -At $Using:TargetRebootTime | |
ScheduledJobOption = New-ScheduledJobOption -RunElevated -DoNotAllowDemandStart | |
} | |
Register-ScheduledJob @DisableJob | |
} | |
} | |
} | |
xWindowsUpdateAgent MuSecurityImportant { | |
IsSingleInstance = 'Yes' | |
UpdateNow = $True | |
Source = 'WindowsUpdate' | |
Notifications = 'ScheduledInstallation' | |
} | |
xPendingReboot CheckReboot { | |
Name = "Check for a pending reboot and trigger if true" | |
} | |
} | |
$ConfigData = @{ | |
AllNodes = @( | |
@{ | |
NodeName = 'localhost' | |
TargetRebootTime = (Get-Date 0:0:0.0).AddDays(3) | |
PSDscAllowDomainUser = $true | |
PsDscRunAsCredential = $localadmin | |
} | |
) | |
} | |
UpdateAndScheduleReboot -ConfigurationData $ConfigData -OutputPath (Join-Path -Path @($env:SystemDrive) -ChildPath ('Temp\UpdateAndScheduleReboot')) | |
Start-DscConfiguration -Path C:\temp\UpdateAndScheduleReboot -Force -Wait -Verbose | |
#install-module -Name @('xComputerManagement','xPendingReboot','xWindowsUpdate') -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment