简体   繁体   English

powershell 写入日志文件

[英]powershell writing to a log file

I've written the code below which works fine apart from one of the variables is over 200 characters and goes onto the next line messing the layout up.我已经编写了下面的代码,除了其中一个变量超过 200 个字符并进入下一行之外,它工作正常,导致布局混乱。 Any suggestions on a better layout so if one of the variables is long, it still retains structure?关于更好布局的任何建议,如果其中一个变量很长,它仍然保留结构?

$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"

Example output:示例 output:

"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"

I would use formatting if you are not sure (I hope I understand your problem).如果您不确定,我会使用格式化(希望我理解您的问题)。 See

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

Then..然后..

$WriteLog = $stuff1, $stuff2, $stuff3, $stuff4, $stuff5, $stuff6 | 
               Foreach-Object { "Here is some stuff:{0,20}" -f $_ }
$WriteLog = $WriteLog -join "`n"

Out-File has a -Width parameter that might help. Out-File 有一个 -Width 参数可能会有所帮助。 Or can you format the long variable as a here string?或者您可以将 long 变量格式化为 here 字符串吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM