简体   繁体   English

Powershell:Select-String不解析此变量

[英]Powershell: Select-String not parsing this variable

PowerShell rookie here. PowerShell新秀在这里。 I'm stuck trying to get Select-String to parse the variable below. 我试图让Select-String解析下面的变量。

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. 我正在采用标准输出并尝试解析我关心的几个项目,所以我只看到那些,而不是其他50条其他信息。

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: 因此,为了尝试排除故障,我将$output发送到一个文本文件,然后运行下面的内容,这就是我得到的:

$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? 任何见解?

You are reading the standard output using StreamReader 's ReadToEnd() method. 您正在使用StreamReaderReadToEnd()方法读取标准输出。 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. 因此,当您在管道上输出此字符串时,Select-String只会看到一个大字符串,而不是每个单独的行。 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:"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM