-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolFunctions.ps1
More file actions
123 lines (103 loc) · 3.83 KB
/
ToolFunctions.ps1
File metadata and controls
123 lines (103 loc) · 3.83 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.SYNOPSIS
Asserts the agent version is at least the specified minimum.
.PARAMETER Minimum
Minimum version - must be 2.104.1 or higher.
#>
function Assert-Agent {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[version]$Minimum)
if (([version]'2.104.1').CompareTo($Minimum) -ge 1) {
Write-Error "Assert-Agent requires the parameter to be 2.104.1 or higher."
return
}
$agent = Get-TaskVariable -Name 'agent.version'
if (!$agent -or $Minimum.CompareTo([version]$agent) -ge 1) {
Write-Error (Get-LocString -Key 'PSLIB_AgentVersion0Required' -ArgumentList $Minimum)
}
}
<#
.SYNOPSIS
Asserts that a path exists. Throws if the path does not exist.
.PARAMETER PassThru
True to return the path.
#>
function Assert-Path {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$LiteralPath,
[Microsoft.PowerShell.Commands.TestPathType]$PathType = [Microsoft.PowerShell.Commands.TestPathType]::Any,
[switch]$PassThru)
if ($PathType -eq [Microsoft.PowerShell.Commands.TestPathType]::Any) {
Write-Verbose "Asserting path exists: '$LiteralPath'"
} else {
Write-Verbose "Asserting $("$PathType".ToLowerInvariant()) path exists: '$LiteralPath'"
}
if (Test-Path -LiteralPath $LiteralPath -PathType $PathType) {
if ($PassThru) {
return $LiteralPath
}
return
}
$resourceKey = switch ($PathType) {
([Microsoft.PowerShell.Commands.TestPathType]::Container) { "PSLIB_ContainerPathNotFound0" ; break }
([Microsoft.PowerShell.Commands.TestPathType]::Leaf) { "PSLIB_LeafPathNotFound0" ; break }
default { "PSLIB_PathNotFound0" }
}
throw (Get-LocString -Key $resourceKey -ArgumentList $LiteralPath)
}
<#
.SYNOPSIS
Executes an external program.
.DESCRIPTION
Executes an external program and waits for the process to exit.
After calling this command, the exit code of the process can be retrieved from the variable $LASTEXITCODE.
.PARAMETER Encoding
This parameter not required for most scenarios. Indicates how to interpret the encoding from the external program. An example use case would be if an external program outputs UTF-16 XML and the output needs to be parsed.
.PARAMETER RequireExitCodeZero
Indicates whether to write an error to the error pipeline if the exit code is not zero.
#>
function Invoke-Tool { # TODO: RENAME TO INVOKE-PROCESS?
[CmdletBinding()]
param(
[ValidatePattern('^[^\r\n]*$')]
[Parameter(Mandatory = $true)]
[string]$FileName,
[ValidatePattern('^[^\r\n]*$')]
[Parameter()]
[string]$Arguments,
[string]$WorkingDirectory,
[System.Text.Encoding]$Encoding,
[switch]$RequireExitCodeZero)
Trace-EnteringInvocation $MyInvocation
$isPushed = $false
$originalEncoding = $null
try {
if ($Encoding) {
$originalEncoding = [System.Console]::OutputEncoding
[System.Console]::OutputEncoding = $Encoding
}
if ($WorkingDirectory) {
Push-Location -LiteralPath $WorkingDirectory -ErrorAction Stop
$isPushed = $true
}
$FileName = $FileName.Replace('"', '').Replace("'", "''")
Write-Host "##[command]""$FileName"" $Arguments"
Invoke-Expression "& '$FileName' --% $Arguments"
Write-Verbose "Exit code: $LASTEXITCODE"
if ($RequireExitCodeZero -and $LASTEXITCODE -ne 0) {
Write-Error (Get-LocString -Key PSLIB_Process0ExitedWithCode1 -ArgumentList ([System.IO.Path]::GetFileName($FileName)), $LASTEXITCODE)
}
} finally {
if ($originalEncoding) {
[System.Console]::OutputEncoding = $originalEncoding
}
if ($isPushed) {
Pop-Location
}
Trace-LeavingInvocation $MyInvocation
}
}