Avoiding newline in write-output

前端 未结 4 875
故里飘歌
故里飘歌 2020-12-03 17:23

I want to collect all output from my script in a log file and must use write-output instaed of write-host.

Write-Output \"Server:\" $a looks like

相关标签:
4条回答
  • 2020-12-03 17:34

    9 hours ... I start an answer.

    In Powershell everything you manipulate is an object.

    so "Server:" is an object, $a is an object

    PS> "server :".gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     String                                   System.Object
    

    Write-output is a CmdLet that put object in a kind of list (a pipe) to be used by other CmdLets or scripts. So there is not a really a newline between "Server:" and "foo". It's the way the console show you a list (an array) of objects. As you can see here under :

    PS> $a = "foo"
    PS> (Write-Output "Server :" $a).gettype()
    
    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Object[]                                 System.Array
    

    Clearly it's an abstract here, but I hope it can make you understand.

    0 讨论(0)
  • 2020-12-03 17:35

    Found somewhere on the Internet:

    Write-Output "Server: $($a)"
    

    Works great in my code.

    0 讨论(0)
  • 2020-12-03 17:38
    Write-Output "Server: $a"
    Write-Output ("Server: {0}" -f $a)
    Write-Output ("Server: " + $a)
    

    If you want to collect the output from a script into a log file, consider using Start-Transcript. It logs every command and all PowerShell output to a file. It doesn't log anything sent to stdout, however, so if you're using any legacy commands, you'll have to pipe their output to Write-Host:

    Start-Transcript C:\logs\mylog.txt
    Write-Host "Server: " $a
    ping | Write-Host
    
    0 讨论(0)
  • 2020-12-03 17:40

    There's good information in the existing answers, but let me attempt a pragmatic summary:

    • When printing to the console or redirecting to a file, Write-Output separates multiple arguments by a newline each.

    • Therefore, in order to produce single-line output, you must "pre-assemble" the parts to output into a single string that you pass as a single argument.

      • Aaron Jensen's helpful answer shows you 3 alternative ways of doing that.
    • Except to suppress enumeration of collections, you generally do not need to use Write-Output explicitly, because PowerShell by default sends anything that is not captured in a variable or redirected elsewhere or sent through the pipeline to the [success] output stream (which Write-Output also writes to); thus, in the case at hand, the following is sufficient:

    "Server: $a"  # what this expandable string expands to is *implicitly* output
    

    What may be confusing is that the Write-Host cmdlet acts differently, but it's important to note that it has a different purpose.

    # IMPORTANT: Do not use Write-Host to output *data*; only use it to write
    #            directly to the host (console).
    PS> Write-Host "Server:"     $a    # multiple spaces used on purpose
    Server: srv1
    

    Write-Host, unlike Write-Output, separates multiple arguments by a single space each.
    There are other important differences, which are summarized in this answer of mine.


    Given the generic title of the question, let's also address how to suppress a trailing newline:

    • For printing to the console only, you can use Write-Host -NoNewline.

    • With data output, be it via Write-Output, implicit output, or from another command:

      • You cannot prevent a trailing newline when the output is sent to the console.

      • Unfortunately, as of Windows PowerShell v5.1 / PowerShell Core v6.0.0, you cannot prevent a trailing newline when sending text to external programs via the pipeline - see this GitHub issue.

      • In PSv5+, however, you can prevent a trailing newline when capturing output in a file, by using Out-File -NoNewline or Set-Content -NoNewline (in PSv4-, you must use the .NET Framework directly); by contrast, redirecting to a file with > does append a trailing newline.

        • Caveat: If you're outputting multiple objects, -NoNewline not only suppresses a trailing newline, but also newlines between these objects.
        • For the differences between Out-File and Set-Content and when to choose which, see this answer of mine.
    0 讨论(0)
提交回复
热议问题