Powershell Az.Accounts and Az.Sql in Azure Runbook Not Working

Viewed 83

I have a Runbook in my Automation Account I'm working on which is supposed to be swapping dns aliases between two Azure Sql Servers. It then removes an Azure Sql DB and performs a copy (this part is not in the code below). This works fine when I run it in Windows Powershell. When run from the Runbook, however, none of the commands are returning anything. I'm executing the Runbook from a Data Factory Webhook.

Here's the script:

param (
    [Parameter (Mandatory = $false)]
    [object] $WebHookData
)
# Get all parameters from body 
# (passed from Data Factory Web Activity)
$Parameters = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
# Save Parameters for readability
# Check for ADF parameter. Not used when testing interactively
If ($Parameters.callBackUri)
{
    $callBackUri = $Parameters.callBackUri
}
# Demo parameter
$ParmMsg =  $Parameters.ParmMsg

$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Write-Output 'Logging into Azure...';
$connectionResult =  Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID `
        -ApplicationId $servicePrincipalConnection.ApplicationID   `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint `
        -ServicePrincipal

Select-AzSubscription -Subscription "xxxpience"

# Main Script
$subscriptionId = 'xxxxxxxx'
$sqlServerName1 = 'sqlsvr-xxxxx-002'
$sqlServerName2 = 'sqlsvr-xxxxx-003'
$sqlServerDnsAliasName = 'xx-xxxx-online'
$databaseName = 'sqldb-adventureworks'
$resourceGroupName = 'rg-sandbox'

Write-Output 'Get the aliases assigned to server 1...';
$alias = Get-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName -ServerName $sqlServerName1
Write-Output 'Server 1 alias: ' $alias.DnsAliasName

if ( $alias.DnsAliasName -eq $sqlServerDnsAliasName )
{
Write-Output 'Move the alias from sqlsvr-xxxxx-002 to sqlsvr-xxxxx-003...';
Set-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName -TargetServerName $sqlServerName2 `
-Name $sqlServerDnsAliasName `
-SourceServerResourceGroup $resourceGroupName -SourceServerName $sqlServerName1 `
-SourceServerSubscriptionId $subscriptionId
$onlineSqlServer = $sqlServerName2
$offlineSqlServer = $sqlServerName1
}
else
{
Write-Output 'Move the alias from sqlsvr-xxxxx-003 to sqlsvr-xxxxx-002...'; 
Set-AzSqlServerDnsAlias -ResourceGroupName $resourceGroupName -TargetServerName $sqlServerName1 `
-Name $sqlServerDnsAliasName `
-SourceServerResourceGroup $resourceGroupName -SourceServerName $sqlServerName2 `
-SourceServerSubscriptionId $subscriptionId
$onlineSqlServer = $sqlServerName1
$offlineSqlServer = $sqlServerName2
}

Write-Output $onlineSqlServer 'is online';
 
# If called from a webhook, use callBackUri to notify task is complete
If ($callBackUri)
{
    Invoke-WebRequest -Uri $callBackUri -Method Post
}

Here's the output:

Output

I do get this error as well: "The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again."

1 Answers

I resolved the issue. The very last statement Invoke-WebRequest needed the -UseBasicParsing switch.

Related