-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Utility.ps1
203 lines (178 loc) · 7.22 KB
/
Utility.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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function Get-SavedModuleContainerPath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[bool]
$isWin
)
if ($isWin) {
return $env:SystemDrive + "\Modules";
} else {
return "/usr/share";
}
}
function Test-IsHostedAgentPathPresent {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[bool]
$isWin
)
$containerPath = Get-SavedModuleContainerPath -isWin $isWin
return Test-Path (Join-Path $containerPath "az_*")
}
function Get-SavedModulePath {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]
$azurePowerShellVersion,
[Parameter(Mandatory = $true)]
[bool]
$isWin
)
$savedModulePath = Join-Path (Get-SavedModuleContainerPath -isWin $isWin) "az_$azurePowerShellVersion"
Write-Verbose "The value of the module path is: $savedModulePath"
return $savedModulePath
}
function Expand-ModuleZip {
param (
[string] [Parameter(Mandatory = $true)]
$zipPath,
[string] [Parameter(Mandatory = $true)]
$destination,
[bool] [Parameter(Mandatory=$true)]
$isWin
)
if ($isWin) {
$parameter = @("x", "-o$destination", "$zipPath")
$command = "$PSScriptRoot\7zip\7z.exe"
&$command @parameter
} else {
$prevProgressPref = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
Expand-Archive -Path $zipPath -DestinationPath $destination
$ProgressPreference = $prevProgressPref
}
}
function Update-PSModulePathForHostedAgent {
[CmdletBinding()]
param([string] $targetAzurePs)
try {
if ($targetAzurePs) {
$hostedAgentAzModulePath = Get-SavedModulePath -azurePowerShellVersion $targetAzurePs -isWin $true
}
else {
$hostedAgentAzModulePath = Get-LatestModule -patternToMatch "^az_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$"
}
$env:PSModulePath = $hostedAgentAzModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
} finally {
Write-Verbose "The updated value of the PSModulePath is: $($env:PSModulePath)"
}
}
function Update-PSModulePathForHostedAgentLinux {
[CmdletBinding()]
param([string] $targetAzurePs)
try {
if ($targetAzurePs) {
$hostedAgentAzModulePath = Get-SavedModulePath -azurePowerShellVersion $targetAzurePs -isWin $false
if(!(Test-Path $hostedAgentAzModulePath)) {
Write-Verbose "No module path found with this name"
throw ("Could not find the module path with given version.")
}
}
else {
$hostedAgentAzModulePath = Get-LatestModuleLinux -patternToMatch "^az_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$"
}
$env:PSModulePath = $hostedAgentAzModulePath + ":" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(':')
} finally {
Write-Verbose "The updated value of the PSModulePath is: $($env:PSModulePath)"
}
}
function Get-LatestModule {
[CmdletBinding()]
param([string] $patternToMatch,
[string] $patternToExtract)
$resultFolder = ""
$regexToMatch = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList $patternToMatch
$regexToExtract = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList $patternToExtract
$maxVersion = [version] "0.0.0"
$modulePath = $env:SystemDrive + "\Modules";
try {
if (-not (Test-Path -Path $modulePath)) {
return $resultFolder
}
$moduleFolders = Get-ChildItem -Directory -Path $modulePath | Where-Object { $regexToMatch.IsMatch($_.Name) }
foreach ($moduleFolder in $moduleFolders) {
$moduleVersion = [version] $($regexToExtract.Match($moduleFolder.Name).Groups[0].Value)
if($moduleVersion -gt $maxVersion) {
$modulePath = [System.IO.Path]::Combine($moduleFolder.FullName,"Az\$moduleVersion\Az.psm1")
if(Test-Path -LiteralPath $modulePath -PathType Leaf) {
$maxVersion = $moduleVersion
$resultFolder = $moduleFolder.FullName
} else {
Write-Verbose "A folder matching the module folder pattern was found at $($moduleFolder.FullName) but didn't contain a valid module file"
}
}
}
}
catch {
Write-Verbose "Attempting to find the Latest Module Folder failed with the error: $($_.Exception.Message)"
$resultFolder = ""
}
Write-Verbose "Latest module folder detected: $resultFolder"
return $resultFolder
}
function Get-LatestModuleLinux {
[CmdletBinding()]
param([string] $patternToMatch,
[string] $patternToExtract)
$resultFolder = ""
$regexToMatch = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList $patternToMatch
$regexToExtract = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList $patternToExtract
$maxVersion = [version] "0.0.0"
try {
$moduleFolders = Get-ChildItem -Directory -Path $("/usr/share") | Where-Object { $regexToMatch.IsMatch($_.Name) }
foreach ($moduleFolder in $moduleFolders) {
$moduleVersion = [version] $($regexToExtract.Match($moduleFolder.Name).Groups[0].Value)
if($moduleVersion -gt $maxVersion) {
$modulePath = [System.IO.Path]::Combine($moduleFolder.FullName,"Az/$moduleVersion/Az.psm1")
if(Test-Path -LiteralPath $modulePath -PathType Leaf) {
$maxVersion = $moduleVersion
$resultFolder = $moduleFolder.FullName
} else {
Write-Verbose "A folder matching the module folder pattern was found at $($moduleFolder.FullName) but didn't contain a valid module file"
}
}
}
}
catch {
Write-Verbose "Attempting to find the Latest Module Folder failed with the error: $($_.Exception.Message)"
$resultFolder = ""
}
Write-Verbose "Latest module folder detected: $resultFolder"
return $resultFolder
}
function CleanUp-PSModulePathForHostedAgent {
# Clean up PSModulePath for hosted agent
$azureRMModulePath = "C:\Modules\azurerm_2.1.0"
$azureModulePath = "C:\Modules\azure_2.1.0"
$azPSModulePath = $env:PSModulePath
if ($azPSModulePath.split(";") -contains $azureRMModulePath) {
$azPSModulePath = (($azPSModulePath).Split(";") | ? { $_ -ne $azureRMModulePath }) -join ";"
write-verbose "$azureRMModulePath removed. Restart the prompt for the changes to take effect."
}
else {
write-verbose "$azureRMModulePath is not present in $azPSModulePath"
}
if ($azPSModulePath.split(";") -contains $azureModulePath) {
$azPSModulePath = (($azPSModulePath).Split(";") | ? { $_ -ne $azureModulePath }) -join ";"
write-verbose "$azureModulePath removed. Restart the prompt for the changes to take effect."
}
else {
write-verbose "$azureModulePath is not present in $azPSModulePath"
}
$env:PSModulePath = $azPSModulePath
}