Shut Down Remote Computer - C#.net |
| |
 |
For Technical Information Reputation Power: 1

| | New Coder Posts: 26 Topics: 11  |
|
|
April 14, 2009, 06:35:38 AM
|
This code is an example of how to shut down a remote computer in C#.net using System.Management;
public class Win32OperatingSystem { public static void Shutdown(string machineName, string username, string password) { ManagementScope Scope = null; ConnectionOptions ConnOptions = null; ObjectQuery ObjQuery = null; ManagementObjectSearcher ObjSearcher = null; try { ConnOptions = new ConnectionOptions(); ConnOptions.Impersonation = ImpersonationLevel.Impersonate; ConnOptions.EnablePrivileges = true; //local machine if (machineName.ToUpper() == Environment.MachineName.ToUpper() ) Scope = new ManagementScope(@”\ROOT\CIMV2″, ConnOptions); else { //remote machine ConnOptions.Username = username; ConnOptions.Password = password; Scope = new ManagementScope(@”\\” + machineName + @”\ROOT\CIMV2″, ConnOptions); } Scope.Connect(); ObjQuery = new ObjectQuery(”SELECT * FROM Win32_OperatingSystem”); ObjSearcher = new ManagementObjectSearcher(Scope, ObjQuery ); foreach( ManagementObject operatingSystem in ObjSearcher.Get()) { MessageBox.Show(”Caption = ” + operatingSystem.GetPropertyValue(”Caption”)); MessageBox.Show(”Version = ” + operatingSystem.GetPropertyValue(”Version”)); ManagementBaseObject outParams = operatingSystem.InvokeMethod (”Shutdown”,null,null);
} } catch (Exception ex) { throw ex; } } }
//Note: Call the function like:
static void Main(string[] args) { Win32OperatingSystem.Shutdown(@”Machinename”, @”UserName”, @”Pwd”); }
« Last Edit: April 14, 2009, 06:42:19 AM by polas »
|
|
|
|
| |
 |
Reputation Power: 17

| | Code Guru Gender:  Posts: 1403 Topics: 86  |
|
|
April 14, 2009, 06:58:10 AM
|
Thanks for the post, I am a little confused - do you need this running on the remote machine too, or can you just run it from your local machine to work on a remote one? Seems quite an unsecure OS that allows itself to be shutdown remotely.....
|
|
| |
 |
For Technical Information Reputation Power: 1

| | New Coder Posts: 26 Topics: 11  |
|
|
April 14, 2009, 07:08:18 AM
|
NO application need to be run in the remote machine....It works fine......yeah OS is quite unsecure......
|
|
| |
 |
Reputation Power: 17

| | Code Guru Gender:  Posts: 1403 Topics: 86  |
|
|
April 14, 2009, 07:12:38 AM
|
Sorry, not entirely clear, I think you are saying it doesn't need to be run on the remote machine... in such case Windows security is worse than I thought!
|
|
| |
 |
For Technical Information Reputation Power: 1

| | New Coder Posts: 26 Topics: 11  |
|
|
April 14, 2009, 08:21:31 AM
|
sorry,....... yes u r right.....You can shut down any machine in the network using this code.No code need to be implemented on the remote machine which we are shutting down
|
|
| |
 |
Reputation Power: 1

| | New Coder Posts: 16 Topics: 0  |
|
|
January 28, 2012, 03:37:05 AM
|
NO application need to be run in the remote machine. . . . It works fine. . . . . . yeah OS is quite unsecure. . . . . .
|
|
| |