TGills
TGills

Reputation: 136

Azure Runbook - No subscription found in the context

Background:

I am trying to set up a script via Runbooks on Azure. What I find unusual is that I can run the command via the Azure Powershell Cloud Shell and it works. However, when I try to run it via the Runbook, I recieve an error(See below).

The Script:

$ResourceGroupName = "group"
$ServerName = "serverName"
$DatabaseName = "databaseName"
$StorageKeyType = "StorageAccessKey"
$StorageKey = "storageKey"
$StorageUri = "storageUri"
$AdminLogin = "admin"
$AdminPassword = (ConvertTo-SecureString "12345" -AsPlainText -Force)

New-AzureRmSqlDatabaseExport `
        -AdministratorLogin $AdminLogin `
        -AdministratorLoginPassword $AdminPassword `
        -DatabaseName $DatabaseName `
        -ResourceGroupName $ResourceGroupName `
        -ServerName $ServerName `
        -StorageKey $StorageKey `
        -StorageKeyType $StorageKeyType `
        -StorageUri $StorageUri `

**Generic values used

The Error:

New-AzureRmSqlDatabaseExport : No subscription found in the context.  Please ensure that the credentials you provided 
are authorized to access an Azure subscription, then run Connect-AzureRmAccount to login.
At line:10 char:1
+ New-AzureRmSqlDatabaseExport `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureRmSqlDatabaseExport], ApplicationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Sql.ImportExport.Cmdlet.NewAzureSqlDatabaseExport

The Question:

What am I doing wrong? I the password and username that I am using are the ones that are used everywhere else, and work when I run the script in the Cloud Shell. Also, what is meant by "No subscription found in the context"?

Upvotes: 0

Views: 16669

Answers (2)

Shui shengbao
Shui shengbao

Reputation: 19223

In Azure Cloud Shell, you have login to your account, so you don't need login again. But in runbook, you need login your account firstly.

you could use the following codes to logon.

$connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}

In above code, you need use connection AzureRunAsConnection, it is created by Azure default, you could use it directly.

More information about this, you also could check this question.

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72191

It means that you need to login to Azure before doing anything. Cloud Shell handles that for you, whereas Azure Automation doesnt.

https://learn.microsoft.com/en-us/powershell/azure/get-started-azureps?view=azurermps-5.5.0#log-in-to-azure

You can use Azure AD user login, certificate login or service principal login. Live account wont work, as it is interactive.

Upvotes: 1

Related Questions