2
votes

I am using an Azure Powershell task in an Azure (VSTS) Pipeline to try and upload a file to an Azure Classic container.
As part of setting up the Azure Powershell task (ver 3.1.10), I added the Azure Classic subscription that this container lives in to the Project Settings\Service connections: service connection

I then select that subscription in the pipeline task: select subscription

When I execute the script, the logs seem to show that the task is both setting and selecting the expected subscription:
logs

However, if I don't explicitly (re-)create and pass in the AzureStorageContext to the Set-AzureStorageBlobContent function, the task fails with:

[error]Could not get the storage context. Please pass in a storage context or set the current storage context.

Is this expected behavior?
Is there any way around having to re-create and pass in the context when it appears that it already exists?

For example, is there an environment variable that might contain the context that was automatically created/selected that I can just pass in?

Update: I suspect that if the Select-AzureSubscription call seen in the logs used the -Current switch, this would work as I'm expecting it to. However, since that command is automatically ran with no way to configure it via the pipeline task, it's not verifiable.
Perhaps this needs to be a feature request?

Excerpts from script:

#Not passing in the context results in the error
Set-AzureStorageBlobContent -File "$file" -Blob "$blobpath/$blah" -Container $blobname -Properties $properties -Force

#Passing in the context works:  
$azureKey = Get-AzureStorageKey "$accountName"
$storagekey = [string]$azureKey.Primary
$context = New-AzureStorageContext "$accountName" -StorageAccountKey $storagekey  
Set-AzureStorageBlobContent -File "$file" -Blob "$blobpath/$blah" -Container $blobname -Properties $properties -Context $context -Force
1

1 Answers

2
votes

While I probably should just delete this question (upon discovering the "answer"), I suppose I will provide what I found after more debugging, etc.

TLDR; -- This is mostly me not grepping the concept that an Azure Subscription (context) does not correlate to an Azure Storage (context).

Is this expected behavior?

Yes. Simply having a currently set subscription does not mean there's a currently set storage context.

Come to find out, our company has multiple storage accounts in the subscription I was using.
It could be that if a subscription only has one storage account, the function would succeed without specifying a context? Maybe I will research that later.

Is there any way around having to re-create and pass in the context when it appears that it already exists?

No (perhaps because of the multiple storage accounts in the subscription).
I will have to specify/select the current storage context from the current subscription (as I did in the "Passing in the context works" part in my question).

Here's how I arrived at this:
First, I verified what actually was being set (if anything) as the current [subscription] context and then explicitly (re-)setting it.
Running the command still failed.
So, it wasn't that the subscription wasn't being set (since it was).

$current = (Get-AzureSubscription -Current).SubscriptionName
Write-Host "current subscription is $current"

$setCurrent = $false
Write-Host "setCurrent is $setCurrent"
$setCurrent = Select-AzureSubscription -Current -SubscriptionName "CDN Subscription" -PassThru
if ($setCurrent)
{
  Write-Host "current set"
  $current = (Get-AzureSubscription -Current).SubscriptionName
  Write-Host "current subscription is $current"
}
else
{
  Write-Host "current not set"
}  

output_1

It then dawned on me that maybe 'subscription' did not equal 'storage'. To verify that, I then ran the following:

$current = (Get-AzureSubscription -Current).SubscriptionName
Write-Host "current subscription is $current"

$table = Get-AzureStorageAccount | Format-Table -AutoSize -Property @{Label="Name";Expression={$_.StorageAccountName}},"Label","Location" | Out-String
Write-Host "$table"

The result - 4 storage accounts in the subscription. Ergo, I will need to specify the account I want to upload to

table