3

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?

1 Answer 1

5

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:"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.