Powershell script to reboot multiple servers

A system administrator in my company recently approached me with a problem — how to shutdown multiple Linux servers at the same time from a central location. So lets suppose you have a file with a list of all your servers. So here is a little something that scans such a list, calls each server to shut it down and waits 30 seconds for the server shutdown to complete.

The assumption here is that you have exchange keys with each server so you can login as root to each server without the need to type in a password. Here is the script, with some inline documentation. Just copy and past it into a text file and make it executable. The server file is expected to be in a file named lan-servers. And thats it. Of course if you really want an application we can complicate it a lot more if we want — add a GUI, some command line switches, a web interface — what not.

But the basic functionality is something that a sysadmin should be able to hack in a few minutes. This story highlights one issue I have with MS-Windows: you need special apps to handle all kinds of simple administration tasks — for example my Brother uses a special program for batch renaming of files. This functionality is completely unneeded on Linux because your simple shell is so much more powerful and an advanced user 1 can write much more complex renaming commands then what a GUI can offer, using simple bash commands.

With MS-Windows simple stops at about the process of moving an entire directory tree. With Linux simple can be much much more interesting. You can follow any responses to this entry through the RSS 2. You can leave a responseor trackback from your own site. It will also elegantly skip hosts that are already down. It will do its job cleanly and report the results in a simple way.

Plus, I have been unemployed for more than a year now, so things might have improved since last time I administered a Windows machine — I recall having heard of a new Windows shell, and some POSIX tools coming from Microsoft. See my reply to Eran below about Powershell. But the reason I use Magic File Renamer is that I wanted something that will make all the pictures I have look like this and all the audiobooks look like that and all the music albums look another way.

Well, I have tested it. They have some good things, like strong syntax, but they messed some other things completely — like pipes as object streams?

powershell script to reboot multiple servers

Come on! Regardless: CMD.The Restart-Computer cmdlet restarts the operating system on the local and remote computers. You can use the parameters of Restart-Computer to run the restart operations, to specify the authentication levels and alternate credentials, to limit the operations that run at the same time, and to force an immediate restart.

Starting in Windows PowerShell 3. Specify a waiting time-out and query interval, and wait for particular services to be available on the restarted computer. This feature makes it practical to use Restart-Computer in scripts and functions. Restart-Computer can restart remote and local computers. The ComputerName parameter accepts an array of computer names. Restart-Computer gets a list of computer names from a text file and restarts the computers.

The ComputerName parameter isn't specified. But because it's the first position parameter, it accepts the computer names from the text file that are sent down the pipeline. Get-Content uses the Path parameter to get a list of computer names from a text file, Domain The computer names are sent down the pipeline. Restart-Computer restarts each computer. This example forces an immediate restart of the computers listed in the Domain The computer names from the text file are stored in a variable. The Force parameter forces an immediate restart.

Restart-Computer uses the ComputerName and Credential parameters with their variables. The Force parameter causes an immediate restart of each computer. Restart-Computer restarts the remote computer and then waits up to 5 minutes seconds for PowerShell to become available on the restarted computer before it continues. Restart-Computer uses the ComputerName parameter to specify Server The Wait parameter waits for the restart to finish.

The For specifies that PowerShell can run commands on the remote computer. The Timeout parameter specifies a five-minute wait. The Delay parameter queries the remote computer every two seconds to determine whether it's restarted. Restart-Computer restarts the remote computer using the WsmanAuthentication mechanism.I am new to powershell and i am trying to write a simple script that will reboot a list of machines XP, and 7 and input the results of the reboots to a text file.

I created the below script that reboots the machine and creates the output file, but when I open the file I do not see any results in it. I also found the below script but for some reason it will not work, but it's not rebooting the machines. I can get it to show me the list of machines in the text file but it does not reboot them. My plan was to get it working and output the results to a file, but it's not working out for me. The first script does restart the computers, most likely, but it will not show what it is doing.

So the second option would most likely be a better option if you actually want to see what is happening. The second script has a -whatif parameter specified. So it will not actually take any action, it will only display what the command would do if -whatif would not have been specified. This should work:.

You really need to define what you were hoping to see when you say "input the results of the reboots to a text file. The problem with what you are asking is that the life of the Restart-Computer command ends before the computer completely goes down.

