簡體   English   中英

powershell 寫入日志文件

[英]powershell writing to a log file

我已經編寫了下面的代碼,除了其中一個變量超過 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"

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

如果您不確定,我會使用格式化(希望我理解您的問題)。

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

Out-File 有一個 -Width 參數可能會有所幫助。 或者您可以將 long 變量格式化為 here 字符串嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM