F11 Functie Installeren Script bestanden en schedulers

Categories: IT, Scripting

Een functie om een script gepland te starten vanuit de task scheduler in Windows.

######################################################################################
# F11 Functie Installeren Script bestanden en schedulers
######################################################################################
Function InstallScheduler
(
    [string]$customer
) {
    $sch_Trigger1 = New-ScheduledTaskTrigger -At 19:45 -Daily # Specify the trigger settings
    $sch_Trigger2 = New-ScheduledTaskTrigger -At 20:00 -Daily # Specify the trigger settings

    $sch_User = "NT AUTHORITY\SYSTEM" # Specify the account to run the script
    $sch_Action1 = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "'$PSSCRIPTroot\SCRIPT.ps1 -parameters" # update script environment

    if ($customer) {
        $sch_Action2 = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "$PSSCRIPTroot\SCRIPT.ps1 -parameters $customer" -WorkingDirectory $PSSCRIPTroot # Run 
    }
    else {
        $sch_Action2 = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "$PSSCRIPTroot\SCRIPT.ps1" -WorkingDirectory $PSSCRIPTroot # Run
    }
    Unregister-ScheduledTask -TaskName "DataGather Update" -Confirm:$False
    Unregister-ScheduledTask -TaskName "DataGather" -Confirm:$False

    Register-ScheduledTask -TaskName "DataGather Update" -Trigger $sch_Trigger1 -User $sch_User -Action $sch_Action1 -RunLevel Highest -Force 
    Register-ScheduledTask -TaskName "DataGather" -Trigger $sch_Trigger2 -User $sch_User -Action $sch_Action2 -RunLevel Highest -Force 
}
«
»