Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,

I am pretty much a novice at coding & i need your help. I want to reboot a list of remote servers & ping them until they come online. I did try my hand at rebooting a single server, but was not able to do so for multiple servers.

ConnectionOptions co = new ConnectionOptions();
            System.Management.ManagementScope ms = new ManagementScope(sServer + "\\root\\CIMV2");
            System.Management.ObjectQuery oq = new ObjectQuery("select * from win32_Processor");
            ManagementObjectSearcher query1 = new ManagementObjectSearcher(ms, oq);
            ManagementObjectCollection moc = query1.Get();
            foreach (ManagementObject mo in moc)
            {
                string[] s = { "" };
                mo.InvokeMethod("Reboot", s);
                Console.WriteLine(mo.ToString());
            }


Any help in getting multiple servers to reboot at once & ping them until they are online will be of great help!

Thanks!
//Vk
Posted

I would personally use PowerShell instead of C# for these kind of Sys Admin tasks.

Here is an article on how to do this with PowerShell:

http://mcpmag.com/articles/2012/04/10/how-to-restart-computers-remotely-via-powershell.aspx[^]

http://myitforum.com/cs2/blogs/yli628/archive/2009/01/06/powershell-script-to-restart-multiple-remote-computers.aspx[^]

It's actually just one liner:

gc c:\Temp\ServerList.txt |%{Restart-computer –computername $_ –force}
 
Share this answer
 
Comments
Vishwanth Kini 7-May-14 0:59am    
Thank you Manas for the links but PowerShell is not my cup of tea! :)
 
Share this answer
 
Comments
Vishwanth Kini 7-May-14 0:58am    
Thank you Nilesh

I followed the links that you gave me & it didn't work. The code just executes, but does nothing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900