缩进“格式表|输出文件”

问题描述

好的,我正在对多个服务器运行查询。我的数据正在输出到格式如下的文本文件

System Info
     -Server1
          -Section1
               Some info for section1
               Some more info for section1
          -Section2
               Some info for section2
               Some more info for section2
          -Section3
ID          Type
1            Type1
2            Type2

每个新级别缩进5个空格,所以-Server行缩进5个空格,-Section行缩进10个空格,一些信息行缩进15个空格......如果我的表格行格式为:

 $Table | Select-Object @{n='ID';e={$_.id}},@{n='Type';e={$_.type}} | Format-Table | Out-File $Output -Append

有没有办法让输出从第 15 个位置开始?

解决方法

首先将 Format-Table 的输出传递给 Out-String,以便您可以修改每一行。

$Table | Select-Object @{n='ID';e={$_.id}},@{n='Type';e={$_.type}} | 
    Format-Table | 
    Out-String -Stream |                 # Output each line separately
    ForEach-Object { ' ' * 15 + $_ } |   # Indent by 15 spaces
    Out-File $Output -Append

Out-String 参数 -Stream 用于将每一行分别传递给 ForEach-Object。默认情况下,Out-String 生成单个多行字符串,这将更难以处理。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...