Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling multiple subscriptions #10371

Merged
merged 3 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Tasks/AzurePowerShellV4/InitializeAz.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
20shivangi marked this conversation as resolved.
Show resolved Hide resolved
"$parameters" # String join the array.
}

if ($endpointObject.scheme -eq 'ServicePrincipal') {
try {
if ($endpointObject.authenticationType -ieq 'SPNKey') {
Expand All @@ -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 }
20shivangi marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV4/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
2 changes: 1 addition & 1 deletion Tasks/AzurePowerShellV4/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"version": {
"Major": 4,
"Minor": 0,
"Patch": 9
"Patch": 10
},
"preview": true,
"releaseNotes": "ms-resource:loc.releaseNotes",
Expand Down