问题
I am looking to use Powershell to set "if the task is already running, then the following rules applies: to "Do not start a new instance"
my powershell script looks like below:
$Task = Get-ScheduledTask -TaskName "MyJob"
$Task.Settings.MultipleInstances= "StopExisting"
Set-ScheduledTask -User "USER" -Password "PASS" $Task
I get error as :
Exception setting "MultipleInstances": "Cannot convert value "StopExisting" to type
"Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.MultipleInstancesEnum". Error: "Unable to match the
identifier name StopExisting to a valid enumerator name. Specify one of the following enumerator names and try again:
Parallel, Queue, IgnoreNew""
When I set $Task.Settings.MultipleInstances to Parallel, Queue and Ignorenew , it works. Just "StopExisting" fails.
Source: https://msdn.microsoft.com/en-us/library/windows/desktop/aa383507(v=vs.85).aspx
I looked over the StackOverflow and found similar problem: Powershell New-ScheduledTaskSettingsSet
I updated my script as :
$Task = Get-ScheduledTask -TaskName "MyJob"
$StopExisting = New-ScheduledTaskSettingsSet
$StopExisting.CimInstanceProperties['MultipleInstances'].Value = 3
$Task.Settings.MultipleInstances= $StopExisting
Set-ScheduledTask -User "USER" -Password "PASS" $Task
Now I get different error as:
Exception setting "MultipleInstances": "Cannot convert the "MSFT_TaskSettings3" value of type
"Microsoft.Management.Infrastructure.CimInstance#Root/Microsoft/Windows/TaskScheduler/MSFT_TaskSettings3" to type
"Microsoft.PowerShell.Cmdletization.GeneratedTypes.ScheduledTask.MultipleInstancesEnum"."
At C:\temp\PythonService\WatchListPowerShell.ps1:13 char:1
+ $Task.Settings.MultipleInstances= $StopExisting
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ScriptSetValueRuntimeException
Any idea what am I doing wrong here and how can I solve this?
Thanks All.
来源:https://stackoverflow.com/questions/44578212/powershell-unable-to-use-enum-multipleinstances