Created
May 30, 2019 10:26
-
-
Save giuliov/36a254db11b435073c45d28138b9ee5f to your computer and use it in GitHub Desktop.
Run a process analyzing output for errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# see https://stackoverflow.com/questions/23239127/powershell-stream-process-output-and-errors-while-running-external-process/28440479 | |
function RunCommand | |
{ | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$True)] | |
[string] $FileName, | |
[Parameter(Mandatory=$True)] | |
[string[]] $Arguments, | |
[string] $OutputPrefix = "> ", | |
[string] $ErrorCountPattern = $null | |
) | |
# see https://social.technet.microsoft.com/Forums/office/en-US/07bbb0e5-0d31-451c-97e8-fad42361389f/passing-variable-to-registerobjectevent-action-block?forum=ITCG | |
$eventData = New-Object PSObject -Property @{ | |
# in | |
OutPrefix = $OutputPrefix | |
ErrorCountPattern = $ErrorCountPattern | |
# out | |
ErrCount = [int]0 | |
} | |
# Setup stdin\stdout redirection | |
$StartInfo = New-Object System.Diagnostics.ProcessStartInfo -Property @{ | |
FileName = $FileName | |
Arguments = $arguments | |
UseShellExecute = $false | |
RedirectStandardOutput = $true | |
RedirectStandardError = $true | |
CreateNoWindow = $true | |
} | |
# Create new process | |
$Process = New-Object System.Diagnostics.Process | |
# Assign previously created StartInfo properties | |
$Process.StartInfo = $StartInfo | |
# Register Object Events for stdin\stdout reading | |
$OutEvent = Register-ObjectEvent -InputObject $Process -EventName OutputDataReceived -Action { | |
$prefix = $Event.MessageData.OutPrefix | |
$line = $Event.SourceEventArgs.Data | |
Write-Host "${prefix}${line}" | |
} -MessageData $eventData | |
$ErrEvent = Register-ObjectEvent -InputObject $Process -EventName ErrorDataReceived -Action { | |
$prefix = $Event.MessageData.OutPrefix | |
$line = $Event.SourceEventArgs.Data | |
Write-Host "${prefix}${line}" | |
if ($line -match $Event.MessageData.ErrorCountPattern) { | |
$Event.MessageData.ErrCount = $Matches[1] | |
} | |
} -MessageData $eventData | |
# Start process | |
[void]$Process.Start() | |
# Begin reading stdin\stdout | |
$Process.BeginOutputReadLine() | |
$Process.BeginErrorReadLine() | |
# Do something else while events are firing | |
do | |
{ | |
# Write-Host 'Still alive!' -ForegroundColor Green | |
Start-Sleep -Seconds 1 | |
} | |
while (!$Process.HasExited) | |
# Unregister events | |
$OutEvent.Name, $ErrEvent.Name | | |
ForEach-Object { Unregister-Event -SourceIdentifier $_ } | |
$ExitCode = $Process.ExitCode | |
$Process.Dispose() | |
return @{ | |
ExitCode = $ExitCode | |
Errors = $eventData.ErrCount | |
} | |
} | |
$res = RunCommand -OutputPrefix 'MSDEPLOY> ' -ErrorCountPattern '^Error count:\s+(\d+)' -FileName 'C:\Program Files\IIS\Microsoft Web Deploy V3\MSDeploy.exe' -Arguments @( | |
"-verb:sync", | |
"-source:contentPath=c:\temp\test3", | |
"-dest:contentPath=c:\temp\test2" | |
#,"-whatif" | |
) | |
Write-Host "RC> $( $res.ExitCode )" -ForegroundColor Cyan | |
Write-Host "ERRCOUNT> $( $res.Errors )" -ForegroundColor Cyan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment