简体   繁体   English

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

[英]Getting error code 1 while executing a command on remote machine in C#

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();
}

Output: 输出:

Command to execute on remote site MyHostIP : 在远程站点MyHostIP上执行的命令:

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

Command output: 命令输出:

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.

Not sure what is the error. 不知道是什么错误。 Can anyone help me on this please. 谁能帮我这个忙。

2nd case: 第二种情况:

Command to execute on remote site Myhost : 在远程站点Myhost上执行的命令:

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

Command output: 命令输出:

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.

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM