Last active
February 12, 2024 11:41
-
-
Save flandersen/bd1d4cd093a1a265e950a5042a35f413 to your computer and use it in GitHub Desktop.
Setup a Windows Scheduler Task for TrackGpo
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
#requires -RunAsAdministrator | |
# Note: Since the *-ScheduledTask* cmdlets are implemented as | |
# CDXML-based *script* modules, they only see preference vars. | |
# in the *global* scope - see https://github.com/PowerShell/PowerShell/issues/4568 | |
$prevEaPref = $global:ErrorActionPreference | |
$global:ErrorActionPreference = 'Stop' | |
# Setup the scheduled task: | |
# The command (program) to run. | |
$action = New-ScheduledTaskAction ` | |
-Execute powershell ` | |
-Argument "-ExecutionPolicy Bypass -NoProfile -Command & { `"Invoke-GpoTracking -GpoRepo C:\GPO_Repo -WorkingDir C:\GPO_WorkingDir`" }" | |
# Run as 'NT AUTHORITY\SYSTEM', which runs: | |
# * invisibly | |
# * whether or not someone is logged on | |
# * implicitly with elevation | |
$user = New-ScheduledTaskPrincipal ` | |
-UserID 'NT AUTHORITY\SYSTEM' ` | |
-LogonType ServiceAccount | |
# Run as 'sMSA', which runs: | |
# * invisibly | |
# * whether or not someone is logged on | |
# $user = New-ScheduledTaskPrincipal ` | |
# -UserID 'domain\MSA1$' ` | |
# -LogonType Password | |
# # Advanced settings such as whether to allow start on demand, not running when on batter power, ... | |
# $settings = New-ScheduledTaskSettingsSet | |
# When to run it: | |
$Now = (Get-Date) | |
$trigger = New-ScheduledTaskTrigger -Once ` | |
-At $Now ` | |
-RepetitionInterval (New-TimeSpan -Hours 8) ` | |
# -RepetitionDuration ([System.TimeSpan]::MaxValue) # <-- OS < Windows Server 2016 | |
# Create the task from the above. | |
$newTask = New-ScheduledTask -Action $action -Principal $user -Trigger $trigger | |
# Register the task with name '_Test' | |
Write-Verbose -Verbose "Creating task..." | |
Register-ScheduledTask 'Backup GPOs' -InputObject $newTask -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The owner of the git repo directory should be SYSTEM. Alternatively, add it to the safe directories for the SYSTEM user using
git config --global --add safe.directory C:/GPO_Repo
.