相关文章推荐
俊逸的大脸猫  ·  VC遍历文件夹下所有文件和文件夹-阿里云开发者社区·  5 月前    · 
光明磊落的眼镜  ·  列表中存在多个字典时,用Python提取每个 ...·  6 月前    · 
风流的板凳  ·  小程序开发使用nodejs的Buffer.f ...·  8 月前    · 
讲道义的山羊  ·  golang json 字符串转对象-掘金·  1 年前    · 
不拘小节的吐司  ·  如果vm停止或计费帐户断开连接,Google ...·  1 年前    · 
Code  ›  开发者社区
https://cloud.tencent.com/developer/ask/sof/93630
大力的饺子
1 年前
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
提问
问 如何在PowerShell中输出没有换行符的文本?
Stack Overflow用户
提问于 2010-10-09 23:49:33
EN

我希望我的PowerShell脚本打印如下内容:

Enabling feature XYZ......Done

该脚本如下所示:

Write-Output "Enabling feature XYZ......."
Enable-SPFeature...
Write-Output "Done"

但是 Write-Output 总是在末尾打印一个新行,所以我的输出不是在一行上。有没有办法做到这一点?

14 282.6K 0 票数 183
EN
powershell

回答 14

Stack Overflow用户

发布于 2015-09-15 22:05:10

不幸的是,正如在一些回答和评论中指出的那样, Write-Host 可能是危险的,并且不能通过管道传输到其他进程,并且 Write-Output 没有 -NoNewline 标志。

但这些方法是显示进度的"*nix“方式," PowerShell”方式似乎是 Write-Progress :它在PowerShell窗口的顶部显示进度信息栏,从PowerShell 3.0开始提供, see manual 提供详细信息。

# Total time to sleep
$start_sleep = 120
# Time to sleep between each notification
$sleep_iteration = 30
Write-Output ( "Sleeping {0} seconds ... " -f ($start_sleep) )
for ($i=1 ; $i -le ([int]$start_sleep/$sleep_iteration) ; $i++) {
    Start-Sleep -Seconds $sleep_iteration
    Write-Progress -CurrentOperation ("Sleep {0}s" -f ($start_sleep)) ( " {0}s ..." -f ($i*$sleep_iteration) )
Write-Progress -CurrentOperation ("Sleep {0}s" -f ($start_sleep)) -Completed "Done waiting for X to finish"

以OP的例子为例:

# For the file log
Write-Output "Enabling feature XYZ"
# For the operator
Write-Progress -CurrentOperation "EnablingFeatureXYZ" ( "Enabling feature XYZ ... " )
Enable-SPFeature...
# For the operator
Write-Progress -CurrentOperation "EnablingFeatureXYZ" ( "Enabling feature XYZ ... Done" )
# For the log file
 
推荐文章
俊逸的大脸猫  ·  VC遍历文件夹下所有文件和文件夹-阿里云开发者社区
5 月前
光明磊落的眼镜  ·  列表中存在多个字典时,用Python提取每个字典的一个key值_列表中的字典怎么按key取值-CSDN博客
6 月前
风流的板凳  ·  小程序开发使用nodejs的Buffer.from一直报错 | 微信开放社区
8 月前
讲道义的山羊  ·  golang json 字符串转对象-掘金
1 年前
不拘小节的吐司  ·  如果vm停止或计费帐户断开连接,Google Cloud SQL数据库是否会自动删除?-腾讯云开发者社区-腾讯云
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号