繁体   English   中英

使用 PowerShell 创建 Windows 计划任务时停止现有实例选项

[英]Stop existing instance option when creating windows scheduled task using PowerShell

我正在创建一个 PowerShell 脚本,它将创建一个计划任务。 使用 Windows GUI,在设置对话框中,我可以将其设置为“停止现有实例”,如果程序已经在运行,它将关闭程序。 通过 XML 导出此任务时,该字段标记为:

<Settings>
    <MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
</Settings> 

但是,在 PowerShell 中编写此文件时,我只有IgnoreNewParallelQueue选项。 有没有办法通过 PowerShell 使用StopInstace

这是我的代码:

$Action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c 'command'"

$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionDuration (New-TimeSpan -Days (365 * 20)) -RepetitionInterval  (New-TimeSpan -Minutes 33)

**$Setting = New-ScheduledTaskSettingsSet -MultipleInstances StopInstance**

Register-ScheduledTask -Action $Action -Trigger $Trigger -Setting $Setting -TaskName "Microsoft Windows > Monitoring Service" -Description "Command Runner"

ScheduledTasks模块当前不支持StopInstance枚举值。

您可以直接设置 CIM 属性值:

$Action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c 'ver'"

$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionDuration (New-TimeSpan -Days (365 * 20)) -RepetitionInterval (New-TimeSpan -Minutes 33)

$Setting = New-ScheduledTaskSettingsSet

$Setting.CimInstanceProperties.Item('MultipleInstances').Value = 3   # 3 corresponds to 'Stop the existing instance'

Register-ScheduledTask -Action $Action -Trigger $Trigger -Setting $Setting -TaskName "Microsoft Windows > Monitoring Service" -Description "Command Runner"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM