@@ -223,15 +223,89 @@ function Get-MajorVersionOnAzurePackage {
223
223
return $lastOneRelease
224
224
}
225
225
226
- function Get-InstalledMajorRelease {
226
+ function Get-LatestModuleFromCommonPath {
227
+ [CmdletBinding ()]
228
+ param ([string ] $patternToMatch ,
229
+ [string ] $patternToExtract )
230
+
231
+ $commonPaths = $env: PSModulePath -split ' ;'
232
+ $maxVersion = [version ] " 0.0.0"
233
+ $regexToMatch = New-Object - TypeName System.Text.RegularExpressions.Regex - ArgumentList $patternToMatch
234
+ $regexToExtract = New-Object - TypeName System.Text.RegularExpressions.Regex - ArgumentList $patternToExtract
235
+
236
+ foreach ($path in $commonPaths ) {
237
+ $azModulePath = Join-Path $path $moduleName
238
+ if (Test-Path $azModulePath ) {
239
+ Write-Verbose " Found Az module directory at: $azModulePath "
240
+ $versions = Get-ChildItem - Directory - Path $modulePath | Where-Object { $regexToMatch.IsMatch ($_.Name ) }
241
+ foreach ($versionDir in $versions ) {
242
+ $manifestPath = Join-Path $versionDir.FullName " $moduleName .psd1"
243
+ $moduleVersion = [version ] $ ($regexToExtract.Match ($moduleFolder.Name ).Groups[0 ].Value)
244
+ if ($moduleVersion -gt $maxVersion ) {
245
+ if (Test-Path $manifestPath ) {
246
+ $maxVersion = $moduleVersion
247
+ }
248
+ }
249
+ }
250
+ }
251
+ }
252
+ return $maxVersion
253
+ }
254
+
255
+ function Get-InstalledMajorRelease {
227
256
[CmdletBinding ()]
228
257
param (
229
258
[Parameter (Mandatory = $true )]
230
- [string ]$moduleName
259
+ [string ]$moduleName ,
260
+ [Parameter (Mandatory = $true )]
261
+ [bool ]$isWin
231
262
)
232
- Write-Host " ##[command]Get-InstalledModule -Name ${moduleName} "
233
- $installedModuleMajorVersion = (Get-InstalledModule - Name $moduleName ).Version
234
- return $installedModuleMajorVersion
263
+ $version = ' '
264
+ $versionPattern = " [0-9]+\.[0-9]+\.[0-9]+"
265
+ Write-Verbose " Attempting to find installed version of module: $moduleName "
266
+ $isHostedAgent = Test-IsHostedAgentPathPresent - isWin $isWin
267
+ if ($isHostedAgent ) {
268
+ if ($isWin ) {
269
+ $latestAzPath = Get-LatestModule - patternToMatch " ^az_[0-9]+\.[0-9]+\.[0-9]+$" - patternToExtract " [0-9]+\.[0-9]+\.[0-9]+$"
270
+ } else {
271
+ $latestAzPath = Get-LatestModuleLinux - patternToMatch " ^az_[0-9]+\.[0-9]+\.[0-9]+$" - patternToExtract " [0-9]+\.[0-9]+\.[0-9]+$"
272
+ }
273
+ if ($latestAzPath -and $latestAzPath -match $versionPattern -and $latestAzPath -ne ' 0.0.0' ) {
274
+ $version = $Matches [0 ]
275
+ Write-Debug " Found Az module version from hosted agent module path: $version "
276
+ return $version
277
+ }
278
+ }
279
+ try {
280
+ $installedModule = Get-InstalledModule - Name $moduleName - ErrorAction SilentlyContinue | Sort-Object Version - Descending | Select-Object - First 1
281
+ if ($installedModule ) {
282
+ $version = $installedModule.Version.ToString ()
283
+ Write-Debug " Found Az module version from Get-InstalledModule: $version "
284
+ return $version
285
+ }
286
+ } catch {
287
+ Write-Verbose " Get-InstalledModule failed: $ ( $_.Exception.Message ) "
288
+ }
289
+ try {
290
+ # First try to get the Az module directly
291
+ $azModule = Get-Module - Name $moduleName - ListAvailable - ErrorAction SilentlyContinue | Sort-Object Version - Descending | Select-Object - First 1
292
+ if ($azModule ) {
293
+ $version = $azModule.Version.ToString ()
294
+ Write-Host " Found Az module version directly in Get-Module: $version "
295
+ return $version
296
+ }
297
+ if (! $isHostedAgent ) {
298
+ $version = Get-LatestModuleFromCommonPath - patternToMatch " ^az_[0-9]+\.[0-9]+\.[0-9]+$" - patternToExtract " [0-9]+\.[0-9]+\.[0-9]+$"
299
+ if ($version -and $version -ne ' 0.0.0' ) {
300
+ # $version = $azModule.ToString()
301
+ Write-Debug " Found Az module version For self hosted Agent: $version "
302
+ return $version
303
+ }
304
+ throw (" Could not find the module version of $moduleName . Please ensure the Az module is installed on the agent." )
305
+ }
306
+ } catch {
307
+ Write-Verbose " Az module specific detection failed: $ ( $_.Exception.Message ) "
308
+ }
235
309
}
236
310
237
311
function Get-IsSpecifiedPwshAzVersionOlder {
@@ -259,6 +333,7 @@ function Initialize-ModuleVersionValidation {
259
333
[Parameter (Mandatory = $true )]
260
334
[string ]$moduleName ,
261
335
[Parameter (Mandatory = $true )]
336
+ [AllowEmptyString ()]
262
337
[string ]$targetAzurePs ,
263
338
[Parameter (Mandatory = $true )]
264
339
[string ]$displayModuleName ,
@@ -268,8 +343,10 @@ function Initialize-ModuleVersionValidation {
268
343
try {
269
344
$DisplayWarningForOlderAzVersion = Get-VstsPipelineFeature - FeatureName " ShowWarningOnOlderAzureModules"
270
345
if ($DisplayWarningForOlderAzVersion -eq $true ) {
346
+ if ($targetAzurePs -eq " " ) {
347
+ $targetAzurePs = Get-InstalledMajorRelease - moduleName $displayModuleName - isWin $true
348
+ }
271
349
$latestRelease = Get-MajorVersionOnAzurePackage - moduleName $moduleName
272
-
273
350
if (Get-IsSpecifiedPwshAzVersionOlder - specifiedVersion $targetAzurePs - latestRelease $ ($latestRelease.tag_name ) - versionsToReduce $versionsToReduce ) {
274
351
Write-Warning (Get-VstsLocString - Key Az_LowerVersionWarning - ArgumentList $displayModuleName , $targetAzurePs , $ ($latestRelease.tag_name ))
275
352
}
0 commit comments