Scripts/New-EventStoreStartupTask.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function New-EventStoreStartupTask()
{
    [Cmdletbinding()]
    Param(
        [String] $taskname,
        [String] $dir
    )
   
    $ex = "start.cmd"

    If ( Get-ScheduledTask | Where-Object { $_.TaskName -eq $taskname } )
    {
        Write-Verbose ":: Unregister existing Task"
        Unregister-ScheduledTask $taskname -Confirm:$false
    }

    $trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30

    Write-Verbose ":: New-ScheduledTaskAction -Execute $ex -WorkingDirectory $dir"
    $action =  New-ScheduledTaskAction -Execute $ex -WorkingDirectory $dir
   
    $principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
    Write-Verbose ":: Register Scheduled Task"
    Register-ScheduledTask -TaskName $taskname -Trigger $trigger -Action $action -Principal $principal -ThrottleLimit 0

    Write-Host "!"

}

Export-ModuleMember 'New-EventStoreStartupTask'