Private/Invoke-SimpleArmExecute.ps1
function Invoke-SimpleArmExecute { [CmdletBinding()] param ( [Parameter()] [string] $arguments, [Parameter()] [switch] $ignoreExitCode, [Parameter()] [switch] $skipBuffer ) Write-Verbose "Executing arm executation module with arguments :: $arguments" if ($skipBuffer) { Write-Information "Skipping buffering because of $skipBuffer argument" $bufferOutput = $false } # Process information $processInformation = New-Object System.Diagnostics.ProcessStartInfo $processInformation.CreateNoWindow = $true $processInformation.UseShellExecute = $false $processInformation.RedirectStandardError = $true $processInformation.RedirectStandardOutput = $true $processInformation.FileName = "cmd.exe" $processInformation.WorkingDirectory = Get-Location $processInformation.Arguments = $arguments # Process $process = New-Object System.Diagnostics.Process $process.StartInfo = $processInformation if ($bufferOutput) { $standardOutputBuffer = New-Object -TypeName System.Text.StringBuilder $standardErrorBuffer = New-Object -TypeName System.Text.StringBuilder $scriptBlock = { if (! [string]::IsNullOrEmpty($EventArgs.Data)) { $Event.MessageData.AppendLine($EventArgs.Data) } } $standardOutputBuffer = Register-ObjectEvent -InputObject $process -Action $scriptBlock -EventName "OutputDataReceived" -MessageData $standardOutputBuffer $standardErrorBuffer = Register-ObjectEvent -InputObject $process -Action $scriptBlock -EventName "OutputDataReceived" -MessageData $standardErrorBuffer } [void]$process.Start() if ($bufferOutput) { $process.BeginOutputReadLine(), $process.BeginErrorReadLine() } [void]$process.WaitForExit() if ($bufferOutput) { Unregister-Event -SourceIdentifier $standardErrorBuffer.Name Unregister-Event -SourceIdentifier $standardOutputBuffer.Name $standardOut = $standardOutputBuffer.ToString().Trim() $standardError = $standardErrorBuffer.ToString().Trim() } else { $standardOut = $process.StandardOutput.ReadToEnd() $standardError = $process.StandardError.ReadToEnd() } if ($standardOut -ne "") { Write-Verbose "-- Standard output $standardOut" } if ($standardError -ne "") { Write-Verbose "-- Standard output $standardError" } if (($process.ExitCode -ne 0) -and (-not $ignoreExitCode)) { throw [System.Management.Automation.RuntimeException] "Standard Error: $standardError [$($process.ExitCode)]" } return @{ "StdOut" = $standardOut; "StdErr" = $standardError; "ExitCode" = $process.ExitCode; "Pid" = $process.Id; "Name" = $process.Name; "FileName" = $process.StartInfo.FileName; "Arguments" = $process.StartInfo.Arguments; } } |