RedirectStandardInput对powershell远程执行没有影响

时间:2015-07-23 09:11:08

标签: c# powershell input process

我正在尝试重定向powershell.exe远程执行的输入。

它给了我下一个输出:

  

Windows PowerShell版权所有(C)2009 Microsoft Corporation。版权所有。

     

主机收到失败10054

所以看起来它调用了powershell,但是下一个命令没有作为输入传递,没有任何反应,所以Process退出并超时。

为什么呢? PowerShell是否有一些特定的输入,因此不能像这样启动?任何解决方法?

PS执行如下:

class AnnouncementForm(ModelForm):
    class Meta:
        model = Announcement
        ...
        widgets = {
            'announcement_title': forms.TextInput(attrs={'class': 'form-control'}),
            'announcement_text': forms.Textarea(attrs={'class': 'form-control', 'cols': 80, 'rows': 10}),
            'announcement_expiry': widgets.DateInput(attrs={'class' : 'datepicker'}),
            'announcement_high': forms.CheckboxInput()
        }

ExecutePowerShell:

Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();

ExecutePSCommandWithInput:

RemoteExecute.ExecutePowerShell(testPSName, testIp, testUserName, testPasswd);

ExecuteCmdWithInput:

FTPTransfer.SendBinary(shellScriptName, ipaddress, userName, password); // This one sends script to remote system. Works OK.
string fullFilePath = "\"" + FTPTransfer.UploadDirectoryRootPath + shellScriptName + "\"";

string cmd;
using (StringWriter sw = new StringWriter())
{
                //sw.WriteLine("powershell.exe"); // Tried launch remexec with cmd, and then pass powershell as first parameter. Results the same, as now, so no matter.
                sw.WriteLine("Set-ExecutionPolicy RemoteSigned");
                sw.WriteLine(fullFilePath);

                cmd = sw.ToString();
}
result = ExecutePSCommandWithInput(cmd, ipaddress, userName, password);

1 个答案:

答案 0 :(得分:0)

PowerShell不使用标准输入或输出,因此我不得不使用变通方法或不同的实现。

像这样解决:

string remexecArgs = string.Format("{0} -q -t {1} -l {2} -p {3} cmd 2>&1", ipaddress, timeout, userName, password);

并作为下一步传递的输入:

using (StringWriter sw = new StringWriter())
{
   sw.WriteLine("powershell.exe -Command Set-ExecutionPolicy RemoteSigned");
   sw.WriteLine();
   string logFilePath = "\"" + FTPTransfer.UploadDirectoryRootPath + shellScriptName + ".log\"";
   sw.WriteLine("powershell.exe -File " + fullFilePath + " " + (pars ?? string.Empty) + "> " + logFilePath);
   sw.WriteLine();
   sw.WriteLine();
   sw.WriteLine("type " + logFilePath);

   cmd = sw.ToString();
}
ExecuteOneCommandWithInput(cmd, ipaddress, userName, password, timeoutS);

空行是nesesery,否则它会卡住

小黑客加入了处理过程:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = mainCmd;
p.StartInfo.Arguments = arguments;
p.Start();
using (StreamWriter inputWriter = p.StandardInput)
{
   foreach(string line in commands)
   {
      //Wait before next input in case when empty string passed
      if (string.IsNullOrEmpty(line))
         System.Threading.Thread.Sleep(1000 * 2);

      inputWriter.WriteLine(line);
   }
}
output = p.StandardOutput.ReadToEnd();
output += Environment.NewLine;
output += p.StandardError.ReadToEnd();
p.WaitForExit(timeout);