运行 Azure 自动化 Runbook 时出现错误消息

Error Message when Running Azure Automation Runbook

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

脚本是:

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

有人可以帮助我解决我在这里缺少的问题吗? (我已确认 AzureRmStorageQueue 模块已安装在 Azure Automation 中。)

在 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;}
 }