0

我编写了下面的代码,除了其中一个变量超过 200 个字符外,它工作正常,并进入下一行,弄乱了布局。关于更好的布局的任何建议,所以如果其中一个变量很长,它仍然保留结构?

$WriteLog = $null 
$WriteLog = @()
$WriteLog +="Here is some stuff:                $stuff1"
$WriteLog +="Here is some stuff:                $stuff2"
$WriteLog +="Here is some stuff:                $stuff3"
$WriteLog +="Here is some stuff:                $stuff4"
$WriteLog +="Here is some stuff:                $stuff5"
$WriteLog +="Here is some stuff:                $stuff6"
Out-File -InputObject $WriteLog -Append -NoClobber -FilePath "$env:SystemDrive\somestuff\somestuff.txt"

示例输出:

"Here is some stuff:                    stuff1"
"Here is some stuff:            stuff2"  
"Here is some stuff:                Heres a lot of stuff
stuff3, stuff3, stuff3, stuff3,  stuff3, stuff3" 
"Here is some stuff:            stuff4"  
"Here is some stuff:                    stuff5"
"Here is some stuff:                    stuff6"
4

2 回答 2

1

Out-File 有一个 -Width 参数可能会有所帮助。或者您可以将长变量格式化为此处的字符串吗?

于 2012-04-09T20:04:31.727 回答
1

如果您不确定,我会使用格式(我希望我理解您的问题)。看

'{0,5} -f 5
#vs.
'{0,5} -f 1115

然后..

$WriteLog = $stuff1, $stuff2, $stuff3, $stuff4, $stuff5, $stuff6 | 
               Foreach-Object { "Here is some stuff:{0,20}" -f $_ }
$WriteLog = $WriteLog -join "`n"
于 2012-04-09T19:10:11.937 回答