VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Split output in multiple lines

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Did you try with

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then try joining the lines with just a CR

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Lucd,

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And what does this return?

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

May I know what is CR-LF?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

I hereby attached the output, I see below output

ganapa2000_0-1630325191048.png

ganapa2000_0-1630325361095.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

that worked perfect!!! Thank you very much 🙂

0 Kudos