PowerCLI

 View Only
Expand all | Collapse all

Split output in multiple lines

  • 1.  Split output in multiple lines

    Posted Aug 27, 2021 04:47 PM

    Hi,

    I am trying to get the output from linux VM and I am getting the output in single line. But I would like to split the output in multiple lines

    I tried as $(($result.output) -split '.') but no luck

    Please help!!!

    Script

    $session = New-SSHSession -ComputerName $vm -Credential $Cred –AcceptKey
    $result = Invoke-SSHCommand -SSHSession $session -Command $cmd
    $result.output

    Output

    McAfee agent service is already running. McAfee common services is already running. McAfee compat service is already running

    I would like to get this output in multiple lines as below

    McAfee agent service is already running.

    McAfee common services is already running.

    McAfee compat service is already running



  • 2.  RE: Split output in multiple lines

    Posted Aug 27, 2021 05:29 PM

    Did you try with

    $result.Output.Split('.').TrimStart(' ') -join "`n"


  • 3.  RE: Split output in multiple lines

    Posted Aug 28, 2021 01:14 PM

    LucD,

    I am getting the output with extra blank line as below

    McAfee agent service is already running.

    McAfee common services is already running.

    McAfee compat service is already running.

     

    but I would like to see without the blank line in between as below.

    McAfee agent service is already running.
    McAfee common services is already running.
    McAfee compat service is already running.



  • 4.  RE: Split output in multiple lines

    Posted Aug 28, 2021 02:24 PM

    Then try joining the lines with just a CR

    $result.Output.Split('.').TrimStart(' ') -join "`r"


  • 5.  RE: Split output in multiple lines

    Posted Aug 28, 2021 04:07 PM

    Lucd,

    Above command is omitting first two lines and I am getting only the last line.



  • 6.  RE: Split output in multiple lines

    Posted Aug 28, 2021 05:07 PM

    And what does this return?

    $result.Output.Split('.').TrimStart(' ')


  • 7.  RE: Split output in multiple lines

    Posted Aug 28, 2021 05:38 PM

    it give me output in one single line.

    McAfee agent service is already running McAfee common services is already running McAfee compat service is already running



  • 8.  RE: Split output in multiple lines

    Posted Aug 28, 2021 05:57 PM

    You will have to check what exactly is in the output.
    Are there any CR-LF already in there?

    I don't have that SW so I can't check I'm afraid.



  • 9.  RE: Split output in multiple lines

    Posted Aug 28, 2021 06:48 PM

    May I know what is CR-LF?



  • 10.  RE: Split output in multiple lines

    Posted Aug 28, 2021 07:18 PM

    Carriage Return (`r) and Line Feed (`n).

    Save the output to text file (Set-Content), then open the file in an editor that can display hex codes (like for example Notepad++).
    Look for 0x0A and 0x0D between the 3 lines.

    Depending on the OS on which the output is generated (Windows or Linux), you might see CR-LF or LF.



  • 11.  RE: Split output in multiple lines

    Posted Aug 30, 2021 12:07 PM
      |   view attached

    Hi LucD,

    I hereby attached the output, I see below output

    ganapa2000_0-1630325191048.png

    ganapa2000_0-1630325361095.png

     

    Attachment(s)

    txt
    output235.txt   270 B 1 version


  • 12.  RE: Split output in multiple lines
    Best Answer

    Posted Aug 30, 2021 12:28 PM

    If that is the actual content of $result.Output, try with this

    $result.Output.Replace("`r`n",'|').Split('|').Where{$_ -ne ''}

    The reason for the Replace is that a Split only works on 1 character at the time, not a string



  • 13.  RE: Split output in multiple lines

    Posted Aug 30, 2021 03:05 PM

    that worked perfect!!! Thank you very much