在c#的远程机器上执行命令时获得错误代码1

本文关键字:命令 执行 错误代码 程机器 机器 | 更新日期: 2023-09-27 18:14:53

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.

不确定是什么错误。有谁能帮我一下吗?

案例2:

远程站点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.

在c#的远程机器上执行命令时获得错误代码1

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