Last active
September 26, 2020 20:30
-
-
Save glekner/1e783467c0cd37df1ee90c98e5ec91b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$taskName = "WSL2_PORTS" | |
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue | |
if ($null -ne $task) { | |
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false | |
} | |
# TODO: Enter Path to script, change delay if you will. | |
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument '-File "C:\Path\To\Script.ps1"' | |
$trigger = New-ScheduledTaskTrigger -AtStartup -RandomDelay 00:00:30 | |
$settings = New-ScheduledTaskSettingsSet -Compatibility Win8 | |
$principal = New-ScheduledTaskPrincipal -UserId SYSTEM -LogonType ServiceAccount -RunLevel Highest | |
$definition = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings -Description "Run $($taskName) at startup" | |
Register-ScheduledTask -TaskName $taskName -InputObject $definition | |
$task = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue | |
# TODO: LOG AS NEEDED... | |
if ($null -ne $task) { | |
Write-Output "Created scheduled task: '$($task.ToString())'." | |
} | |
else { | |
Write-Output "Created scheduled task: FAILED." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment