$rootDrive = (get-location).Drive.Name + ":"
$pathDeployments = Join-Path -Path "$rootDrive" -ChildPath "Deployments"

Try
{
    Write-Host -ForegroundColor yellow "`n--- Scheduled Tasks ---`n"

    $service = new-object -ComObject("Schedule.Service")
    $service.Connect()
    $rootFolder = $service.GetFolder("\")
    $scheduledTasksPath = "$pathDeployments\ScheduledTasks"

    # launch (to run at user login)
    Write-Host "Creating launch task..." -NoNewline
    $task_name = "AA Genie Display App - Launch"
    $description = "Launches the Resident Display application on startup."
    $execute = "$pathDeployments\Install\Utility\VBScript\launch.vbs"
    $user = "$env:COMPUTERNAME\$env:USERNAME"
    $settings = New-ScheduledTaskSettingsSet -Hidden
    $get_task = Get-ScheduledTask $task_name -ErrorAction SilentlyContinue
    if (!$get_task) {
        $action = New-ScheduledTaskAction -Execute $execute
        $trigger = New-ScheduledTaskTrigger -AtLogon -User $user
        Register-ScheduledTask -Action $action -Trigger $trigger -RunLevel Highest -TaskName $task_name -Description $description -Settings $settings | Out-Null
    }
    Write-Host "done."

    # Windows restart
	Write-Host "Creating Windows Restart task..." -NoNewline
    $task_name = "AA Genie - Restart Windows"
    $description = "Restarts Windows on a scheduled basis."
    $execute =  "$scheduledTasksPath\RestartWindows.bat"
    $user = "$env:COMPUTERNAME\$env:USERNAME"
    $start_time = "03:00"

    $get_task = Get-ScheduledTask $task_name -ErrorAction SilentlyContinue
    if (!$get_task) {

        $action = New-ScheduledTaskAction -Execute $execute
        $trigger = New-ScheduledTaskTrigger -Daily -At "03:00"
        $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest

        Register-ScheduledTask -TaskName $task_name -Action $action -Trigger $trigger -Description $description -Principal $principal | Out-Null
    }
    Write-Host "done."

}
Catch
{
    throw $_.Exception.Message
}