Ami*_*t G 137 powershell
我希望我的PowerShell脚本能够打印出这样的内容:
Enabling feature XYZ......Done
Run Code Online (Sandbox Code Playgroud)
脚本看起来像这样:
Write-Output "Enabling feature XYZ......."
Enable-SPFeature...
Write-Output "Done"
Run Code Online (Sandbox Code Playgroud)
但Write-Output
总是在最后打印一个新行,所以我的输出不在一行.有没有办法做到这一点?
Sha*_*evy 157
__PRE__
Tho*_*mas 42
不幸的是,正如几个答案和评论中所指出的那样,Write-Host
可能是危险的,不能通过管道输送到其他进程并且Write-Output
没有-NoNewline
标志.
但是,这些方法是"*nix的"方式来显示进程中,"的PowerShell"的方式做到这一点似乎是Write-Progress
:它会显示与进度信息PowerShell窗口顶部从PowerShell的3.0条,可用以后,看说明书的细节.
# 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"
Run Code Online (Sandbox Code Playgroud)
并采取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
Write-Output "Feature XYZ enabled"
Run Code Online (Sandbox Code Playgroud)
shu*_*ler 14
虽然它可能不适用于您的情况(因为您向用户提供信息输出),创建一个可用于追加输出的字符串.当输出它时,只输出字符串.
当然忽略这个例子在你的情况下很愚蠢但在概念上很有用:
$output = "Enabling feature XYZ......."
Enable-SPFeature...
$output += "Done"
Write-Output $output
Run Code Online (Sandbox Code Playgroud)
显示:
Enabling feature XYZ.......Done
Run Code Online (Sandbox Code Playgroud)
是的,因为其他答案都有状态,所以无法使用Write-Output完成。如果PowerShell发生故障,请转到.NET,这里甚至还有几个.NET答案,但它们比实际需要的要复杂得多。
只需使用:
[Console]::Write("Enabling feature XYZ.......")
Enable-SPFeature...
Write-Output "Done"
Run Code Online (Sandbox Code Playgroud)
它不是最纯的PowerShell,但可以运行。
要写入文件,可以使用字节数组。以下示例创建一个空 ZIP 文件,您可以向其中添加文件:
[Byte[]] $zipHeader = 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
[System.IO.File]::WriteAllBytes("C:\My.zip", $zipHeader)
Run Code Online (Sandbox Code Playgroud)
或者使用:
[Byte[]] $text = [System.Text.Encoding]::UTF8.getBytes("Enabling feature XYZ.......")
[System.IO.File]::WriteAllBytes("C:\My.zip", $text)
Run Code Online (Sandbox Code Playgroud)
在 PowerShell 中似乎没有办法做到这一点。以前的所有答案都不正确,因为它们的行为方式不同Write-Output
,但更像Write-Host
是没有这个问题。
Write-Host
closes 解决方案似乎与参数一起使用-NoNewLine
。您不能通过管道传输这通常是一个问题,但有一种方法可以覆盖此函数,如Write-Host => Export to a file中所述,因此您可以轻松地使其接受输出文件的参数。这还远不是一个好的解决方案。这样Start-Transcript
更有用,但该 cmdlet 在本机应用程序中存在问题。
Write-Output
根本无法在一般情况下做你需要的事情。
2023 年编辑:
你可以疯狂地做这样的事情:
function log ($msg, [switch] $NoNewLine)
{
$script:_msg+=$msg
if ($NoNewLine)
{
Write-Host -NoNewline $msg
}
else
{
Write-Host $msg
$script:_msg >> output.txt
$script:_msg = ''
}
}
Run Code Online (Sandbox Code Playgroud)
测试:
log 'enabling feature xyx ' -NoNewLine; 1..3 | % { sleep 1; log . -NoNewLine }; log ' ok'; cat output.txt`
> enabling feature xyx ... ok
Run Code Online (Sandbox Code Playgroud)