如何使用"format-list"调用powershell命令和“;out-file"c#

本文关键字:quot out-file 命令 调用 何使用 format-list powershell | 更新日期: 2023-09-27 18:01:42

嗨,我正在编写一个c#程序,用于在远程运行空间中调用exchange2010 powershell cmdlet。ps命令为:

"Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List .标识,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File'C:'db.txt' -Encoding UTF8 -Width 8192".

我的代码类似于:


static int Main(string[] args)
{
    const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";
    const string COMMAND = "Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:'db.txt' -Encoding UTF8 -Width 8192";
    System.Uri serverUri = new Uri("http://EX2010SVR1/powershell?serializationLevel=Full");
    PSCredential creds = (PSCredential)null; // Use Windows Authentication
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds);
    try
    {
        using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            rs.Open();
            PowerShell psh = PowerShell.Create();
            psh.Runspace = rs;
            psh.AddCommand(COMMAND);
            Collection results = psh.Invoke();
            rs.Close();
        }
    }
    catch (Exception ex)
    {
        System.Console.WriteLine("exception: {0}", ex.ToString());
    }
    return 0;
}

当我在Win2008 R2上运行c#程序时,它承载了exchange 2010服务器,我总是得到异常:


    System.Management.Automation.RemoteException: The term 'Format-List' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke()
    at RemotePS.Program.Main(String[] args)

如果没有"Format-List"answers"Out-File"管道,程序可以正常工作。整个命令在exchange 2010管理shell中也可以正常工作。我还确认了系统上的powershell 2.0。

有谁能帮忙弄清楚发生了什么事吗?如有任何帮助,不胜感激。 汤姆

如何使用"format-list"调用powershell命令和“;out-file"c#

我写的第一个嵌入式PowerShell也有同样的问题。我在找蛛丝马迹,可是再也找不到了。

这是我的工作,我适应你的代码:

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

小心,我没有使用Exchange PowerShell

在我使用管道的例子中,也许你的问题来自于你传递命令的方式。

您可以尝试使用'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();
从c#中调用powershell cmdlet

我知道这是一个旧的线程,但我想提出我的发现,无论多么短。

我最近和我的一个同事遇到了同样的问题。我们设法追踪到问题的根源是缺失的运行空间。我们还必须连接到Microsoft.Exchange运行空间,当我们这样做时,Format-List命令集变得不可用。如果我们不使用运行空间,命令行也可以正常工作。

我们还没有解决这个问题,但我打算探索使用RunspacePool而不仅仅是Runspace的可能性,从而允许在管道中执行两个命令。