Why does -ErrorAction 'silentlycontinue' work on script A but not B

0

Update2:

I changed script B to the following and the terminal shows two error output logs. Why would it choose both else statements at once?

##############################
######## CREATE TASKS ########
##############################
#
## ERROR STDOUT PREFERENCE ###
$ErrorActionPreference = 'silentlycontinue'
#
###### TIME-SPAN VALUE #######
$TimeSpan = New-TimeSpan -Minutes 3
#
##### WHEN TO RUN TASK #######
$Trigger = New-ScheduledTaskTrigger -AtLogOn
#
###### TASK OWNER INFO #######
$Principal = New-ScheduledTaskPrincipal -UserId "$Env:USERDOMAIN\$Env:USERNAME" -LogonType Interactive -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -Priority 0 -RestartCount 3 -RestartInterval $TimeSpan -ExecutionTimeLimit '00:00:00'
#
###### TASKS FOLDER NAME #####
$Folder = 'Start-Scripts'
#
######### TASKNAMES ##########
$TaskName1 = 'start_ahk_multiple_hotkeys'
$TaskName2 = 'start_ryzen_master'
$TaskName3 = 'start_corsair_link'
#
###### FOLDER + TASKNAME #####
$FullTask1 = "$Folder\$TaskName1"
$FullTask2 = "$Folder\$TaskName2"
$FullTask3 = "$Folder\$TaskName3"
#
############ AHK #############
$Action1 = New-ScheduledTaskAction -Execute "$Env:ProgramFiles\AHK\AutoHotkey.exe" -Argument "$Env:OneDrive\Documents\01_Scripts\02_AHK\Multiple_Hotkeys\Hotkeys_1.ahk"
#
########### RYZEN ############
$Action2 = New-ScheduledTaskAction -Execute "$Env:ProgramFiles\AMD\RyzenMaster\bin\AMD Ryzen Master.exe"
#
######## CorsairLink #########
$Action3 = New-ScheduledTaskAction -Execute "$Env:ProgramFiles(x86)\CorsairLink4\CorsairLink4.exe"
#
#########  IF EXISTS #########
if((Get-ScheduledTask -TaskName "$FullTask1" -ErrorAction 'silentlycontinue')){
   Write-Host "$FullTask1 - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask1" -InputObject (
  New-ScheduledTask -Action $Action1 -Principal $Principal -Trigger $Trigger -Settings $Settings
  )
  Write-Host "A task was created - $FullTask1"-ForegroundColor Yellow -BackgroundColor Black
}

########  IF EXISTS 2 ########
if((Get-ScheduledTask -TaskName "$FullTask2" -ErrorAction 'silentlycontinue')){
  Write-Host "$FullTask2 - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask2" -InputObject (
  New-ScheduledTask -Action $Action2 -Principal $Principal -Trigger $Trigger -Settings $Settings
  )
Write-Host "A task was created - $FullTask2"-ForegroundColor Yellow -BackgroundColor Black
}

########  IF EXISTS 3 ########
if((Get-ScheduledTask -TaskName "$FullTask3" -ErrorAction 'silentlycontinue')){
   Write-Host "$FullTask3 - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask3" -InputObject (
  New-ScheduledTask -Action $Action3 -Principal $Principal -Trigger $Trigger -Settings $Settings
  )
  Write-Host "A task was created - $FullTask3" -ForegroundColor Yellow -BackgroundColor Black
}

###### EXIT PARAMETERS #######
Read-Host -Prompt 'Press Enter to exit'

Error output log in termal shows this when script is run.

Register-ScheduledTask : Cannot create a file when that file already exists.
At C:\Users\jholl\OneDrive\Documents\01_Scripts\04_Task_Scheduler\Start_Programs\Scripts\1-Create_ALL_v3.ps1:45 char:1
+ Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask1" -InputObj ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x800700b7,Register-ScheduledTask

A task was created - Start-Scripts\start_ahk_multiple_hotkeys
Register-ScheduledTask : Cannot create a file when that file already exists.
At C:\Users\jholl\OneDrive\Documents\01_Scripts\04_Task_Scheduler\Start_Programs\Scripts\1-Create_ALL_v3.ps1:56 char:1
+ Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask2" -InputObj ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x800700b7,Register-ScheduledTask

A task was created - Start-Scripts\start_ryzen_master
Register-ScheduledTask : Cannot create a file when that file already exists.
At C:\Users\jholl\OneDrive\Documents\01_Scripts\04_Task_Scheduler\Start_Programs\Scripts\1-Create_ALL_v3.ps1:67 char:1
+ Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask3" -InputObj ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceExists: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x800700b7,Register-ScheduledTask

A task was created - Start-Scripts\start_corsair_link
Press Enter to exit:

