如果用户未登录,则用于重新启动的 Windows 任务 [英] Windows Task for restart if user not logged in

查看:51
本文介绍了如果用户未登录,则用于重新启动的 Windows 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Windows 重启创建计划任务,如果任何用户未登录,就会执行该任务.如果用户在本地或通过 RDP 登录,则不应重新启动计算机.

对于无条件重启,我在任务计划程序中有任务:操作:启动程序",程序/脚本:powershell",添加参数:restart-computer -force"

对于有条件重启,我有 powershell 代码:

$ErrorActionPreference = "继续"$userstatus = quser/server:'localhost'如果(!$用户状态){重启-电脑-力}别的{出口}

当我将脚本另存为 .ps1 文件并将文件路径作为参数添加到计划时,我可以让条件重启工作正常.我需要有几个桌面的条件重启,我想避免为每台机器在本地保存 .ps1 文件.是否可以将整个脚本作为参数传递?

解决方案

你的逻辑有点不对.如果您计划为每台 PC 创建计划任务,它应该如下所示:

$Users = quser.exe如果 ($Users -match 'No\sUser') { Restart-Computer -Force }

从这里开始,由于语法的原因,您应该对其进行编码,然后您可以使用 schtasks.exe 来创建您的任务:

[转换]::ToBase64String([Text.Encoding]::Unicode.GetBytes("If(@(quser.exe) -match 'no\suser'){Restart-Computer -Force}"))

输出:

<块引用>

SQBmACAAKABAACgAcQB1AHMAZQByAC4AZQB4AGUAKQAgAC0AbQBhAHQAYwBoACAAJwBuAG8AXABzAHUAcwBlAHIAJwApAHsAUgBlAHMAdABhAHIAdAAtAEMAbwBtAHAAdQB0AGUABvAgA

 <代码> SchTasks.exe会/创建/TN AUTORESTART/SC DAILY/ST 20:00/RU SYSTEM/TR powershell.exe -EncodedCommand SQBmACAAKABAACgAcQB1AHMAZQByAC4AZQB4AGUAKQAgAC0AbQBhAHQAYwBoACAAJwBuAG8AXABzAHUAcwBlAHIAJwApAHsAUgBlAHMAdABhAHIAdAAtAEMAbwBtAHAAdQB0AGUAcgAgAC0ARgBvAHIAYwBlAH0A"

I'm trying to create scheduled task for Windows reboot, which is executed if any user is not logged in. If user is logged in locally or over RDP, the machine should not be restarted.

For unconditional restart, I have task in Task Scheduler with: Action: "Start a program", Program/script: "powershell", Add arguments: "restart-computer -force"

For conditional restart I have powershell code:

$ErrorActionPreference = "Continue"
$userstatus = quser /server:'localhost'
if(!$userstatus){
    Restart-Computer -Force
    }
else{
    exit
    }

I can get the conditional restart working fine when I save the script as .ps1 file and add the filepath to the schedule as argument. I need the conditional restart with several desktops and I would like to avoid saving the .ps1 file locally for each machine. Is it possible to pass the entire script as argument?

解决方案

Your logic's a bit off. If you plan on creating the scheduled task for each PC, it should look something like this:

$Users = quser.exe
If ($Users -match 'No\sUser') { Restart-Computer -Force }

From here, because of the syntax, you should encode it, then you could utilize schtasks.exe to create your task:

[Convert]::ToBase64String(
    [Text.Encoding]::Unicode.GetBytes(
        "If(@(quser.exe) -match 'no\suser'){Restart-Computer -Force}"
    )
)

Output:

SQBmACAAKABAACgAcQB1AHMAZQByAC4AZQB4AGUAKQAgAC0AbQBhAHQAYwBoACAAJwBuAG8AXABzAHUAcwBlAHIAJwApAHsAUgBlAHMAdABhAHIAdAAtAEMAbwBtAHAAdQB0AGUAcgAgAC0ARgBvAHIAYwBlAH0A

schtasks.exe /Create /TN AutoRestart /SC DAILY /ST 20:00 /RU SYSTEM /TR "powershell.exe -EncodedCommand SQBmACAAKABAACgAcQB1AHMAZQByAC4AZQB4AGUAKQAgAC0AbQBhAHQAYwBoACAAJwBuAG8AXABzAHUAcwBlAHIAJwApAHsAUgBlAHMAdABhAHIAdAAtAEMAbwBtAHAAdQB0AGUAcgAgAC0ARgBvAHIAYwBlAH0A"

这篇关于如果用户未登录,则用于重新启动的 Windows 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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