-
Notifications
You must be signed in to change notification settings - Fork 15
/
Start-NewProcess.ps1
31 lines (27 loc) · 1.16 KB
/
Start-NewProcess.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Function Start-NewProcess {
Param (
[string]$FilePath,
[string]$Arguments
)
Try {
$Info = New-Object System.Diagnostics.ProcessStartInfo
$Process = New-Object System.Diagnostics.Process
$Info.FileName = $FilePath
$Info.RedirectStandardError = $true
$Info.RedirectStandardOutput = $true
$Info.UseShellExecute = $false
$Info.Arguments = $Arguments
$Info.CreateNoWindow = $true
$Process.StartInfo = $Info
$Process.Start() | Out-Null
[string]$stdOut = $Process.StandardOutput.ReadToEnd()
[string]$stdErr = $Process.StandardError.ReadToEnd()
$Process.WaitForExit(10000) # Wait maximum of 10 seconds
If ($process.ExitCode -eq 0) { Return ($stdOut.Split("`n")) } # Standard Output
Else {
If ($stdErr.Length -gt 0) { Return ($stdErr.Split("`n")) } # Standard Error (if it exists)
Else { Return ($stdOut.Split("`n")) } # Standard Output (if there are no errors)
}
}
Catch { Return "ERROR: $($_.Exception.Message)" }
}