powershell写入日志文件

时间:2012-04-09 18:28:42

标签: file logging powershell

我已经编写了下面的代码,除了其中一个变量超过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"

2 个答案:

答案 0 :(得分: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"

答案 1 :(得分:1)

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