Scripts/TriggerDsc.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
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Wait until DSC is idle
while ($true) {
    $dscStatus = Get-DscLocalConfigurationManager
    "$(Get-Date) TriggerDsc status is $($dscStatus.LCMState)"

    if ($dscStatus.LCMState -eq 'Idle') {
        break
    }

    Start-Sleep -Seconds 60
}

# Get last execution
$lastConfiguration = Get-DscConfigurationStatus -All | Sort-Object StateDate -Descending | Select-Object -First 1
"$(Get-Date) TriggerDsc last execution is $($lastConfiguration.Status)"

if ($lastConfiguration.Status -eq 'Success') {
    # Can't do this, it breaks DSC WaitFor on other servers
    # "$(Get-Date) TriggerDsc removing DSC documents so they don't trigger again"
    # Remove-DscConfigurationDocument -Stage Current, Pending, Previous
    "$(Get-Date) TriggerDsc removing scheduled task so this script doesn't trigger again"
    &schtasks.exe /delete /tn 'TriggerDsc' /f
} else {
    "$(Get-Date) TriggerDsc starting DSC"
    Start-DscConfiguration -UseExisting
}