C# .NET - How to turnoff other computer in Lan Network

Asked By Lokesh Phanindra Bonthu on 04-Nov-08 01:42 PM

please tell me

How to write C# code for  turnoff other computers in Lan Network

re - Web Star replied to Lokesh Phanindra Bonthu on 04-Nov-08 11:36 PM

Syntax
shutdown [{-l|-s|-r|-a}] [-f] [-m [\\ComputerName]] [-t xx] [-c "message"] [-d[u][p]:xx:yy]

Parameters
-l
Logs off the current user, this is also the defualt. -m ComputerName takes precedence.
-s
Shuts down the local computer.
-r
Reboots after shutdown.
-a
Aborts shutdown. Ignores other parameters, except -l and ComputerName. You can only use -a during the time-out period.
-f
Forces running applications to close.
-m [\\ComputerName]
Specifies the computer that you want to shut down.
-t xx
Sets the timer for system shutdown in xx seconds. The default is 20 seconds.
-c "message"
Specifies a message to be displayed in the Message area of the System Shutdown window. You can use a maximum of 127 characters. You must enclose the message in quotation marks.
-d [u][p]:xx:yy
Lists the reason code for the shutdown. The following table lists the different values. Value Description
u Indicates a user code.
p Indicates a planned shutdown code.
xx Specifies the major reason code (0-255).
yy Specifies the minor reason code (0-65536).

CODE TO SHUTDOWN COMPUTER REMOTELY - C_A P replied to Lokesh Phanindra Bonthu on 05-Nov-08 04:47 AM

try this code,
-------
Private Sub Command1_Click()
Shell ("shutdown -a\\(computername or ip)")
End Sub

READ THIS TO SHUTDOWN COMPUTER REMOTELY - C_A P replied to Lokesh Phanindra Bonthu on 05-Nov-08 04:48 AM

How can I quickly shut down all the computers on my network?

Use the FOR command combined with the SHUTDOWN command:

FOR /L %i in (1,1,254) DO shutdown \\192.168.0.%i

Windows XP will let you log-off all users in one line:

FOR /L %i in (1,1,254) DO shutdown \\192.168.0.%i /l

Change 192.168.0 to match you own Network ID.

For Windows Server 2003 based computers you'll need to refine the SHUTDOWN command and provide the reason for the restart or shutdown. For example, if you plan to reboot servers because of a security fix deployment you can use the following command:

FOR /L %i in (1,1,254) DO shutdown /r /m \\192.168.0.%i /d p:2:18

Use SHUTDOWN /? for a complete list of available switches.

Note: The above scripts will cause ALL the computers on your LAN (basically, on the segment you pointed, 192.168.0.0 for the above example) without notifying their users. It will also shut down all your file servers, print servers, domain controllers, e-mail servers and all other Windows based computers. You do NOT want to do that, do you? Change the IP address range to suit YOUR needs!

You can disable other user's ability to remotely shutdown computers by editing their respective Local Security Policy in the User Rights section, or, if you're in a W2K domain you can edit the servers' GPO.

Download Shutdown.exe it you don't have it from http://www.petri.co.il/software/shutdown.zip (11kb). This is a GUI version of the command:

http://www.petri.co.il/images/shutdown.jpg

Windows XP has the SHUTDOWN  command built-in, and running it as SHUTDOWN -i will bring up this console:

http://www.petri.co.il/images/shutdownxp.jpg

Many other tools will enable you to remotely shutdown a system.

turnoff other computer in Lan Network - Binny ch replied to Lokesh Phanindra Bonthu on 05-Nov-08 01:12 PM
Use this code:

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:
[STAThread]
static void Main(string[] args)
{
Win32OperatingSystem.Shutdown(@"Machinename", @"UserName", @"Pwd");
}
error - Murat Covanni replied to Binny ch on 31-Oct-09 07:23 AM
After I run this code, It returned an error back... "RPC Server is unavaible"

It returned the error on " Scope.Connect(); "

Is there anybody solved this error ?
SSaaa replied to Web Star on 05-Oct-10 04:13 AM
After executing the command it gives an error 'Access Denied'. We want the programme which will not ask any type of user name / password of remote computer. On the basis of computer name / IP address it should work. Is there any thing like that ?
Julius replied to Binny ch on 10-Dec-10 05:43 AM

I tried  ur program code as below and am getting 14 errors:

================
1.The type or namespace name 'ManagementScope' could not be found are  u missing a directive or assembly reference?
2. as 1. above for 'ConnectionOptions', 'ObjectQuery', 'ManagementObjectSearcher','

ConnectionOptions'(), and where ever else these are used.

I have tried adding a few namespaces but none seem to work what am I doing wrong or is this run on a different platform? am using  vista and team studio 2008.

, and where ever else these are used.I have tried adding a few namespaces but none seem to work what am I doing wrong or is this run on a different platform? am using  vista and team studio 2008.

Pls help
======================

turnoff other computer in Lan Network

Binny ch replied to Lokesh Phanindra Bonthu at Wednesday, November 05, 2008 1:12 PM
Use this code:

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:
[STAThread]
static void Main(string[] args)
{
Win32OperatingSystem.Shutdown(@"Machinename", @"UserName", @"Pwd");
}