Powershell 添加内容换行符
我试图弄清楚如何在使用 add-content 时消除换行符
echo $server "Uptime: " $uptime.days ", Days" $uptime.hours ", Hours" $uptime.minutes ", Minutes" | add-content $output_file
基本上我一直在尝试让服务器的正常运行时间转到文本文件,当我这样做时,输出出来
HOSTNAME
Uptime:
, 2 Days
2
, Hours
15
, Minutes
我查看了这个问题: Powershell 替换丢失换行符
另外,我从使用 out-file -append 改为 add-内容,但是两者都会产生相似的结果,有人可以阐明我如何消除中断吗?
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
I looked at this question: Powershell replace lose line breaks
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜你想要一行包含信息,然后:
如果每个项目都应该在单独的行上,你可以添加 `n
另一种可能性是使用
-f
其中有时更具可读性:更新
echo
是Write-Output
(Get-Alias -name echo
) 的别名,在您的情况下会生成对象数组。该数组被传递给Add-Content
;每个对象都存储在自己的行中。I guess you want to have one line with the info, then:
If every item should be on separate line, you might add `n
Other possibility is to use
-f
which is sometimes more readable:Update
echo
is alias forWrite-Output
(Get-Alias -name echo
) which in your case produces array of objects. This array is passed toAdd-Content
; each object is stored on its own line.回避 PowerShell 可能在换行符中出现的任何问题的最简单方法是避免使用提供程序。
通过使用 [IO.File]::WriteAllText 写入文件,您应该能够避免来自 PowerShell 的换行符。唯一需要注意的是 [IO.File]::WriteAllText 不理解 PowerShell 路径,因此您需要向其传递绝对路径。
希望这有帮助,
The simplest way to sidestep any problem PowerShell might be putting into the line breaks would be to avoid using the providers.
By using [IO.File]::WriteAllText to write the file, you should be able to avoid the linebreaks that come from PowerShell. The only caveat is that [IO.File]::WriteAllText doesn't understand PowerShell paths, so you'll need to pass it an absolute path.
Hope this helps,
怎么样
how about