Skip to content

Commit

Permalink
Users/chshrikh/azure ps module load fix m130 (#6581)
Browse files Browse the repository at this point in the history
* updated the logic to load the module as per auth scheme
  • Loading branch information
chshrikh authored Mar 5, 2018
1 parent ffafabd commit 55959a0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
28 changes: 27 additions & 1 deletion Tasks/AzurePowerShell/AzurePowerShell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $scriptArguments = Get-VstsInput -Name ScriptArguments
$targetAzurePs = Get-VstsInput -Name TargetAzurePs
$customTargetAzurePs = Get-VstsInput -Name CustomTargetAzurePs


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

Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs
$authScheme = ''
try
{
$serviceNameInput = Get-VstsInput -Name ConnectedServiceNameSelector -Default 'ConnectedServiceName'
$serviceName = Get-VstsInput -Name $serviceNameInput -Default (Get-VstsInput -Name DeploymentEnvironmentName)
if (!$serviceName)
{
Get-VstsInput -Name $serviceNameInput -Require
}

$endpoint = Get-VstsEndpoint -Name $serviceName -Require

if($endpoint)
{
$authScheme = $endpoint.Auth.Scheme
}

Write-Verbose "AuthScheme $authScheme"
}
catch
{
$error = $_.Exception.Message
Write-Verbose "Unable to get the authScheme $error"
}

Update-PSModulePathForHostedAgent -targetAzurePs $targetAzurePs -authScheme $authScheme

try {
# Initialize Azure.
Expand Down
1 change: 1 addition & 0 deletions Tasks/AzurePowerShell/Tests/PerformsBasicFlow.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Register-Mock Get-VstsInput { $targetAzurePs } -- -Name TargetAzurePs
Register-Mock Get-VstsInput { 'arg1 arg2' } -- -Name ScriptArguments
Register-Mock Update-PSModulePathForHostedAgent
Register-Mock Initialize-Azure
Register-Mock Get-VstsEndpoint { @{auth = @{ scheme = "ServicePrincipal" }} }

# Act.
$actual = & $PSScriptRoot\..\AzurePowerShell.ps1
Expand Down
23 changes: 18 additions & 5 deletions Tasks/AzurePowerShell/Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function Get-SavedModulePath {

function Update-PSModulePathForHostedAgent {
[CmdletBinding()]
param([string] $targetAzurePs)
param([string] $targetAzurePs,
[string] $authScheme)
Trace-VstsEnteringInvocation $MyInvocation
try {
if ($targetAzurePs) {
Expand All @@ -28,10 +29,22 @@ function Update-PSModulePathForHostedAgent {
$hostedAgentAzureRmModulePath = Get-LatestModule -patternToMatch "^azurerm_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$" -Classic:$false
$hostedAgentAzureModulePath = Get-LatestModule -patternToMatch "^azure_[0-9]+\.[0-9]+\.[0-9]+$" -patternToExtract "[0-9]+\.[0-9]+\.[0-9]+$" -Classic:$true
}
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')

if($authScheme -eq 'ServicePrincipal' -or $authScheme -eq '')
{
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
}
else
{
$env:PSModulePath = $hostedAgentAzureRmModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
$env:PSModulePath = $hostedAgentAzureModulePath + ";" + $env:PSModulePath
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
}

} finally {
Write-Verbose "The updated value of the PSModulePath is: $($env:PSModulePath)"
Trace-VstsLeavingInvocation $MyInvocation
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 5
"Patch": 6
},
"demands": [
"azureps"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShell/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 2,
"Minor": 0,
"Patch": 5
"Patch": 6
},
"demands": [
"azureps"
Expand Down

0 comments on commit 55959a0

Please sign in to comment.