
&"$PSScriptRoot\vars.ps1"

$files ="$ROOT\Scripts\Tasks\*.xml"

Get-Item $files | ForEach-Object {

    $name = $_.Name.Replace('.xml', '')

    Write-Host "File ..." $name
    
    $xml = Get-Content -Path $_.FullName | Out-String

    $taskExists = Get-ScheduledTask -TaskPath "\CEDAR\*" | Where-Object {$_.TaskName -eq $name }

    if ((-Not $taskExists) -Or ($taskExists.State -eq "Disabled")) {
        if ($taskExists) {
            Unregister-ScheduledTask -TaskPath "\CEDAR\" -TaskName $name -Confirm:$False > $null    
        }
        Register-ScheduledTask -xml $xml -TaskName $name -TaskPath "\CEDAR" > $null
    } else {
        Write-Host "Skipping"
    }
}
