允许用户推迟计划任务

问题描述 投票:0回答:1

我需要允许用户推迟重新启动计算机的计划任务。

这是我用于重新启动任务的脚本,我不确定如何提供推迟 30 分钟的选项。我希望每 30 分钟显示一个对话框,直到它们重新启动。 `

    # Create task action
    $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument 'Restart-Computer -Force'
    # Create a trigger (wednesday at 9:00 AM)
    $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Wednesday -At 9:00am
    # The user to run the task
    $taskUser = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount - RunLevel Highest
    # The name of the scheduled task.
    $taskName = "Weekly Reboot"
    # Describe the scheduled task.
    $description = "Forcibly reboot the computer at 9:00am on wednesday"
    # set condition to wake computer to run task
    $settingset = New-ScheduledTaskSettingsSet -waketorun
    # Register the scheduled task
    Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal   $taskUser -Description $description -Settings $settingset
    $shellObject = New-Object -ComObject Wscript.Shell
    $notification = $shellObject.Popup("Computer will reboot in 30 minutes",0,"Reboot Notification")
powershell scheduled-tasks
1个回答
0
投票

您的脚本应该创建一个计划任务...每 30 分钟显示一条消息的逻辑必须由计划任务执行,而不是由创建它的脚本执行。

为了实现您的目标,您需要修改脚本以执行如下所示的 Powershell 代码(示例):

while (((New-Object -ComObject Wscript.Shell).Popup("Computer will reboot in 30 minutes",0,"Reboot Notification",1)) -gt 1) {
    Start-Sleep -Seconds 1800
}
Restart-Computer -Force

如果您希望计算机在 30 分钟后没有用户交互的情况下自动重新启动,只需更改弹出参数如下:

...Popup("计算机将在 30 分钟后重启",1800,"重启通知",1)

此外,如果您使用 SYSTEM 帐户执行任务,它将无法与任何登录用户的桌面进行交互。 因此,您需要在登录用户的上下文中执行它。按如下方式修改您的脚本:

From this:
    $taskUser = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest

To this:
    $taskUser = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Users" -RunLevel Highest

最后,出于多种原因,使用编码命令来配置任务会更容易。只需创建一个脚本块并对其进行编码即可:

$RebootCommand = [scriptblock]::Create('while (((New-Object -ComObject Wscript.Shell).Popup("Computer will reboot in 30 minutes",0,"Reboot Notification",1)) -gt 1) { Start-Sleep -Seconds 1800 } Restart-Computer -Force')
$BytesCommand  = [System.Text.Encoding]::Unicode.GetBytes($RebootCommand)
$EncodeCommand = [Convert]::ToBase64String($BytesCommand)

然后,您使用 $EncodeCommand 变量来创建计划任务:

$taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-WindowStyle Hidden -EncodedCommand $EncodeCommand"

您的最终脚本将如下所示:

# Prepare Encoded Command
    $RebootCommand = [scriptblock]::Create('while (((New-Object -ComObject Wscript.Shell).Popup("Computer will reboot in 30 minutes",0,"Reboot Notification",1)) -gt 1) { Start-Sleep -Seconds 1800 } Restart-Computer -Force')
    $BytesCommand  = [System.Text.Encoding]::Unicode.GetBytes($RebootCommand)
    $EncodeCommand = [Convert]::ToBase64String($BytesCommand)

# Create task action
    $taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-WindowStyle Hidden -EncodedCommand $EncodeCommand"
# Create a trigger (wednesday at 9:00 AM)
    $taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Wednesday -At 9:00am
# The user to run the task
    $taskUser = New-ScheduledTaskPrincipal -GroupId "BUILTIN\Users" -RunLevel Highest
# The name of the scheduled task.
    $taskName = "Weekly Reboot"
# Describe the scheduled task.
    $description = "Forcibly reboot the computer at 9:00am on wednesday"
# set condition to wake computer to run task
    $settingset = New-ScheduledTaskSettingsSet -waketorun
# Register the scheduled task
    Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Principal $taskUser -Description $description -Settings $settingset

希望这能帮助您实现目标。

© www.soinside.com 2019 - 2024. All rights reserved.