【问题标题】:Error Message when Running Azure Automation Runbook运行 Azure 自动化 Runbook 时出现错误消息
【发布时间】:2019-07-03 06:29:18
【问题描述】:

我正在尝试创建 Azure 自动化运行手册,以使用 SQL 查询中的信息将消息写入现有 Azure 存储队列。以下内容在我的 Windows 10 机器上的 Powershell ISE 中完美运行,但在 Azure the Automation 中测试时出现错误。

脚本是:

Connect-AzureRmAccount
Get-AzureRmSubscription
Set-AzureRmContext -SubscriptionId <our subscription id>

$resourceGroup = "our resource group"
$storageAccountName = "our storage account name"
$queueName = "our queue name"
$queue = Get-AzureRmStorageQueueQueue -resourceGroup $resourceGroup -storageAccountName $storageAccountName -queueName $queueName

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection

$SqlConnection.ConnectionString = "our Azure SQL connection string"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand

$SqlCmd.CommandText = $("SELECT SourceId FROM dbo.batches GROUP BY SourceId HAVING SourceId > 101")

$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter

$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet

$Table = new-object system.data.datatable

$SqlAdapter.Fill($Table) | out-null

$SqlConnection.Close()

$compArray = @($Table | select -ExpandProperty SourceId)

 foreach ($array in $compArray) {
 Add-AzureRmStorageQueueMessage -queue $queue -message @{"SourceId"=$array;"RetryCount"=0;}
 }

同样,这在我本地计算机上的 Powershell 中完美运行,但在 Azure 自动化中,我收到此错误:

Failed
Queue <our queue name> could not be retrieved/created from Storage Account <our storage account> on resource group  (Queue <our queue name> could not be retrieved/created from Storage Account <our storage account> on resource group )

Set-AzureRmContext : Run Connect-AzureRmAccount to login.
At line:3 char:1
+ Set-AzureRmContext -SubscriptionId <our subscription id> ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Set-AzureRmContext], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

Get-AzureRmStorageAccount : 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 C:\Modules\User\AzureRmStorageQueue\AzureRmStorageQueueCoreHelper.psm1:86 char:19
+ ... aContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGro ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureRmStorageAccount], ApplicationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountCommand

Get-AzureStorageQueue : Could not get the storage context.  Please pass in a storage context or set the current storage 
context.
At C:\Modules\User\AzureRmStorageQueue\AzureRmStorageQueueCoreHelper.psm1:88 char:94
+ ... ue]$queue = Get-AzureStorageQueue -Name $queueName -Context $saContex ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureStorageQueue], InvalidOperationException
    + FullyQualifiedErrorId : 
InvalidOperationException,Microsoft.WindowsAzure.Commands.Storage.Queue.GetAzureStorageQueueCommand

New-AzureStorageQueue : Could not get the storage context.  Please pass in a storage context or set the current storage 
context.
At C:\Modules\User\AzureRmStorageQueue\AzureRmStorageQueueCoreHelper.psm1:92 char:95
+ ... ue]$queue = New-AzureStorageQueue -Name $queueName -Context $saContex ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzureStorageQueue], InvalidOperationException
    + FullyQualifiedErrorId : 
InvalidOperationException,Microsoft.WindowsAzure.Commands.Storage.Queue.NewAzureStorageQueueCommand

有人可以帮助我解决我在这里缺少的东西吗? (我已经确认 Azure 自动化中安装了 AzureRmStorageQueue 模块。)

【问题讨论】:

    标签: powershell azure-storage azure-automation


    【解决方案1】:

    在 azure Runbook 中,无需使用交互式登录,如果您创建自动化帐户,它将创建一个服务主体并自动将其作为贡献者角色添加到您的订阅中。所以你只需要使用服务主体来做你需要的事情。

    命令应该如下,你可以试试。

    $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
        }
    }
    
    $resourceGroup = "our resource group"
    $storageAccountName = "our storage account name"
    $StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroup -AccountName $storageAccountName).Value[1]
    $context=New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
    $queueName = "our queue name"
    $queue = Get-AzureRmStorageQueueQueue -resourceGroup $resourceGroup -storageAccountName $storageAccountName -queueName $queueName -Context $context
    
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    
    $SqlConnection.ConnectionString = "our Azure SQL connection string"
    
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    
    $SqlCmd.CommandText = $("SELECT SourceId FROM dbo.batches GROUP BY SourceId HAVING SourceId > 101")
    
    $SqlCmd.Connection = $SqlConnection
    
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    
    $SqlAdapter.SelectCommand = $SqlCmd
    
    $DataSet = New-Object System.Data.DataSet
    
    $Table = new-object system.data.datatable
    
    $SqlAdapter.Fill($Table) | out-null
    
    $SqlConnection.Close()
    
    $compArray = @($Table | select -ExpandProperty SourceId)
    
     foreach ($array in $compArray) {
     Add-AzureRmStorageQueueMessage -queue $queue -message @{"SourceId"=$array;"RetryCount"=0;}
     }
    

    【讨论】:

    • 完美运行,乔伊。谢谢!
    猜你喜欢
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2020-07-24
    相关资源
    最近更新 更多