Powershell添加内容换行符 [英] Powershell add-content line breaks
问题描述
我试图弄清楚如何在使用添加内容时消除换行符
I am trying to figure out how to eliminate line breaks when using add-content
echo $server "Uptime: " $uptime.days ", Days" $uptime.hours ", Hours" $uptime.minutes ", Minutes" | add-content $output_file
基本上,我一直在尝试使服务器的正常运行时间转到文本文件,而当我这样做时,输出结果就会出现
Basically I have been trying to get the uptime of a server to go to a text file, and when I do this the output comes out
HOSTNAME
Uptime:
, 2 Days
2
, Hours
15
, Minutes
我看了这个问题: Powershell替换丢失了换行符
我也从使用out-file -append转到add-content,但是两者都产生了相似的结果,有人可以对我如何消除这些中断有所了解吗?
Also I went from using out-file -append to add-content, however both produce similar results, can someone shed to some light on how I can eliminate the breaks?
推荐答案
我想您要在信息中加上一行,然后:
I guess you want to have one line with the info, then:
"$server Uptime: $($uptime.days) Days, $($uptime.hours) Hours, $($uptime.minutes) Minutes" | add-content $output_file
如果每个项目都应放在单独的行上,则可以添加`n
If every item should be on separate line, you might add `n
"$server Uptime`n$($uptime.days) Days`n$($uptime.hours) Hours`n$($uptime.minutes) Minutes" | add-content $output_file
其他可能性是使用-f
,它有时更易读:
Other possibility is to use -f
which is sometimes more readable:
"$server Uptime: {0} Days, {1} Hours, {2} Minutes" -f $uptime.days, $uptime.hours, $uptime.minutes | add-content $output_file
更新
echo
是Write-Output
(Get-Alias -name echo
)的别名,在您的情况下,它会生成对象数组.此数组传递给Add-Content
;每个对象都存储在自己的行中.
Update
echo
is alias for Write-Output
(Get-Alias -name echo
) which in your case produces array of objects. This array is passed to Add-Content
; each object is stored on its own line.
这篇关于Powershell添加内容换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!