[英]Retrieve cmd prompt output from PsExec command?
Use case: I am checking certain credentials on a remote system by running commands via PsExec (ie for this example, I am trying to retrieve the KB articles currently installed on the remote system). 用例:我通过通过PsExec运行命令来检查远程系统上的某些凭据(即,对于本示例,我试图检索当前安装在远程系统上的KB文章)。
I have the following to retrieve command output: 我有以下检索命令输出的内容:
public string GetCmDOutput(string cmd)
ProcessStartInfo startInfo = new ProcessStartInfo("control", cmd)
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
string output = string.Empty;
Process process = Process.Start(startInfo);
process.OutputDataReceived += (sender, e) => output = string.Concat(output, e.Data);
process.BeginOutputReadLine();
process.Start();
process.WaitForExit();
Delay.Milliseconds(1500) //API-specific delay
return output;
}
Whenever I use GetCmdOutput()
to run a command locally it works like a charm, but if I try to run a command with PsExec, my output is empty. 每当我使用
GetCmdOutput()
在本地运行命令时,它就像一个GetCmdOutput()
一样工作,但是如果我尝试使用PsExec运行命令,则输出为空。
For instance, I ran the following: 例如,我运行以下命令:
string cmd = @"\psexec.exe \\remoteComputerName -u username -p password -c cmd /c wmic qfe";
GetCmdOutput(cmd);
Report.Info(cmd); //API-specific reporting
And an empty string was returned. 然后返回一个空字符串。
After playing around with this for a couple of hours, I feel I may need a second set of eyes. 在玩了几个小时之后,我觉得我可能需要第二只眼睛。 What might be causing this issue?
是什么导致此问题?
I have run into this same problem. 我遇到了同样的问题。 My solution was to run cmd and have it call psexec.
我的解决方案是运行cmd并将其称为psexec。 I have psexec's output saved to a temp file for further manipulation.
我已将psexec的输出保存到临时文件中,以进行进一步操作。 My code is returning a List.
我的代码返回一个列表。
public List<string> ExecutePSExec(string hostname)
{
List<string> recordNames = new List<string>();
string command = @"\\path\to\psexec.exe /accepteula \\" + hostname + ". exe-to-run-remotely";
try
{
string location = AppDomain.CurrentDirectory.BaseDirectory;
string cmdWithFileOutput = string.Format("{0} >{1}temp.log", command, location);
procStartInfo.UseShellExecute = true;
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
// Read file contents, manipulate data and then delete temp file here
}
catch (Exception e)
{
Console.WriteLine("Failure to run psexec: {0}", e.Message);
}
return recordNames;
}
NOTE : I ran into another problem and found out that running psexec this way requires the remote hostname (not IP Address) in the command to end in a period \\\\" + hostname + ".
注意 :我遇到另一个问题,发现以这种方式运行psexec要求命令中的远程主机名(不是IP地址)以句点
\\\\" + hostname + ".
结尾\\\\" + hostname + ".
This code assumes you can run psexec on the remote machine as your current user. 此代码假定您可以以当前用户身份在远程计算机上运行psexec。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.