Original Post:

I have two scripts which the same goal in the end achieve of creating multiple new scheduled tasks to run at user logon.

Script A does not utilize custom and global variables the way script B does and the line on script A below works but the same line on script B does not and produces STDOUT error logging. The error logs are annoying and confusing and unneeded in a working script that has already been perfected (or as close as your able).

if((Get-ScheduledTask -TaskName 'start_ahk_multiple_hotkeys' -ErrorAction 'silentlycontinue')){

Script A:

$ErrorActionPreference = 'silentlycontinue'
########################################
### Create AHK Multiple Hotkeys Task ###
########################################
# Set folder\taskname or just taskname by itself
$TaskName1 = 'Start-Scripts\start_ahk_multiple_hotkeys'
# File paths
$Action1 = New-ScheduledTaskAction `
-Execute 'C:\Program Files\AHK\AutoHotkey.exe' `
-Argument 'C:\Users\jholl\OneDrive\Documents\01_Scripts\02_AHK\Multiple_Hotkeys\Hotkeys_1.ahk'
# User info
$Principal1 = New-ScheduledTaskPrincipal `
-UserId 'JDESKTOP\jholl' `
-LogonType Interactive `
-RunLevel Highest
# When to run task
$Trigger1 = New-ScheduledTaskTrigger `
-AtLogOn
# Task settings
$TimeSpan1 = New-TimeSpan `
-Minutes 3
$Settings1 = New-ScheduledTaskSettingsSet `
 -StartWhenAvailable `
 -AllowStartIfOnBatteries `
 -DontStopIfGoingOnBatteries `
 -Priority 0 `
 -RestartCount 3 `
 -RestartInterval $TimeSpan1 `
 -ExecutionTimeLimit '00:00:00'

# If task already exists function

if((Get-ScheduledTask -TaskName 'start_ahk_multiple_hotkeys' -ErrorAction 'silentlycontinue')){
   Write-Host "$TaskName1" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$TaskName1" -InputObject (
    New-ScheduledTask -Action $Action1 `
    -Principal $Principal1 `
    -Trigger $Trigger1 `
    -Settings $Settings1
  )
   Write-Host 'Created A New Task Named' -NoNewline -ForegroundColor Yellow -BackgroundColor Black 
   Write-Host " - $TaskName1" -ForegroundColor Green -BackgroundColor Black
}

###########################
### Create Ryzen Master ###
###########################
# Set folder\taskname or just taskname by itself
$TaskName2 = 'Start-Scripts\start_ryzen_master'
# File paths
$Action2 = New-ScheduledTaskAction `
-Execute 'C:\Program Files\AMD\RyzenMaster\bin\AMD Ryzen Master.exe' `
# User info
$Principal2 = New-ScheduledTaskPrincipal `
-UserId 'JDESKTOP\jholl'`
-LogonType Interactive `
-RunLevel Highest
# When to run task
$Trigger2 = New-ScheduledTaskTrigger `
-AtLogOn
# Task settings
$TimeSpan2 = New-TimeSpan `
-Minutes 3
$Settings2 = New-ScheduledTaskSettingsSet `
 -StartWhenAvailable `
 -AllowStartIfOnBatteries `
 -DontStopIfGoingOnBatteries `
 -Priority 0 `
 -RestartCount 3 `
 -RestartInterval $TimeSpan2 `
 -ExecutionTimeLimit '00:00:00'

# If task already exists function
if((Get-ScheduledTask -TaskName 'start_ryzen_master' -ErrorAction 'silentlycontinue')){
   Write-Host "$TaskName2" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$TaskName2" -InputObject (
    New-ScheduledTask -Action $Action2 `
    -Principal $Principal2 `
    -Trigger $Trigger2 `
    -Settings $Settings2
  )
   Write-Host 'Created A New Task Named' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - $TaskName2" -ForegroundColor Green -BackgroundColor Black
}

##########################
### Create CorsairLink ###
##########################
# Set folder\taskname or just taskname by itself
$TaskName3 = 'Start-Scripts\start_corsair_link'
# File paths
$Action3 = New-ScheduledTaskAction `
-Execute 'C:\Program Files (x86)\CorsairLink4\CorsairLink4.exe' `
# User info
$Principal3 = New-ScheduledTaskPrincipal `
-UserId 'JDESKTOP\jholl' `
-LogonType Interactive `
-RunLevel Highest
# When to run task
$Trigger3 = New-ScheduledTaskTrigger `
-AtLogOn
# Task settings
$TimeSpan3 = New-TimeSpan `
-Minutes 3
$Settings3 = New-ScheduledTaskSettingsSet `
 -StartWhenAvailable `
 -AllowStartIfOnBatteries `
 -DontStopIfGoingOnBatteries `
 -Priority 0 `
 -RestartCount 3 `
 -RestartInterval $TimeSpan3 `
 -ExecutionTimeLimit '00:00:00'

# If task already exists function
if((Get-ScheduledTask -TaskName 'start_corsair_link' -ErrorAction 'silentlycontinue')){
   Write-Host "$TaskName3" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black

}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$TaskName3" -InputObject (
    New-ScheduledTask -Action $Action3 `
    -Principal $Principal3 `
    -Trigger $Trigger3 `
    -Settings $Settings3
  )
   Write-Host 'Created A New Task Named' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - $TaskName3" -ForegroundColor Green -BackgroundColor Black
}

Read-Host -Prompt "Press Enter to exit"

Script B:

##############################
######## CREATE TASKS ########
##############################
#
## ERROR STDOUT PREFERENCE ###
$ErrorActionPreference = 'silentlycontinue'
#
###### TIME-SPAN VALUE #######
$TimeSpan = New-TimeSpan `
-Minutes 3
#
##### WHEN TO RUN TASK #######
$Trigger = New-ScheduledTaskTrigger `
-AtLogOn
#
###### TASK OWNER INFO #######
$Principal = New-ScheduledTaskPrincipal `
-UserId "$Env:USERDOMAIN\$Env:USERNAME" `
-LogonType Interactive `
-RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet `
 -StartWhenAvailable `
 -AllowStartIfOnBatteries `
 -DontStopIfGoingOnBatteries `
 -Priority 0 `
 -RestartCount 3 `
 -RestartInterval $TimeSpan `
 -ExecutionTimeLimit '00:00:00'
#
###### TASKS FOLDER NAME #####
$Folder = 'Start-Scripts'
#
######### TASKNAMES ##########
$TaskName1 = 'start_ahk_multiple_hotkeys'
$TaskName2 = 'start_ryzen_master'
$TaskName3 = 'start_corsair_link'
#
###### FOLDER + TASKNAME #####
$FullTask1 = "$Folder\$TaskName1"
$FullTask2 = "$Folder\$TaskName2"
$FullTask3 = "$Folder\$TaskName3"
#
############ AHK #############
$Action1 = New-ScheduledTaskAction `
-Execute "$Env:ProgramFiles\AHK\AutoHotkey.exe" `
-Argument "$Env:OneDrive\Documents\01_Scripts\02_AHK\Multiple_Hotkeys\Hotkeys_1.ahk"
#
########### RYZEN ############
$Action2 = New-ScheduledTaskAction `
-Execute "$Env:ProgramFiles\AMD\RyzenMaster\bin\AMD Ryzen Master.exe"
#
##############################
######## CORSAIRLINK #########
##############################
$Action3 = New-ScheduledTaskAction `
-Execute "$Env:ProgramFiles(x86)\CorsairLink4\CorsairLink4.exe"
#
#########  IF EXISTS #########
if((Get-ScheduledTask -TaskName "$FullTask1" -ErrorAction 'silentlycontinue')){
   Write-Host "$FullTask1" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask1" -InputObject (
    New-ScheduledTask -Action $Action1 `
    -Principal $Principal `
    -Trigger $Trigger `
    -Settings $Settings
  )
   Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - $FullTask1" -ForegroundColor Green -BackgroundColor Black
}
#
########  IF EXISTS 2 ########
if((Get-ScheduledTask -TaskName "$FullTask2" -ErrorAction 'silentlycontinue')){
   Write-Host "$FullTask2" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask2" -InputObject (
    New-ScheduledTask -Action $Action2 `
    -Principal $Principal `
    -Trigger $Trigger `
    -Settings $Settings
  )
   Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - $FullTask2" -ForegroundColor Green -BackgroundColor Black
}
#
########  IF EXISTS 3 ########
if((Get-ScheduledTask -TaskName "$FullTask3" -ErrorAction 'silentlycontinue')){
   Write-Host "$FullTask3" -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - Already Exists" -ForegroundColor Red -BackgroundColor Black
}
else{
Register-ScheduledTask -TaskPath '\' -TaskName "$FullTask3" -InputObject (
    New-ScheduledTask -Action $Action3 `
    -Principal $Principal `
    -Trigger $Trigger `
    -Settings $Settings
  )
   Write-Host 'A task was created -' -NoNewline -ForegroundColor Yellow -BackgroundColor Black
   Write-Host " - $FullTask3" -ForegroundColor Green -BackgroundColor Black
}
#
###### EXIT PARAMETERS #######
Read-Host -Prompt "Press Enter to exit"
performance
powershell
scheduled-tasks
asked on Stack Overflow Nov 7, 2019 by slyfox1186 • edited Nov 7, 2019 by slyfox1186

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0