Skip to content

Commit 55959a0

Browse files
authored
Users/chshrikh/azure ps module load fix m130 (#6581)
* updated the logic to load the module as per auth scheme
1 parent ffafabd commit 55959a0

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

Tasks/AzurePowerShell/AzurePowerShell.ps1

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ $scriptArguments = Get-VstsInput -Name ScriptArguments
99
$targetAzurePs = Get-VstsInput -Name TargetAzurePs
1010
$customTargetAzurePs = Get-VstsInput -Name CustomTargetAzurePs
1111

12+
1213
# Validate the script path and args do not contains new-lines. Otherwise, it will
1314
# break invoking the script via Invoke-Expression.
1415
if ($scriptType -eq "FilePath") {
@@ -45,7 +46,32 @@ if ($targetAzurePs -eq $latestVersion) {
4546
. "$PSScriptRoot\Utility.ps1"
4647
$targetAzurePs = Get-RollForwardVersion -azurePowerShellVersion $targetAzurePs
4748

48-
Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs
49+
$authScheme = ''
50+
try
51+
{
52+
$serviceNameInput = Get-VstsInput -Name ConnectedServiceNameSelector -Default 'ConnectedServiceName'
53+
$serviceName = Get-VstsInput -Name $serviceNameInput -Default (Get-VstsInput -Name DeploymentEnvironmentName)
54+
if (!$serviceName)
55+
{
56+
Get-VstsInput -Name $serviceNameInput -Require
57+
}
58+
59+
$endpoint = Get-VstsEndpoint -Name $serviceName -Require
60+
61+
if($endpoint)
62+
{
63+
$authScheme = $endpoint.Auth.Scheme
64+
}
65+
66+
Write-Verbose "AuthScheme $authScheme"
67+
}
68+
catch
69+
{
70+
$error = $_.Exception.Message
71+
Write-Verbose "Unable to get the authScheme $error"
72+
}
73+
74+
Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs -authScheme $authScheme
4975

5076
try {
5177
# Initialize Azure.

Tasks/AzurePowerShell/Tests/PerformsBasicFlow.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Register-Mock Get-VstsInput { $targetAzurePs } -- -Name TargetAzurePs
1010
Register-Mock Get-VstsInput { 'arg1 arg2' } -- -Name ScriptArguments
1111
Register-Mock Update-PSModulePathForHostedAgent
1212
Register-Mock Initialize-Azure
13+
Register-Mock Get-VstsEndpoint { @{auth = @{ scheme = "ServicePrincipal" }} }
1314

1415
# Act.
1516
$actual = & $PSScriptRoot\..\AzurePowerShell.ps1

Tasks/AzurePowerShell/Utility.ps1

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function Get-SavedModulePath {
1717

1818
function Update-PSModulePathForHostedAgent {
1919
[CmdletBinding()]
20-
param([string] $targetAzurePs)
20+
param([string] $targetAzurePs,
21+
[string] $authScheme)
2122
Trace-VstsEnteringInvocation $MyInvocation
2223
try {
2324
if ($targetAzurePs) {
@@ -28,10 +29,22 @@ function Update-PSModulePathForHostedAgent {
2829
$hostedAgentAzureRmModulePath = Get-LatestModule -patternToMatch "^azurerm_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$" -Classic:$false
2930
$hostedAgentAzureModulePath = Get-LatestModule -patternToMatch "^azure_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$" -Classic:$true
3031
}
31-
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
32-
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
33-
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
34-
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
32+
33+
if($authScheme -eq 'ServicePrincipal' -or $authScheme -eq '')
34+
{
35+
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
36+
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
37+
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
38+
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
39+
}
40+
else
41+
{
42+
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
43+
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
44+
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
45+
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
46+
}
47+
3548
} finally {
3649
Write-Verbose "The updated value of the PSModulePath is: $($env:PSModulePath)"
3750
Trace-VstsLeavingInvocation $MyInvocation

Tasks/AzurePowerShell/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"version": {
1818
"Major": 2,
1919
"Minor": 0,
20-
"Patch": 5
20+
"Patch": 6
2121
},
2222
"demands": [
2323
"azureps"

Tasks/AzurePowerShell/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"version": {
1818
"Major": 2,
1919
"Minor": 0,
20-
"Patch": 5
20+
"Patch": 6
2121
},
2222
"demands": [
2323
"azureps"

0 commit comments

Comments
 (0)