Avoiding newline in write-output

前端 未结 4 949
故里飘歌
故里飘歌 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.

提交回复
热议问题