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?