In C #, You Can Use WMI to execute commands on a remote machine (WMI: Windows Management Interface) to access the components of different operating systems (Windows) through a public interface, it can be used to efficiently manage remote and local computers. It is also the control core of w2k3, w2k8, and XP management systems. The following is a demonstration of this work.Code:
//////////////////////////////////////// //////////////////////////////////////// //////////////////
// Connectionoptions specifies the settings required to generate the WMI connection
Connectionoptions connoption = new connectionoptions ();
Connoption. Username = domain + @ "\" + username;
Connoption. Password = password;
// Managementpath encapsulates the path for generating and analyzing WMI objects
Managementpath mngpath = new managementpath (@ "\" + serverhostname + @ "\ Root \ cimv2: win32_process ");
Managementscope scope = new managementscope (mngpath, connoption );
Scope. Connect ();
// The objectgetoptions class specifies the options used to obtain management objects.
Objectgetoptions objoption = new objectgetoptions ();
// Managementclass indicates the Common Information Model (CIM) management class. Members of this class can use specific WMI class paths to access WMI data.
Managementclass classinstance = new managementclass (scope, mngpath, objoption );
Int processid = 0;
Object [] cmdline = {"CMD/C" + strcommand, path, null, processid };
// Call the method for executing the command
Classinstance. invokemethod ("CREATE", cmdline );
Specifically, domain is the domain name used to log on to the remote machine, username, and password are the account and password used to log on to the remote machine.
Serverhostname is the name or IP address of the remote server to be accessed.
Strcommand is a command that needs to be executed on a remote machine.
//////////////////////////////////////// //////////////////////////////////////// //////////////////
In C #, you can use the client channel for transmitting messages over HTTP to implement remote calls. The following is the sample code:
// First establish a channel and register the channel service
Httpchannel c = new httpchannel ();
Channelservices. registerchannel (C, false );
// Call the executable file to perform the operation
Object remoteobject = activator. GetObject (type. GetType (remoteobject), remoteobjecturl );
Remoteobject jsonalobj = (remoteobject) remoteobject;
Export alobj. runcommand (exefilepath );
// Disable the channel
Channelservices. unregisterchannel (C );
Public class remoteobject: marshalbyrefobject
{
Public String runcommand (string cmd)
{
PROCESS p = new process ();
P. startinfo. filename = "cmd.exe ";
P. startinfo. useshellexecute = false;
P. startinfo. redirectstandardinput = true;
P. startinfo. redirectstandardoutput = true;
P. startinfo. redirectstandarderror = true;
P. startinfo. createnowindow = true;
P. Start ();
P. standardinput. writeline (CMD );
P. standardinput. writeline ("exit ");
P. Close ();
}
}