Powershell: Select-String not parsing this variable

parsing, powershell, powershell-2.0, variables

Solution

You are reading the standard output using StreamReader's ReadToEnd() method. This will return a single string containing the contents, including the carriage returns. So, when you output this string on the pipeline, Select-String only sees that one big string, not each individual line. What you could do is split the string on the carriage returns before passing it down the pipeline:

$output -split "`n" | Select-String "Copy New","Copy Over","Delete File","Items Synced","Changes:"

Problem

PowerShell rookie here. I'm stuck trying to get Select-String to parse the variable below. I'm calling an external program which logs everything to standard output. This is for a backup sync software I'm using. The output is a lot of information such as source, destination, copied files, changed files, etc. I only care about a few items from the standard output. I'm taking the standard output and trying to parse it for the few items I care about so I only see those, rather than 50 other lines of miscellaneous information. Here's the code: ``` $procInfo = New-Object System.Diagnostics.ProcessStartInfo $procInfo.FileName = "C:\Program Files\Siber Systems\GoodSync\gsync.exe" $procInfo.RedirectStandardError = $true $procInfo.RedirectStandardOutput = $true $procInfo.UseShellExecute = $false $procInfo.Arguments = "/progress=yes /exit sync DroboBackup" $proc = New-Object System.Diagnostics.Process $proc.StartInfo = $procInfo $proc.Start() | Out-Null (Get-Date -format T) + ": Backup Process Running. Please Stand By..." $proc.WaitForExit() if ($proc.ExitCode -eq 0) { "GoodSync Reported: Analyze or Sync Successfully Completed." } elseif ($proc.ExitCode -eq 1) { "GoodSync Error: Analyze had Terminal Errors. Did gsync.exe close abruptly?" } elseif ($proc.ExitCode -eq 2) { "GoodSync Error: Sync had Terminal Errors. Did gsync.exe close abruptly?" } else { "GoodSync Error: General Error. Typo in job name?" } $output = $proc.StandardOutput.ReadToEnd() $output += $proc.StandardError.ReadToEnd() #$output | Out-File c:\output.txt -Append $newOutput = $output | Select-String "Copy New","Copy Over","Delete File","Items Synced","Changes:" $newOutput ``` What I get after running this is all lines from standard output. I'm not getting my nice and clean parsed return. So, to try and troubleshoot, I sent `$output` to a text file, then ran the below, and this is what I got: ``` $x = Get-Content c:\output.txt | Select-String "Copy New","Copy Over","Delete File","Items Synced","Changes:" $x ``` ``` PS C:\> .\test.ps1 Changes: 0, Conflicts: 0, CopyTime: 0, CopyState: 0/0, Errors: 0 ``` So as you can see, it is working against the text file, but not as the variable. Any insight?

Original source