0

I need to setup powershell script for refreshing tabular model cube on Azure, Microsoft SQL Analysis Server (version 15.0.0.52). I wrote this solution, but every time when it's executed I have same errors.

   # PowerShell code 
# Connect to a connection to get TenantId and SubscriptionId
$Connection = Get-AutomationConnection -Name "AzureRunAsConnection"
$TenantId = $Connection.TenantId
$SubscriptionId = $Connection.SubscriptionId

# Get the service principal credentials connected to the automation account. 
$null = $SPCredential = Get-AutomationPSCredential -Name "Samcred"

# Login to Azure ($null is to prevent output, since Out-Null doesn't work in Azure)
Write-Output "Login to Azure using automation account 'Samcred'."
$null = Login-AzureRmAccount -TenantId $TenantId -SubscriptionId $SubscriptionId -Credential $SPCredential

# Select the correct subscription
Write-Output "Selecting subscription '$($SubscriptionId)'."
$null = Select-AzureRmSubscription -SubscriptionID $SubscriptionId

# Get variable values
$DatabaseName = Get-AutomationVariable -Name 'DatabaseName'
$AnalysisServerName = Get-AutomationVariable -Name 'AnalysisServerName'

# Show info before processing (for testing/logging purpose only)
Write-Output "Processing $($DatabaseName) on $($AnalysisServerName)"

#Process database
$null = Invoke-ProcessASDatabase -databasename $DatabaseName -server $AnalysisServerName -RefreshType "Full" -Credential $SPCredential 

# Show done when finished (for testing/logging purpose only)
Write-Output "Done"

Errors are:

Login to Azure using automation account 'Samcred'.
Login-AzureRmAccount : unknown_user_type: Unknown User Type
At line:12 char:9
+ $null = Login-AzureRmAccount -TenantId $TenantId -SubscriptionId $Sub ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzureRmAccount], AadAuthenticationFailedException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand

Selecting subscription 'fb3456-56c2-40a2-aae6-9eeace345678'.
Select-AzureRmSubscription : Run Login-AzureRmAccount to login.
At line:16 char:9
+ $null = Select-AzureRmSubscription -SubscriptionID $SubscriptionId
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Set-AzureRmContext], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

Processing TabCube on asazure://westus.asazure.windows.net/dex:rw
Invoke-ProcessASDatabase : Exception has been thrown by the target of an invocation.
At line:26 char:9
+ $null = Invoke-ProcessASDatabase -databasename $DatabaseName -server  ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-ProcessASDatabase], TargetInvocationException
    + FullyQualifiedErrorId : 
System.Reflection.TargetInvocationException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessASDatabase

Done

Any advice about this ?

Darko Milic
  • 189
  • 3
  • 13
  • Is the credential you're using a Microsoft account? Apparently there are some limitations there, saw this on GitHub: https://github.com/Azure/azure-powershell/issues/3108 – trebleCode Jan 19 '18 at 20:14

1 Answers1

1
$null = $SPCredential = Get-AutomationPSCredential -Name "Samcred"

If you use Microsoft account to login Azure, you will get the error log. In Azure runbook, you could use service principal to login, not use a account. Change your script like below:

$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
    }
}
Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • Also, see this [question](https://stackoverflow.com/questions/45015557/azure-automation-credentials-delivered-by-get-psautomationcredential-dont-wor/45023853#45023853). – Shui shengbao Jan 22 '18 at 01:48
  • Yes, but now there is new issue: Invoke-ProcessASDatabase : Authentication failed: User ID and Password are required when user interface is not available. At line:50 char:9 + $null = Invoke-ProcessASDatabase -databasename $DatabaseName -server ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Invoke-ProcessASDatabase], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessASDatabase Done – Darko Milic Jan 22 '18 at 09:56