问题描述
好的,我正在对多个服务器运行查询。我的数据正在输出到格式如下的文本文件:
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
生成单个多行字符串,这将更难以处理。