Powershell v4.创建远程任务调度程序任务集过期和删除 [英] Powershell v4. Create remote task scheduler task set to expire and delete

查看:51
本文介绍了Powershell v4.创建远程任务调度程序任务集过期和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个基本上重新启动服务器的任务,但该任务由远程服务器放置在那里,运行检查以查看是否需要重新启动.我在尝试添加到期时遇到困难,因此它会自行删除,但找不到该设置的放置位置或它是什么.它与结束边界或其他设置有关 -DeleteExpiredTaskAfter 但不知道要放入什么值.

I am trying to create a task that essentially reboots the server but the task is put there by a remote server running a check to see if a reboot is needed. I am stuck trying to add an expiration so it deletes itself but can't find where to put that setting or what it is. It has to do with an end boundry or something and this setting -DeleteExpiredTaskAfter but don't know what value to put in.

$dc = "server2reboot"
$taskname = "Reboot $DC"
$taskpath  = "PendingReboots"
$CimSession = New-CimSession -ComputerName $dc -Credential $credentials -Authentication Negotiate

Function Create-AndRegisterRebootTask{
 Param ($taskname, $taskpath)
 $action = New-ScheduledTaskAction -Execute '#shutdown.exe -r -f -t 0"'
 $trigger =  New-ScheduledTaskTrigger -once -At ("$nextsundaydate 3:00") -RandomDelay 03:00:00 
 Register-ScheduledTask -CimSession $cimsession -RunLevel Highest  -Action $action -Trigger $trigger -TaskName $taskname -Description "Server Reboot" -TaskPath $taskpath -Force
 }



Function Create-NewRebootTaskSettings{
 Param ($taskname, $taskpath)
 $settings = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter "PT0S" -compatability "win8" -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit "PT1H" -RestartCount 3
 Set-ScheduledTask -CimSession $cimsession -TaskName $taskname -Settings $settings -TaskPath $taskpath
 }

Create-AndRegisterRebootTask -taskname $taskname -taskpath $taskpath 
Create-NewRebootTaskSettings -taskname $taskname -taskpath $taskpath 

Set-ScheduledTask : The task XML is missing a required element or attribute.
(48,4):EndBoundary:
At line:5 char:2
+  Set-ScheduledTask -CimSession $cimsession -TaskName $taskname -Settings $settin ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Set-ScheduledTask], CimException
    + FullyQualifiedErrorId : HRESULT 0x80041319,Set-ScheduledTask

推荐答案

Craig Duff,这里是如何创建一个在没有兼容性标志的情况下运行并使用 PS4.0 cmdlet 后被删除的任务:

With some help from Craig Duff, here is how you can create a task that is deleted after being run without the compatibility flag and using the PS4.0 cmdlets:

$run = (Get-Date).AddMinutes(2) # Two minutes from now
Register-ScheduledTask -TaskName "MyTask"  -User "Domain\User" -InputObject (
  (
    New-ScheduledTask -Action (
      New-ScheduledTaskAction -Execute "C:\path\to\your.exe" -Argument (
        "many" + 
        "arguments " +
        """with quotes"" "
      )
    ) -Trigger (
      New-ScheduledTaskTrigger -Once -At ($run.TimeOfDay.ToString("hh\:mm")) # As a "TimeOfDay" to get 24Hr format
    ) -Settings (
      New-ScheduledTaskSettingsSet  -DeleteExpiredTaskAfter 00:00:01 # Delete one second after trigger expires
    ) 
  ) | %{ $_.Triggers[0].EndBoundary = $run.AddMinutes(60).ToString('s') ; $_ } # Run through a pipe to set the end boundary of the trigger
)

问题是触发器必须定义一个 EndBoundary 以便可以删除任务.New-ScheduledTaskTrigger cmdlet 没有用于定义它的参数.诀窍是创建任务并在两个不同的步骤中注册它,中间有一个步骤来定义触发器的结束边界.显然,如果您的任务有多个触发器,则需要为每个触发器设置结束边界.

The thing is that the trigger must have an EndBoundary defined so that the task can be deleted. The New-ScheduledTaskTrigger cmdlet doesn't have a parameter to define it. The trick is then to create the task and register it in two different steps, with a step in between to define the End Boundary of the trigger. Obviously if your task has more than one trigger, you would need to set the End Boundary for each.

此特定示例创建了一个任务,该任务将运行 c:\path\to\your.exe 一次调用 MyTask 两分钟后,使用凭据运行 >域\用户.触发器将在执行开始后一小时到期(以便有人可以验证它是否从计划任务窗口运行,而不是浏览 Windows 日志),然后该任务将在此后一秒被删除.

This specific example creates a task that will run c:\path\to\your.exe once called MyTask two minutes into the future, running with credentials Domain\User. The trigger will expire an hour after the execution start (just so someone could verify if it was run from the scheduled tasks window, instead of browsing through the windows logs), and the task will be deleted one second after that.

这篇关于Powershell v4.创建远程任务调度程序任务集过期和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