Powershell 添加内容换行符

发布于 2024-10-27 14:34:18 字数 515 浏览 7 评论 0原文

我试图弄清楚如何在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍暮颜 2024-11-03 14:34:18

我猜你想要一行包含信息,然后:

"$server Uptime: $($uptime.days) Days, $($uptime.hours) Hours, $($uptime.minutes) Minutes" | add-content $output_file

如果每个项目都应该在单独的行上,你可以添加 `n

"$server Uptime`n$($uptime.days) Days`n$($uptime.hours) Hours`n$($uptime.minutes) Minutes" | add-content $output_file

另一种可能性是使用 -f 其中有时更具可读性:

"$server Uptime: {0} Days, {1} Hours, {2} Minutes" -f $uptime.days, $uptime.hours, $uptime.minutes | add-content $output_file

更新
echoWrite-Output (Get-Alias -name echo) 的别名,在您的情况下会生成对象数组。该数组被传递给Add-Content;每个对象都存储在自己的行中。

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

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

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

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.

浊酒尽余欢 2024-11-03 14:34:18

回避 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,

夜夜流光相皎洁 2024-11-03 14:34:18

怎么样

[IO.File]::AppendAllText($testfile,"abc",[System.Text.Encoding]::UTF8)

how about

[IO.File]::AppendAllText($testfile,"abc",[System.Text.Encoding]::UTF8)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文