我希望使用Powershell设置"如果任务已经运行,则适用以下规则:到#34;不要启动新实例"
我的powershell脚本如下所示:
$Task = Get-ScheduledTask -TaskName "MyJob"
$Task.Settings.MultipleInstances= "StopExisting"
Set-ScheduledTask -User "USER" -Password "PASS" $Task
我收到错误:
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""
当我将$ Task.Settings.MultipleInstances设置为Parallel,Queue和Ignorenew时,它可以工作。只是" StopExisting"失败。
来源:https://msdn.microsoft.com/en-us/library/windows/desktop/aa383507(v=vs.85).aspx
我查看了StackOverflow,发现了类似的问题:Powershell New-ScheduledTaskSettingsSet
我将脚本更新为:
$Task = Get-ScheduledTask -TaskName "MyJob"
$StopExisting = New-ScheduledTaskSettingsSet
$StopExisting.CimInstanceProperties['MultipleInstances'].Value = 3
$Task.Settings.MultipleInstances= $StopExisting
Set-ScheduledTask -User "USER" -Password "PASS" $Task
现在我得到了不同的错误:
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
知道我在这里做错了什么,怎么解决这个问题?
全部谢谢。