Iterating through a variable line by line

I have a small script that outputs text to a variable. I need to go through it one at a time to disassemble it. I could do this by dumping the variable into a text file and then reading it with Get-Content, but that seems a bit redundant.

My script connects to the Fortigate block and fires a specific request. This answer is what I am looking for to analyze.

New-SshSession 10.0.0.138 -Port 65432 -Credential (Get-Credential) -AcceptKey
$command = 'config router policy
show'
$result = Invoke-SSHCommand -Index 0 -Command $command

      

+3


source to share


2 answers


As you describe, your variable is a string with newlines. You can turn it into a one-line string array by calling this:



$result = $result -split "`r`n"

      

+5


source


ForEach ($line in $($result -split "`r`n"))
{
    Write-host $Line
}

      



+8


source







All Articles