And there is no concept of "checking in" after the fact. One possible solution would be to create a one-time "At Startup" scheduled task before issuing the actual restart. The whole point of this task is to report in via a logfile on some share. It's simple method.

How to Restart or Shutdown a Remote Computer

The code below generates a comma delimted text file with the client name and a time stamp. Idera uses cookies to improve user experience. Site Search User. Ask the Experts. More Cancel. Share More Cancel. Reply Cancel Cancel. Jaap Brasser over 6 years ago. Up 0 Down Reply Cancel. Bob McCoy over 6 years ago. Hey Jaap, thanks for your input. I tried it and it didn't work. Bob McCoy over 6 years ago in reply to psnewbieI am horribly stuck into a challenge for designing a powershell code to reboot a list of servers from a CSV depending on a Sequence Number.

I am seeking for suggestions on how shall I proceed or any bits or piece of code that would help me. If you want to make your life easy, make sure you're running Powershell 3. To determine your version:. I'd skip having a sequence number and use the line number as the boot sequence in a line delineated file. Then use the restart-computer commandlet Something like this should work for you:. Thanks for the replyif I am not wrong according to you the servers will be rebooted sequentially i.

Line by line and the list of servers falling in each line would be rebooted together before proceeding to the next line? You are real McCoy to handle this challenge so well. Btw in my initial requirement in which I am following the sequence number to go with can we follow the below procedure:. This is usually where the concept falls apart.

You have to be very crisp in your language as to what "online" means to you. Commas create an array.

powershell script to reboot multiple servers

The above generates 7 array elements. So in this case the sequence would be server01, server02, server10, etc. To get to concurrency it would have to be. Thanks for your enormous support. I am will implement it in an Live environment and will let you know on its Success or Failure. Everything that comes from you is so matured and so precise. Highly appreciable. To continue this discussion, please ask a new question. Get answers from your peers along with millions of IT pros who visit Spiceworks.

Hello Everyone, I am horribly stuck into a challenge for designing a powershell code to reboot a list of servers from a CSV depending on a Sequence Number. Best Answer. Sigkill Dec 24, at UTC. To determine your version: Powershell. Popular Topics in PowerShell. Which of the following retains the information it's storing when the system power is turned off?

Correct, the reboot-computer commandlet can accept a a comma separated list of servers, which would reboot every server on each line at the same time in my above example. The only thing you probably want to modify is some logic for when it should reboot the next line.

As a place holder, I put in a 10 second delay. Do you have any specific requirements before it reboots the next line? Thai Pepper. What does it truly mean to you for a system to be online?Unfortunately, you cannot not always just restart servers during maintenance as they may have a service dependent on another server.

Due to this, you may need to restart your servers in sequential order.

powershell script to reboot multiple servers

Luckily, powershell 3. Please note, the restart-computer commandlet added several important parameters vs the 2. The above parameters allow you to ensure a server rebooted before moving on in the script. For a full description, see Restart-Computer on TechNet. For our script, we will use a CSV file that lists the server names and the sequence to group them for restarting Yes we can have multiple servers in the same sequence number.

Our powershell script will then read in the the servers. The below powershell script will wait until the servers have fully rebooted before moving to the next sequence number to reboot. You may wish to modify the restart-computer parameters to suit your needs. I have the actual name in the csv file and it reads it but I keep getting this error.

Works great if the servers all have different sequence numbers, but I receive the following error if any two have the same sequence number:. Object[] cannot be resolved with the exception: One or more errors occurred. I am getting the same thing. I had something odd happen with this, It only worked on some of the servers I was trying to reboot. One group worked perfectly, while others were hit and miss. Here is the error I get:.

And by the way, the one group that worked was amazing. Perfectly rebooted 4 servers in group. All I had to do was look at it every once in a while to make sure it was still going. Your email address will not be published. Main menu Skip to primary content. Skip to secondary content.

powershell script to reboot multiple servers

Home About Me Sitemap. For a full description, see Restart-Computer on TechNet For our script, we will use a CSV file that lists the server names and the sequence to group them for restarting Yes we can have multiple servers in the same sequence number. Just found a formatting error on the code I pasted on this post. It has now been corrected.

Works great if the servers all have different sequence numbers, but I receive the following error if any two have the same sequence number: restart-computer : Computer name System. I also checked to make sure the names were correct, I could ping, etc. Leave a Reply Cancel reply Your email address will not be published.Rebooting a Windows computer is easy, right?

