为什么-ErrorAction'silentlycontinue'在脚本A上有效但在B上无效

时间:2019-11-07 21:20:24

标签: performance powershell scheduled-tasks

Update2:

我将脚本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 - 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'

运行脚本时,termal中的错误输出日志会显示此内容。

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:

原始帖子:

我有两个脚本,最终实现相同的目标是创建多个新的计划任务以在用户登录时运行。

脚本A不会像脚本B那样使用自定义变量和全局变量,下面的脚本A上的行也可以工作,但是脚本B上的同一行却不能使用,并且会产生STDOUT错误日志记录。错误日志令人烦恼,令人困惑,并且在已经完善(或与您的能力尽可能接近)的工作脚本中不需要。

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

脚本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"

脚本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"

0 个答案:

没有答案