how to invoke the powershell command with “format-list” and “out-file” pipeline from c#?

霸气de小男生 提交于 2019-12-06 05:48:51

I've got the same problem with the first embeded PowerShell I wrote. I look for a trace, but I can't find it anymore.

Here is something working for me that I adapt to your code :

static void Main(string[] args)
{
  const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
  const string COMMAND = @"get-process | format-List | Out-File -file c:\temp\jpb.txt";
  System.Uri serverUri = new Uri("http://WM2008R2ENT/powershell?serializationLevel=Full");
  PSCredential creds = (PSCredential)null; // Use Windows Authentication
  WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false,
                                                               "WM2008R2ENT",
                                                               5985,
                                                               "/wsman",
                                                               SHELL_URI,
                                                               creds);
  try
  {
    using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))
    {
      rs.Open();

      Pipeline pipeline = rs.CreatePipeline();

      string cmdLine;
      cmdLine = string.Format("&{{{0}}}", COMMAND);

      pipeline.Commands.AddScript(cmdLine);

      Collection<PSObject> results = pipeline.Invoke();

      rs.Close();
    }
  }
  catch (Exception ex)
  {
    System.Console.WriteLine("exception: {0}", ex.ToString());
  }
  return; 
}

Be carefull, I'am not using Exchange PowerShell

In the example I use pipeline, perhaps your problem comes from the way you pass the command.

algorhythm

You can try to work with the 'Command'-Object.

Runspace rs = RunspaceFactory.CreateRunspace();
PowerShell ps = PowerShell.Create();

Pipeline pipeline = rs.CreatePipeline();

Command cmd1 = new Command("Get-MailboxDatabase");
cmd1.Parameters.Add("Server", "EX2010SVR1");
cmd1.Parameters.Add("Status");
pipeline.Commands.Add(cmd1);

Command cmd2 = new Command("Format-List");
cmd2.Parameters.Add("Property", "Identity, Guid, mounted, CircularLoggingEnabled, Recovery");
pipeline.Commands.Add(cmd2);

Command cmd3 = new Command("Format-List");
cmd3.Parameters.Add("FilePath", "C:\db.txt");
cmd3.Parameters.Add("Encoding", "UTF8");
cmd3.Parameters.Add("Width", "8192");
pipeline.Commands.Add(cmd3);

Collection<PSObject> output = pipeline.Invoke();

See also here: Invoking powershell cmdlets from C#

I realize this is an old thread, but I wanted to present my findings, however short they are.

I ran into this same problem just recently with a colleague of mine. We managed to track the problem down to the missing runspaces. We also had to connect to the Microsoft.Exchange runspace and when we do it, the Format-List commandlet becomes unavailable. If we don't use the runspace, the commandlet works just fine.

We didn't get to solving it yet, but I intend to explore the possibility of using the RunspacePool instead of just Runspace, thus allowing the execution of both commandlets in the pipeline.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!