31 lines
2.3 KiB
PowerShell
31 lines
2.3 KiB
PowerShell
Set-StrictMode -Version Latest
|
|
|
|
$script:TaskName = 'ConfigCentral-UserSync'
|
|
|
|
function Register-ConfigCentralSchedule {
|
|
param([Parameter(Mandatory)][string]$RepositoryRoot, [Parameter(Mandatory)][string]$Machine, [Parameter(Mandatory)]$Paths)
|
|
if (-not $IsWindows) { throw 'Scheduled sync is available only on Windows.' }
|
|
$scriptPath = Join-Path $RepositoryRoot 'cc.ps1'
|
|
if (-not (Test-Path -LiteralPath $scriptPath -PathType Leaf)) { throw "CLI script is missing at '$scriptPath'." }
|
|
foreach ($value in @($scriptPath, $Machine, $Paths.Root, $RepositoryRoot)) {
|
|
if ($value.Contains('"')) { throw 'Scheduled task arguments contain unsupported quote characters.' }
|
|
}
|
|
$arguments = "-NoLogo -NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`" sync -Machine `"$Machine`" -ConfigsOnly -DataRoot `"$($Paths.Root)`" -RepositoryRoot `"$RepositoryRoot`""
|
|
$action = New-ScheduledTaskAction -Execute 'pwsh.exe' -Argument $arguments
|
|
$trigger = New-ScheduledTaskTrigger -AtLogOn -User ([Security.Principal.WindowsIdentity]::GetCurrent().Name)
|
|
$principal = New-ScheduledTaskPrincipal -UserId ([Security.Principal.WindowsIdentity]::GetCurrent().Name) -LogonType Interactive -RunLevel Limited
|
|
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Hours 1) -MultipleInstances IgnoreNew
|
|
Register-ScheduledTask -TaskName $script:TaskName -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Description 'Fetch and deploy Config Central user configuration at logon. Package operations remain manual.' -Force | Out-Null
|
|
[pscustomobject]@{ name = $script:TaskName; status = 'Scheduled'; message = 'Config-only sync will run at user logon.' }
|
|
}
|
|
|
|
function Unregister-ConfigCentralSchedule {
|
|
if (-not $IsWindows) { throw 'Scheduled sync is available only on Windows.' }
|
|
$task = Get-ScheduledTask -TaskName $script:TaskName -ErrorAction SilentlyContinue
|
|
if ($null -eq $task) { return [pscustomobject]@{ name = $script:TaskName; status = 'Unchanged'; message = 'Scheduled sync is not registered.' } }
|
|
Unregister-ScheduledTask -TaskName $script:TaskName -Confirm:$false
|
|
[pscustomobject]@{ name = $script:TaskName; status = 'Removed'; message = 'Scheduled sync was removed.' }
|
|
}
|
|
|
|
Export-ModuleMember -Function Register-ConfigCentralSchedule, Unregister-ConfigCentralSchedule
|