在C#中在远程计算机上执行命令时获取错误代码1

时间:2014-07-25 10:26:29

标签: c# psexec

static void Main(string[] args)
{
    string executionDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);      
    string remoteToolFileName = executionDir + "\\PSTools\\PsExec.exe";
    string myfolderpath = executionDir;
    string CommandToExecute = "\\\\" + MyHostIP + " -u username -p password ipconfig /all >> \"" + myfolderpath + "\\log.txt\"";
    RunCommand(remoteToolFileName, executionDir, CommandToExecute);
}

private static void RunCommand(string filename, string executionDir, string arguments = null)
{       
    var process = new Process();
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.FileName = filename;

    if (!string.IsNullOrEmpty(arguments))
    {
        processStartInfo.Arguments = arguments;
    }

    processStartInfo.CreateNoWindow = true;
    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    processStartInfo.UseShellExecute = false;
    processStartInfo.RedirectStandardError = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.WorkingDirectory = executionDir;
    process.StartInfo = processStartInfo;
    process.Start();
    process.WaitForExit();
}

输出:

在远程站点MyHostIP上执行的命令:

\\MyHostIP -u username -p password ipconfig /all >> "C:\My
folder\log.txt"

命令输出:

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

Connecting to MyHostIP...


Starting PSEXESVC service on MyHostIP...


Connecting with PsExec service on MyHostIP...


Starting ipconfig on MyHostIP...


ipconfig exited on MyHostIP with error code 1.

不确定是什么错误。任何人都可以帮我这个。

第二种情况:

在远程站点Myhost上执行的命令:

\\Myhost -u Username -p password "C:\Program Files\..\myapp.exe" -xml "C:\my Client\..\input.xml" 

命令输出:

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

Connecting to Myhost...

Starting PSEXESVC service on Myhost...

Connecting with PsExec service on Myhost...

Starting C:\Program Files\..\myapp.exe on Myhost...

C:\Program Files\..\myapp.exe exited on Myhost with error code -1.

1 个答案:

答案 0 :(得分:1)

重定向>>正由ipconfig命令解释,您必须使用cmd /c ipconfig才能使重定向正常工作 - 即重定向是{{}的一部分1}}选项,而不是cmd选项。