From 3a7cc6577c12dd00c8f110100fb640357690ab1b Mon Sep 17 00:00:00 2001 From: Shivangi Date: Wed, 15 May 2019 10:22:48 +0530 Subject: [PATCH 1/2] Handling multiple subscriptions --- Tasks/AzurePowerShellV4/InitializeAz.ps1 | 36 ++++++++++++++++++++++++ Tasks/AzurePowerShellV4/task.json | 2 +- Tasks/AzurePowerShellV4/task.loc.json | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Tasks/AzurePowerShellV4/InitializeAz.ps1 b/Tasks/AzurePowerShellV4/InitializeAz.ps1 index 0c63ef4b2054..b692c10350ba 100644 --- a/Tasks/AzurePowerShellV4/InitializeAz.ps1 +++ b/Tasks/AzurePowerShellV4/InitializeAz.ps1 @@ -48,6 +48,32 @@ $null = Clear-AzContext -Scope Process Write-Host "##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue" $null = Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue +$scopeLevel = "Subscription" + +If ($endpoint.PSObject.Properties['Data']) +{ + If ($endpoint.Data.PSObject.Properties['scopeLevel']) + { + $scopeLevel = $endpoint.Data.scopeLevel + } +} + +function Format-Splat { + [CmdletBinding()] + param([Parameter(Mandatory = $true)][hashtable]$Hashtable) + + # Collect the parameters (names and values) in an array. + $parameters = foreach ($key in $Hashtable.Keys) { + $value = $Hashtable[$key] + # If the value is a bool, format the parameter as a switch (ending with ':'). + if ($value -is [bool]) { "-$($key):" } else { "-$key" } + $value + } + + $OFS = " " + "$parameters" # String join the array. +} + if ($endpointObject.scheme -eq 'ServicePrincipal') { try { if ($endpointObject.authenticationType -ieq 'SPNKey') { @@ -69,6 +95,16 @@ if ($endpointObject.scheme -eq 'ServicePrincipal') { throw (New-Object System.Exception("There was an error with the service principal used for the deployment.", $_.Exception)) } + + if($scopeLevel -eq "Subscription") + { + $SubscriptionId = $endpointObject.subscriptionId + $TenantId = $endpointObject.tenantId + $additional = @{ TenantId = $TenantId } + + Write-Host "##[command] Set-AzContext -SubscriptionId $SubscriptionId $(Format-Splat $additional)" + $null = Set-AzContext -SubscriptionId $SubscriptionId @additional + } } else { # Provide an additional, custom, credentials-related error message. Will handle localization later diff --git a/Tasks/AzurePowerShellV4/task.json b/Tasks/AzurePowerShellV4/task.json index aa668635084c..1ceb6109ef21 100644 --- a/Tasks/AzurePowerShellV4/task.json +++ b/Tasks/AzurePowerShellV4/task.json @@ -17,7 +17,7 @@ "version": { "Major": 4, "Minor": 0, - "Patch": 9 + "Patch": 10 }, "preview": true, "releaseNotes": "Added support for Az Module and cross platform agents.", diff --git a/Tasks/AzurePowerShellV4/task.loc.json b/Tasks/AzurePowerShellV4/task.loc.json index 7a6041ccb1b0..797b9f20c086 100644 --- a/Tasks/AzurePowerShellV4/task.loc.json +++ b/Tasks/AzurePowerShellV4/task.loc.json @@ -17,7 +17,7 @@ "version": { "Major": 4, "Minor": 0, - "Patch": 9 + "Patch": 10 }, "preview": true, "releaseNotes": "ms-resource:loc.releaseNotes", From 0c9b2f3be64e830e667de0434d3bcc8f64083636 Mon Sep 17 00:00:00 2001 From: Shivangi Date: Thu, 16 May 2019 14:28:21 +0530 Subject: [PATCH 2/2] resolving comment --- Tasks/AzurePowerShellV4/InitializeAz.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tasks/AzurePowerShellV4/InitializeAz.ps1 b/Tasks/AzurePowerShellV4/InitializeAz.ps1 index b692c10350ba..88ca9df19165 100644 --- a/Tasks/AzurePowerShellV4/InitializeAz.ps1 +++ b/Tasks/AzurePowerShellV4/InitializeAz.ps1 @@ -69,8 +69,7 @@ function Format-Splat { if ($value -is [bool]) { "-$($key):" } else { "-$key" } $value } - - $OFS = " " + "$parameters" # String join the array. }