代码之家  ›  专栏  ›  技术社区  ›  Sudheer

将powershell cmdlet输出写入文件,而不是从c写入管道#

  •  1
  • Sudheer  · 技术社区  · 10 年前

    我正在尝试用C#实现powershell cmdlet。

    我在代码中有一个类似的对象。

    class A{
        string Title_p;
        string Title_q;
        int Title_r;
        string[] Title_s;
    }
    

    因此,在cmdlet实现中,当我给出 this.WriteObject(A_obj) ,它将对象数据转换为漂亮的可读表格格式,列标题作为对象名称。如下所示:

    Title_p   Title_q  Title_r  Title_s
    =======   =======  =======  =======
    Hello      Bolo     12     {RR,TT,YY}
    

    但所有这些输出都被重定向到管道,最终的cmdlet将从该管道触发。

    但我还想将所有这些输出记录到一个文件中(除了打印到管道)

    我知道我可以给予 CmdLet-Sud | out-file c:/data.txt 。但我不想要这个。我只想发出命令 CmdLet-Sud ,它应该将数据打印到管道和文件。

    那么,如何将对象转换为这种可打印的表格格式呢?

    3 回复  |  直到 10 年前
        1
  •  0
  •   Χpẘ    10 年前

    您可以在C#代码中创建“Powershell”(使用System.Management.Automation.Powershell)。您的“Powershell”(错误的类名选择IMO,我将其称为SMA。为了清楚起见,在这个答案中Powershell)可以调用 tee-object (或 append-content set-content ). 然后,可以将类实例作为参数传递给SMA.Powershell。

    或者,您可以只使用系统下的类。IO文件空间并直接写入文件。您可以通过write对象单独写入相同的输出。

    顺便说一句,除非在类中支持序列化和反序列化,否则将输出写入文件是没有意义的。除非,您真正想要的是将类实例编写为文本表。在这种情况下,您的SMA。Powershell可以是这样的 % { $_ | ft | set-content c:\data.txt; $_} .

        2
  •  0
  •   Sudheer    10 年前

    Tee Object为我做了这项工作。

    PowerShell powerShellInstance = PowerShell.Create();
    powerShellInstance.AddCommand("Tee-Object");
    powerShellInstance.AddParameter("FilePath", filePath);
    powerShellInstance.AddParameter("InputObject", obj_A);
    powerShellInstance.AddParameter("Append");
    powerShellInstance.Invoke();
    

    但是,当上面的代码被多次调用时,该文件被写为

    Title_p   Title_q  Title_r  Title_s
    =======   =======  =======  =======
    Hello      Bolo     12     {RR,TT,YY}
    
    Title_p   Title_q  Title_r  Title_s
    =======   =======  =======  =======
    Aaaao      Colo     13     {EE,YY}
    
    Title_p   Title_q  Title_r  Title_s
    =======   =======  =======  =======
    HHSAR      Dolo     14     {QQ,XX}
    

    但在cmdlet实现中,即使您调用了它。WriteObject()多次使用多个对象,结果如下

    Title_p   Title_q  Title_r  Title_s
    =======   =======  =======  =======
    Hello      Bolo     12     {RR,TT,YY}
    Aaaao      Colo     13     {EE,YY}
    HHSAR      Dolo     14     {QQ,XX}
    

    我正在尝试调整代码,以便即使在程序中多次调用invoke()命令,输出文件仍然只有一个列标题,而不是每个对象的一个列头(在最终输出文件中)

        3
  •  0
  •   Sudheer    10 年前

    终于完成了,我想要:

    // When this method is called for the first time, it creates a table header too.
    // The following is the PowerShell line that will be run in the runspace.
    // Write-Output -InputObject <object_A> | Format-Table | Out-String
    
    // For the next time, when this routine is called, the following gets executed.
    // Write-Output -InputObject <object_A> | Format-Table -HideTableHeaders | Out-String
    
    Runspace runspace = RunspaceFactory.CreateRunspace();
    runspace.Open();
    Pipeline pipeLine = runspace.CreatePipeline();
    
    Command command1 = new Command("Write-Output");
    command1.Parameters.Add("InputObject", object_A);
    
    Command command2 = new Command("Format-Table");
    if (this.filePath != null)
    {
        // If this is not the first row, hide the table headers
        command2.Parameters.Add("HideTableHeaders");
    }
    Command command3 = new Command("Out-String");
    
    pipeLine.Commands.Add(command1);
    pipeLine.Commands.Add(command2);
    pipeLine.Commands.Add(command3);
    var results = pipeLine.Invoke();
    
    // The resultant text from the command will have a lot of white spaces/carriage returns.
    // So, let us trim the content before adding it to file.
    foreach (var result in results)
    {
        string data = null;
        data = string.Concat(result);
        data = data.Trim();
    
        // For the first time, we need to create a log file too.
        if (filePath == null)
        {
            // Call custom routine CreateLogFile() which creates a file and returns the file path as string for you
            filePath = CreateLogFile();
        }
    
        // Write the object row into the file.
        using (StreamWriter writer = System.IO.File.AppendText(this.filePath))
        {
            writer.WriteLine(data);
        }
    }
    
    runspace.Close();
    
    推荐文章