Link to home
Start Free TrialLog in
Avatar of warriorfan808
warriorfan808

asked on

Scripts to shut down PC?

Hey there.  I was wondering if any of you knew a way I can have a workstation automatically shut down on Friday nights at 6pm?  Also, get it to prompt the user and give them a set amount of time to either deny the shut down if someone is using the workstation.  Links and tutorials on how to write this script will be great too.
Avatar of Kenneniah
Kenneniah

First download psshutdown from http://www.sysinternals.com/utilities/psshutdown.html
Extract it to something like C:\Pstools. You can put the file anywhere, just remember to change the path in the following command for scheduling it...
Then from a command prompt type:

schtasks /create /tn "Shutdown" /tr "c:\pstools\psshutdown.exe -c -f" /ru system /sc weekly /d FRI /st 18:00:00
By default psshutdown gives 20 seconds for the user to hit cancel. If you want to change it, add the -t option. For example for 60 seconds.....

schtasks /create /tn "Shutdown" /tr "c:\pstools\psshutdown.exe -c -f -t 60" /ru system /sc weekly /d FRI /st 18:00:00

To test the task after creating it just run....

schtasks /run /tn shutdown
There is a builtin command in Windows XP. It's called shutdown. Open a dos box and type shutdown /? for information.
A line such as this would inform the user the computer will shut down in 5 minutes.

shutdown -s -t 300 -c "This computer is scheduled to shutdown after 6:00. Save and close all documents to prevent data loss." -f

The problem with the built-in shutdown, is it does not work when scheduled to run under the System account, and a user is not logged in at the time.
Avatar of warriorfan808

ASKER

After you schedule it, how do you take it off?  The reason I"m asking is because I wanted to test it out.
schtasks /delete /tn shutdown
I just ran a test and it didn't work.

Here is what I did.

Downloaded the tools, extracted to

C:\Pstools\

went to the run command and typed:

schtasks /create /tn "Shutdown" /tr "c:\Pstools\psshutdown.exe -c -f -t 120" /ru system /sc weekly /d THURS /st 10:15:00

I was using my computer at the time.  Is this why it didn't shut down?
Also, is there a command to see all the scheduled tasks?
Was it 10am where you are or 10pm?
To see scheduled tasks, just run schtasks with no options.
Hmm you should have gotten an error message when you used your command. Are you running this from Start|Run or from a command prompt? If from Run, do it from a command prompt instead so you can see any error messages.

But the problem in your command is THURS, change it to THU
Thanks man.

I was looking on the site.

Where does schtasks/create /tn "Shutdown"/tr  come from?  Is that a Windows command

also what does

ru --I know u is for user, but ru?
sc - scheduled calendar?
st - scheduled time?

Where did you get all these?  Is there a place I can go so I can print them all out?
sorry forgot one other thing,

is it:

mon
tues
wed
thu
fri
sat
sun

??

do they have to be capitalized?
Schtasks is built into Windows.
/create - just means to make a new task
/tn - just makes up a name for the task, it can be anything you want
/tr - is the task to run
/ru - the user the task runs as, there is also a /u option to specify a user context to run under. For example if you used both switches, it would run under the /u users context, with the /ru users security privelages
/sc - just short for schedule, weekly, daily, monthly etc.
/st - yup, time to start the task.

You can just type schtasks /? in a command prompt to see help info also, but for some indepth stuff......

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true
Correct on the days of the week except for tuesday. All are 3 letter abbreviations, so "tue", and no capitalization doesn't matter.
In the command prompt after you've created a task just type schtasks and  it will list any scheduled tasks, and also tell you the next time it will run.
Just to make sure, would it work to create multiple shut down times?  I could probably just test it.  By the way, I did test the shutdown and it worked great.

What I mean by multiple shutdown times.

If the user clicks to cancel, then he will be prompted to shut down again at 7:00pm, 8:00pm, so on and so forth until either he walks away from the computer and lets it shut itself down or he shuts the machine down himself.

Is it easier to make multiple tasks or could it be done with one line.

If I created multiple tasks, would I just create different names?
tn "Shutdown1" /tr
tn "Shutdown2" /tr
tn "Shutdown3" /tr

etc.

Thanks a lot Kennenah, you're a lot of help.  Wish I could give you more than 500 points
Hmm, yeah that might not be a bad idea. Yep, just different names and times. I tried thinking of a way to do that with just one task, but can't think of any. An hourly schedule won't allow the /d option for day, so it would just keep running every hour until you deleted the task. So I then thought about using a scheduled task that on every friday creates an hourly scheduled task at 6:00 with a /sd and /ed using the %date% variable, but that would be a pain since you've had to edit the %date% variable in a batch file to make it in the right format. Hehe it probably could be done, but would end up being more difficult then just scheduling psshutdown at multiple times.

Most welcome, and points aren't a big deal. I'm just here to help and to give me something to do when I don't have a lot of work here :)
Cool.  You'll probably hear a lot from me.  I'm far below the levels of the guys on here.
Before I close this thread.  Have you run into any problems without using the f switch?  I mean, have there been hangs if the workstations haven't saved their work?  I would hate for someone to leave on their computer and then lose all their work.
ASKER CERTIFIED SOLUTION
Avatar of Kenneniah
Kenneniah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh awesome.  So you all came up with an estimated amount of time it should take to run all updates and installations and then forced the workstation to reboot, just in case it required a reboot?  I take it your'e using a few WSUS Servers?
We used to use SUS, and just set the setting for it to install updates at 3am and reboot if necessary. Now we use Shavlik for patch management, and Radia for pushing applications etc.
I never have used psshutdown in this manner, but have used it quite a few different situations.