We click the Start button and click Restart, and it just does it. This is fine for your computer at home, but what about doing this at work where we're working with hundreds or thousands of clients and servers? Rebooting a computer isn't as simple then. You may be familiar with the Restart-Computer cmdlet in PowerShell. Restart-Computer is a common command a lot of IT professionals use to reboot the local as well as many remote computers. After all, we can specify as many computers as we want in its ComputerName parameter to restart lots of computers at once!

Problem solved? Maybe—maybe not. One potential problem is that Restart-Computer doesn't just work automatically. The remote computers themselves have to meet certain prerequisites for that to happen.

By default, it uses DCOM. By using each of these parameters, you can fine-tune how Restart-Computer reaches out to each computer. Yet what if you need to restart computers at once? This is possible with Restart-Computerbut by default, this happens serially. Restart-Computer invokes a reboot for computer 1, then computer 2, then computer 3…and so on and so on.

If you're rebooting computers, this is going to take a while. Since each reboot is independent of the other, why wait for each one?

reboot a list of servers and output results to file.

That's what we have background jobs for! Restart-Computer has an AsJob parameter just for this. Instead of waiting for each reboot to start, we can just use the AsJob parameter to create a background job and keep going. Using background jobs is much faster than performing reboots one at a time.

Powershell Script to Install Software to Remote Computers [AskJoyB]

Next, the result of a reboot is the computer actually comes back up, right? Then why are we just invoking Restart-Computer and assuming that will be the case? To be more accurate and actually confirm a computer comes back up, we can use the Wait parameter with the optional For parameter. The Wait parameter does what we want. It forces Restart-Comp uter not only to invoke the reboot but wait for it to come back up.

Since "come back up" can be a relative term, we also have the For parameter that allows us to get even more granular and wait for a specific service to come back online. The Wait and For parameters are great to use in a script where you have additional tasks to perform on the remote computer after it comes back up. The Restart-Computer cmdlet is extremely useful in a number of different scenarios. By using the parameters provided, you can initiate not only a reboot but also initiate that reboot and ensure the reboot happened.

Join the 4sysops PowerShell group! Your question was not answered? Ask in the forum! Dear Professional, I don't have background with PowerShell and recently I'm looking for the script to perform server restart.

My scenario is : I have 5 Server which's need to restart by order ServerA : will be the first restart and I need to check some service of my application which is runing in the server whether is already up then go to the next server ServerB, ServerC,ServerD, and ServerE. Will you provide Mr Bertram with some sort of monetary compensation for his effort to help you, or are you just expecting him to do your job?By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I've created one powershell in which i am passing values in a csv file for server name and service. One powershell script which will either start or stop all the services mentioned in second text file on first server listed in first text file, once done with first server it shd go to second server with all these services and so on till last entry in list of remote server.

I've created two separate text files, one for server name say servers. Learn more. Asked 6 years, 1 month ago. Active 4 years, 2 months ago. Viewed 8k times. AkashR AkashR 39 1 1 silver badge 6 6 bronze badges. Is there some dependency involved - i. Do you need to handle exceptions and errors, if e.

So what have you attempted in the course of solving this? At what point are you getting stuck. Show your work - don't ask people here to do it all for you. I just want avoid lengthy csv file with simple two text files which i've mentioned in my question. Any suggestion welcome.

Active Oldest Votes.

PowerShell – Simple Mass Server Shutdown/Reboot Script with Menu & Status Check (Video)

I have found solution to above. Ansgar Wiechers k 19 19 gold badges silver badges bronze badges. This is not a good solution, since the only association between host name and services is the line number in the two separate files.

Could this happen in a parral way? Ansgar Wiechers Ansgar Wiechers k 19 19 gold badges silver badges bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown.

The Overflow Blog. Socializing with co-workers while social distancing. Podcast Programming tutorials can be a real drag. Featured on Meta. Community and Moderator guidelines for escalating issues via new response…. Feedback on Q2 Community Roadmap. Dark Mode Beta - help us root out low-contrast and un-converted bits. Technical site integration observational experiment live on Stack Overflow.

thoughts on “Powershell script to reboot multiple servers

Leave a Reply

Your email address will not be published. Required fields are marked *