Skip to content

Commit 5838e56

Browse files
User Story 2299663: Regression on AzurePowershellV5 (#21148)
1 parent 1bc2672 commit 5838e56

File tree

5 files changed

+123
-16
lines changed

5 files changed

+123
-16
lines changed

Tasks/AzurePowerShellV5/AzurePowerShell.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ if ($targetAzurePs -eq $otherVersion) {
4141
throw (Get-VstsLocString -Key InvalidAzurePsVersion $customTargetAzurePs)
4242
} else {
4343
$targetAzurePs = $customTargetAzurePs.Trim()
44-
Initialize-ModuleVersionValidation -moduleName "azure-powershell" -targetAzurePs $targetAzurePs -displayModuleName "Az" -versionsToReduce $versionTolerance
4544
}
4645
}
4746

@@ -50,15 +49,15 @@ $regex = New-Object -TypeName System.Text.RegularExpressions.Regex -ArgumentList
5049

5150
if ($targetAzurePs -eq $latestVersion) {
5251
$targetAzurePs = ""
53-
$installedVersion = Get-InstalledMajorRelease "Az"
54-
Initialize-ModuleVersionValidation -moduleName "azure-powershell" -targetAzurePs $installedVersion -displayModuleName "Az" -versionsToReduce $versionTolerance
5552

5653
} elseif (-not($regex.IsMatch($targetAzurePs))) {
5754
throw (Get-VstsLocString -Key InvalidAzurePsVersion -ArgumentList $targetAzurePs)
5855
}
5956

6057
. $PSScriptRoot\TryMakingModuleAvailable.ps1 -targetVersion "$targetAzurePs" -platform Windows
6158

59+
Initialize-ModuleVersionValidation -moduleName "azure-powershell" -targetAzurePs $targetAzurePs -displayModuleName "Az" -versionsToReduce $versionTolerance
60+
6261
if ($validateScriptSignature) {
6362
try {
6463
if ($scriptType -ne "InlineScript") {

Tasks/AzurePowerShellV5/Utility.ps1

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,89 @@ function Get-MajorVersionOnAzurePackage {
223223
return $lastOneRelease
224224
}
225225

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 {
227256
[CmdletBinding()]
228257
param(
229258
[Parameter(Mandatory=$true)]
230-
[string]$moduleName
259+
[string]$moduleName,
260+
[Parameter(Mandatory=$true)]
261+
[bool]$isWin
231262
)
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+
}
235309
}
236310

237311
function Get-IsSpecifiedPwshAzVersionOlder{
@@ -259,6 +333,7 @@ function Initialize-ModuleVersionValidation {
259333
[Parameter(Mandatory=$true)]
260334
[string]$moduleName,
261335
[Parameter(Mandatory=$true)]
336+
[AllowEmptyString()]
262337
[string]$targetAzurePs,
263338
[Parameter(Mandatory=$true)]
264339
[string]$displayModuleName,
@@ -268,8 +343,10 @@ function Initialize-ModuleVersionValidation {
268343
try {
269344
$DisplayWarningForOlderAzVersion = Get-VstsPipelineFeature -FeatureName "ShowWarningOnOlderAzureModules"
270345
if ($DisplayWarningForOlderAzVersion -eq $true) {
346+
if ($targetAzurePs -eq "") {
347+
$targetAzurePs = Get-InstalledMajorRelease -moduleName $displayModuleName -isWin $true
348+
}
271349
$latestRelease = Get-MajorVersionOnAzurePackage -moduleName $moduleName
272-
273350
if (Get-IsSpecifiedPwshAzVersionOlder -specifiedVersion $targetAzurePs -latestRelease $($latestRelease.tag_name) -versionsToReduce $versionsToReduce) {
274351
Write-Warning (Get-VstsLocString -Key Az_LowerVersionWarning -ArgumentList $displayModuleName, $targetAzurePs, $($latestRelease.tag_name))
275352
}

Tasks/AzurePowerShellV5/azurepowershell.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ async function run() {
6666
else {
6767
targetAzurePs = ""
6868
if (tl.getPipelineFeature('ShowWarningOnOlderAzureModules')) {
69-
const versionCommand = tl.getPipelineFeature('UseAzVersion') ? "version" : "--version"
70-
const azVersionResult = tl.execSync("az", versionCommand);
71-
await validateAzModuleVersion(fetchingModule, azVersionResult.stdout, moduleDisplayName, majorversionTolerance, true)
69+
const azVersionResult = await getInstalledAzModuleVersion();
70+
if (azVersionResult) {
71+
await validateAzModuleVersion(fetchingModule, azVersionResult, moduleDisplayName, majorversionTolerance, true)
72+
}
7273
}
7374
}
7475

@@ -203,4 +204,34 @@ async function run() {
203204
}
204205
}
205206

207+
async function getInstalledAzModuleVersion(): Promise<string | null> {
208+
try {
209+
tl.debug('Checking installed Az PowerShell module version...');
210+
211+
// PowerShell command to get the installed Az module version
212+
const powershell = tl.tool(tl.which('pwsh') || tl.which('powershell') || tl.which('pwsh', true))
213+
.arg('-NoLogo')
214+
.arg('-NoProfile')
215+
.arg('-NonInteractive')
216+
.arg('-ExecutionPolicy')
217+
.arg('Unrestricted')
218+
.arg('-Command')
219+
.arg(`. '${path.join(path.resolve(__dirname),'Utility.ps1')}'; Get-InstalledMajorRelease -moduleName 'Az' -iswin $false`);
220+
221+
const result = await powershell.execSync()
222+
if (result.code === 0 && result.stdout) {
223+
const version = result.stdout.trim();
224+
if (version && version !== '' && version !== '0.0.0') {
225+
tl.debug(`Found installed Az module version: ${version}`);
226+
return version;
227+
}
228+
}
229+
tl.debug('Az PowerShell module not found on the agent');
230+
return null;
231+
} catch (error) {
232+
tl.debug(`Error checking installed Az module version: ${error.message}`);
233+
return null;
234+
}
235+
}
236+
206237
run();

Tasks/AzurePowerShellV5/task.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 259,
21-
"Patch": 4
20+
"Minor": 260,
21+
"Patch": 0
2222
},
2323
"releaseNotes": "Added support for Az Module and cross platform agents.",
2424
"groups": [

Tasks/AzurePowerShellV5/task.loc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"author": "Microsoft Corporation",
1818
"version": {
1919
"Major": 5,
20-
"Minor": 259,
21-
"Patch": 4
20+
"Minor": 260,
21+
"Patch": 0
2222
},
2323
"releaseNotes": "ms-resource:loc.releaseNotes",
2424
"groups": [

0 commit comments

Comments
 (0)