Reboot script powershell for terminal servers W2K8R2

Hi,

We 'd need to make a reboot schedule/script for two terminal servers without a broker.

This is the idea: two terminal servers: x and y

* x should reboot every other day at 5 PM, f.e. Monday, Wednesday
* y should reboot the days in between at 5 PM, f.e. Tuesday, Thursday

*reminder (removable) should be given to users that server will be rebooted: "This server will be rebooted within x hours, please logoff by then".
*permanent reminder (not removable) should be given within the last hour: "This server will be rebooted in x hour/min (counting down clock), please logoff or there could be data loss. You can logon to server x/y and continue your work."
*any changes to config should be easily made (change in days, time) ... => GUI?

We'd use a chglogon /drainuntilrestart to make sure no new users logon.


Please advise on concept (how do you use/implement it). I'd prefer Powershell.

J.
janhoedtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joharderCommented:
Were you planning to configure this as a scheduled task?  That will provide the ease and flexibility you're seeking.
0
David Johnson, CD, MVPOwnerCommented:
scheduled tasks would be the best

A nice countdown timer is located @ http://gallery.technet.microsoft.com/scriptcenter/Countdown-Timer-06ae1ce7
0
yo_beeDirector of Information TechnologyCommented:
I would just create a new Task under Task Scheduler and use the Shutdown /r /f /t 3600  /c "Your message" /d p:2:18 to schedule your shutdowns.

I am pretty sure that the shutdown command will alert the users that the server is being shutdown/rebooted.

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2013-01-24T16:26:21.3941223</Date>
    <Author>Domain\User</Author>
  </RegistrationInfo>
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2013-01-25T03:00:00</StartBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>Domain\user</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>shutdown</Command>
      <Arguments>/r /t 00 /d p:2:18</Arguments>
    </Exec>
  </Actions>
</Task>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

janhoedtAuthor Commented:
Thanks but I'd need to able to deploy/change the settings centrally.
0
janhoedtAuthor Commented:
Let me clarify, it would be something like this:

Show default a popup with reboot schedules for the machine, f.e. user logs on to machine x = This server will be rebooted 1 September at 7 PM

enter-pssession remotemachine
'message 4 hours before reboot that server will be rebooted (for all users)
'message 1 hours before reboot that server will be rebooted (for all users)
'message 30 minutes before reboot that server will be rebooted (for all users)
msg * /v "This server will be restarted in x minutes. Please logoff and work on server y."
Countdown timer to restart then
chglogon /drainuntinrestart  'to not allow users to logon anymore until restart but leave connected users connected

'query open terminal server sessions and logg them off
reboot terminal server
0
joharderCommented:
In order to meet your requirement to do this centrally, you can set this up as a scheduled task within a GPO, as a Group Policy Preference.

Go to Computer-->Preferences-->Control Panel-->Scheduled Task.
0
janhoedtAuthor Commented:
Ok thanks, but that doesn't help me with the script. The script is what I actually need to configure/make work.
0
yo_beeDirector of Information TechnologyCommented:
You can do that or just go to each one of your TS and add the script to run at a scheduled time.
Either way will meet your needs, but you will need to make sure that the script is accessible to all servers.
0
janhoedtAuthor Commented:
Ok, but I don't need help with targetting, I need help with the script itself.
0
yo_beeDirector of Information TechnologyCommented:
If you send the Shutdown Command to the server all sessions will get the alert.

Try it on your machine.

Shutdown /r /t 300 /c "the server is going to shutdown in 5 Mins please save all work" /d p:2:18
Once run you can abort it by entering a new command shutdown /a

Once you confirmed this I would schedule it on the servers.
0
janhoedtAuthor Commented:
Reboot command isn't the issue, is the full powershell script what I'm looking for, specifically the timer and the logoff of logged on users.

enter-pssession remotemachine
'message 4 hours before reboot that server will be rebooted (for all users)
'message 1 hours before reboot that server will be rebooted (for all users)
'message 30 minutes before reboot that server will be rebooted (for all users)
msg * /v "This server will be restarted in x minutes. Please logoff and work on server y."
Countdown timer to restart then
chglogon /drainuntinrestart  'to not allow users to logon anymore until restart but leave connected users connected

'query open terminal server sessions and logg them off
reboot terminal server
0
yo_beeDirector of Information TechnologyCommented:
So you have the timer PS script from one of the other posters correct?
0
janhoedtAuthor Commented:
Yes.
0
yo_beeDirector of Information TechnologyCommented:
Have you tested the Timer display yet to see if the users can see the message and count down?
0
janhoedtAuthor Commented:
No, I have not.
0
yo_beeDirector of Information TechnologyCommented:
From what I can see you are using the MSG as well as the chglogon to accomplish the Alert as well as the lock down of the sessions
Do you have to have the Countdown Timer or can you just leverage the Message from the Shutdown command?
From my testing the msg command with /Time: display switch worked as well as the message from the shutdown command.
0
janhoedtAuthor Commented:
Thanks! I'll check asap.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.