PowerShell unable to use enum MultipleInstances

心不动则不痛 提交于 2020-01-30 11:38:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!