-
Notifications
You must be signed in to change notification settings - Fork 176
/
RunProcess.ps1
56 lines (51 loc) · 1.62 KB
/
RunProcess.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Runs the given executable with the given parameters
.DESCRIPTION
runs the executable passed as parameter, with the given arguments, in the given working folder. Wait for process to exit, and fail if exit code is not 0
.PARAMETER workingDirectory
specify the folder used as working folder for the executable. Omit if you want to use the current working folder.
.PARAMETER exeToRun
specify the path to the executable to run.
.PARAMETER arguments
specify the arguments passed to the executable. Omit if the executable does not require arguments.
#>
param ([string] $exeToRun, [string] $arguments, [string] $workingDirectory)
try
{
$currentPath = Get-Location
$Info = New-Object System.Diagnostics.ProcessStartInfo
$Info.FileName = $exeToRun
$Info.Arguments = $arguments
if ($workingDirectory)
{
if ([System.IO.Path]::IsPathRooted($workingDirectory))
{
$Info.WorkingDirectory = $workingDirectory
}
else
{
$Info.WorkingDirectory = Join-Path $currentPath $workingDirectory
}
}
else
{
$Info.WorkingDirectory = $currentPath
}
Write-Host "running $($Info.FileName) $($Info.arguments), working folder $($Info.WorkingDirectory)"
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $Info
$Process.Start()
$Process.WaitForExit()
Write-Host "process exit code : $($Process.ExitCode)"
if ($Process.ExitCode -ne 0)
{
Write-Error "error detected, exit with code $($Process.ExitCode)"
Exit $Process.ExitCode
}
}
catch
{
Write-Error $PSItem.Exception
exit 1
}