I'm trying to create a script that will download SQL backups from Azure before using docker to build containers. I created a service principal using these instructions: https://docs.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azps-2.4.0
I'm using the following code to try to start the downloads but it fails with the following error:
$tenantID = '6ed674z5-my tenant ID-802730b05737'
$passwd = ConvertTo-SecureString 'x43my long passwordR69' -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential('079054cd-my application ID-0b19d8ar6e77', $passwd)
Connect-AzAccount -ServicePrincipal -Credential $pscredential -TenantId $tenantId
$containerName = "mycontainer"
$location = "westus2"
$resourceGroup = "myresourcegroup"
$storageAccount = Get-AzStorageAccount
$ctx = $storageAccount.Context
Get-AzStorageBlob -Container $ContainerName -Context $ctx | Get-AzStorageblobcontent -Destination ".\dbase\backups" -Force
Error:
Get-AzStorageAccount : 'this.Client.SubscriptionId' cannot be null.
At C:\dev\thcguard\launch.ps1:9 char:19
+ $storageAccount = Get-AzStorageAccount
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzStorageAccount], ValidationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountCommand
Where am I going wrong? If I just use Connect-AzAccount and manually log in the download code works.
Connect-AzAccount
line, Add this lineSet-AzContext -SubscriptionId "my azure subscription id" -TenantId "my tenant id"
– Thomas