slyfox1186

Create scheduled tasks to run exe's at logon powershell Ver2

Nov 7th, 2019
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##############################
  2. ######## CREATE TASKS ########
  3. ##############################
  4. #
  5. ## ERROR STDOUT PREFERENCE ###
  6. $ErrorActionPreference = 'silentlycontinue'
  7. #
  8. ###### TIME-SPAN VALUE #######
  9. $TimeSpan = New-TimeSpan `
  10. -Minutes 3
  11. #
  12. ##### WHEN TO RUN TASK #######
  13. $Trigger = New-ScheduledTaskTrigger `
  14. -AtLogOn
  15. #
  16. ###### TASK OWNER INFO #######
  17. $Principal = New-ScheduledTaskPrincipal `
  18. -UserId "$Env:USERDOMAIN\$Env:USERNAME" `
  19. -LogonType Interactive `
  20. -RunLevel Highest
  21. $Settings = New-ScheduledTaskSettingsSet `
  22.  -StartWhenAvailable `
  23.  -AllowStartIfOnBatteries `
  24.  -DontStopIfGoingOnBatteries `
  25.  -Priority 0 `
  26.  -RestartCount 3 `
  27.  -RestartInterval $TimeSpan `
  28.  -ExecutionTimeLimit '00:00:00'
  29. #
  30. ###### TASKS FOLDER NAME #####
  31. $Folder = 'Start-Scripts'
  32. #
  33. ######### TASKNAMES ##########
  34. $TaskName1 = 'start_ahk_multiple_hotkeys'
  35. $TaskName2 = 'start_ryzen_master'
  36. $TaskName3 = 'start_corsair_link'
  37. #
  38. ###### FOLDER + TASKNAME #####
  39. $FullTask1 = "$Folder\$TaskName1"
  40. $FullTask2 = "$Folder\$TaskName2"
  41. $FullTask3 = "$Folder\$TaskName3"
  42. #
  43. ############ AHK #############
  44. $Action1 = New-ScheduledTaskAction `
  45. -Execute "$Env:ProgramFiles\AHK\AutoHotkey.exe" `
  46. -Argument "$Env:OneDrive\Documents\01_Scripts\02_AHK\Multiple_Hotkeys\Hotkeys_1.ahk"
  47. #
  48. ########### RYZEN ############
  49. $Action2 = New-ScheduledTaskAction `
  50. -Execute "$Env:ProgramFiles\AMD\RyzenMaster\bin\AMD Ryzen Master.exe"
  51. #
  52. ##############################
  53. ######## CORSAIRLINK #########
  54. ##############################
  55. $Action3 = New-ScheduledTaskAction `
  56. -Execute "$Env:ProgramFiles(x86)\CorsairLink4\CorsairLink4.exe"
  57. #
  58. #########  IF EXISTS #########
  59. if((Get-ScheduledTask -TaskName "$FullTask1")){
  60.    Write-Host "$FullTask1" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  61.    Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
  62. }
  63. else{
  64. Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask1" -InputObject (
  65.     New-ScheduledTask -Action $Action1 `
  66.     -Principal $Principal `
  67.     -Trigger $Trigger `
  68.     -Settings $Settings
  69.   )
  70.    Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  71.    Write-Host " - $FullTask1" -ForegroundColor Green -BackgroundColor Black
  72. }
  73. #
  74. ########  IF EXISTS 2 ########
  75. if((Get-ScheduledTask -TaskName "$FullTask2")){
  76.    Write-Host "$FullTask2" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  77.    Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
  78. }
  79. else{
  80. Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask2" -InputObject (
  81.     New-ScheduledTask -Action $Action2 `
  82.     -Principal $Principal `
  83.     -Trigger $Trigger `
  84.     -Settings $Settings
  85.   )
  86.    Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  87.    Write-Host " - $FullTask2" -ForegroundColor Green -BackgroundColor Black
  88. }
  89. #
  90. ########  IF EXISTS 3 ########
  91. if((Get-ScheduledTask -TaskName "$FullTask3")){
  92.    Write-Host "$FullTask3" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  93.    Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
  94. }
  95. else{
  96. Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask3" -InputObject (
  97.     New-ScheduledTask -Action $Action3 `
  98.     -Principal $Principal `
  99.     -Trigger $Trigger `
  100.     -Settings $Settings
  101.   )
  102.    Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
  103.    Write-Host " - $FullTask3" -ForegroundColor Green -BackgroundColor Black
  104. }
  105. #
  106. ###### EXIT PARAMETERS #######
  107. Read-Host -Prompt "Press Enter to exit"
Advertisement
Add Comment
Please, Sign In to add comment