Working on a script to find servers w/ a lastboottime > 30 days. Once its identified those its queries for active users and does 1 of 2 things. 1) If active creates a task for 1am the following day to reboot or 2) If not active creates a task for 1 minute from the current time and reboots right away. What im struggling with is how to not leave a left over task on the server. For the task that reboots right away it seems like just using start-sleep gets the job done just fine BUT when I need to schedule it for 1am the following day I cant let it sleep for hours on end while it waits for that time to be reached. Is there any other way to unregister a task only after its been executed without relying on time? Heres my setup so far
For no User, if you dont want to leave a task, why create one? Can you simply use shutdown.exe? I would also propose the same for the user found as well, just adjust /t based on some time math ?? I am guessing maybe shutdown.exe may not survive the end of the script? I have not tried/tested.
[quote quote=209547]For no User, if you dont want to leave a task, why create one? Can you simply use shutdown.exe? I would also propose the same for the user found as well, just adjust /t based on some time math ?? I am guessing maybe shutdown.exe may not survive the end of the script? I have not tried/tested.
[/quote]
I wish. No ability to reboot a server directly because my permissions are restrictive af. This was the workaround I came up with. Ive tested it for no active user and even after unregistering the task it still executes the shutdown because of that argument /t 300 so at least I know that is working.
&& executes the second item if the first succeeds. You might also simply try & which wont wait.
I have not tested this, it is 100% theory/guessing. You may need to mess with quotes here and there.
Let me know the results, I am curious if you can nuke the task you are running from. My guess is not, but maybe worth a try? I guess if you have permissions to create the task, you can also delete?
So basically launching cmd to execute the shutdown and once that completes && cleans it up. CMD for the win thank you for the tip! Ive tested and this worked great but had to tweak a few things. Final -execute looks like
needed to be added as an argument for it to run properly BUT the -argument parameter isnt passing it to the argument field in tasksched. When it worked earlier I had configured by hand (pic below). So it works but CMD is including the argument in the program/script field which throws errors about invalid parameters. Just need to figure out how to pass those right and it should be good.