2

C# を使用して、リモート コンピューターのコマンド プロンプトでコマンドを実行したいと考えています。このリンクごとリモートコンピューターでコマンドを実行する方法? 、次のコードを使用してこれを実行しようとしています:

public static void RunRemoteCommand(string command, string RemoteMachineName)
{
    ManagementScope WMIscope = new ManagementScope(
        String.Format("\\\\{0}\\root\\cimv2", RemoteMachineName));
    WMIscope.Connect();
    ManagementClass WMIprocess = new ManagementClass(
        WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    object[] process = { command };
    object result = WMIprocess.InvokeMethod("Create", process);
    Log.Comment("Creation of process returned: " + result);
}

これは終了コード 0 を返し、エラーはスローされませんが、何も実行されません。助けてください。

4

3 に答える 3

1

これが役立つことを願っています

http://www.petri.co.il/command-line-wmi-part-2.htm

「代替資格情報」セクションをご覧ください。私はあなたが自分で小さな変更を行うことができると信じています.

于 2013-07-25T01:45:54.173 に答える
0

投稿が古いことは知っていますが、このページに出くわした他の人や完全性のために: RRUZ のコメントは正しいですが、リモート マシンで実行するために必要なことがもう 1 つあります。資格情報です。そのためには、接続オプションを追加する必要があります。

public static void RunRemoteCommand(string command, string RemoteMachineName, 
                                    string username,string password)
{


            var connection = new ConnectionOptions();
            connection.Username = username;
            connection.Password = password;


    ManagementScope WMIscope = new ManagementScope(
        String.Format("\\\\{0}\\root\\cimv2", RemoteMachineName), connection);
    WMIscope.Connect();
    ManagementClass WMIprocess = new ManagementClass(
        WMIscope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
    object[] process = { command };
    object result = WMIprocess.InvokeMethod("Create", process);
    Log.Comment("Creation of process returned: " + result);
}
于 2016-11-08T22:26:49.080 に答える