PowerShell script to process cube on Azure - connection issue - powershell

I am trying to process cube on Azure with Powershell script. Have this kind of code:
# 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"
$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
}
}
# 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"
After execution there is connection error:
Invoke-ProcessASDatabase : Authentication failed: User ID and Password are required when user interface is not
available.
At line:46 char:9
+ $null = Invoke-ProcessASDatabase -databasename $DatabaseName -server ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-ProcessASDatabase], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessASDatabase
Using Microsoft SQL Analysis Server (version 15.0.0.52).
Somehow there is need again to put account and password, which I already done.

Related

Trying to run a word count hadoop on visual studio code and have saved it as a ps1 extension but function not running on powershell

VS CODE
Write-Host "Getting the context of your profile on Microsoft Azure..." -ForegroundColor Green
$context = Get-AzContext
if ($null -eq $context) { Connect-AzAccount }
$context
$subscription = "5054f72b-16ed-4894-8bf5-fe7ad8fa20e3"
$resourceGroupName = "BUS5WB"
$clusterName = "bus5wb-hydra-s1-2022"
Write-Host "Selecting the subscription the HDInsight cluster is on..." -ForegroundColor Green
Select-AzSubscription $subscription
Write-Host "Logging into the HDInsight cluster..." -ForegroundColor Green
$cred = Get-Credential -Message "Enter the HDInsight cluster HTTP user credential:"
$wordCountJobDefinition = New-AzHDInsightMapReduceJobDefinition -JarFile "/example/jars/hadoop-mapreduce-examples.jar" -ClassName "wordcount" -Arguments "/example/data/20450461/2Star.txt", ` "/example/data/20450461/2Star"
#Submit the job to the cluster
Write-Host "Start the MapReduce job..." -ForegroundColor Green
$wordCountJob = Start-AzHDInsightJob -ClusterName $clusterName -JobDefinition $wordCountJobDefinition -HttpCredential $cred -ResourceGroupName $resourceGroupName
#Wait for the job to complete
Write-Host "Wait for the job to complete..." -ForegroundColor Green
Wait-AzHDInsightJob -ClusterName $clusterName -JobId $wordCountJob.JobId ` -HttpCredential $cred
THIS IS THE ERROR ON POWERSHELL
POWERSHELL ERROR
Start the MapReduce job...
Start-AzHDInsightJob: C:\Users\20450461\Documents\2Star.ps1:48:17
Line |
48 | $wordCountJob = Start-AzHDInsightJob `
| ~~~~~~~~~~~~~~~~~~~~~~
| Operation returned an invalid status code 'Forbidden'
Wait for the job to complete...
Wait-AzHDInsightJob: C:\Users\20450461\Documents\2Star.ps1:58:12
Line |
58 | -JobId $wordCountJob.JobId `
| ~~~~~~~~~~~~~~~~~~~
| Cannot bind argument to parameter 'JobId' because it is null.

Start-AzureRmAutomationRunbook is not accepting System.String parameter

I am trying to automate server reboot for one of our environment in Azure through azure automation.
I have created one runbook to know how many servers online.
Then trying pass the output as a parameter to another runbook and getting the
error.
below is the code.
$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
}
}
$Output = #()
$Running_XenApps = #()
$Output = .\Test.ps1
Foreach ($out in $Output)
{
$Running_XenApps += $out.Name | Out-String
}
Start-AzureRmAutomationRunbook –AutomationAccountName 'acm2eo-azure- automation' -Name 'Server-Reboot' -ResourceGroupName 'acm2eo-automation' -Parameters $Running_Xenapps -Runon acm2eo-hybrid-group1
I am getting below error.
Start-AzureRmAutomationRunbook : Cannot convert 'System.Object[]' to the type 'System.Collections.IDictionary' requiredby parameter 'Parameters'. Specified method is not supported.
Please let me know how to convert this.
Thanks in Advance.
You have to pass a hashtable / dictionary to the -Parameter param:
$params = #{
"YourParameterName" = $Running_XenApps
}
Start-AzureRmAutomationRunbook –AutomationAccountName 'acm2eo-azure- automation' -Name 'Server-Reboot' -ResourceGroupName 'acm2eo-automation' -Parameters $params -Runon acm2eo-hybrid-group1
As documented here: Start-AzureRmAutomationRunbook cmdlet.

Cube refresh with Automation Account in Azure SSAS

I'm currently trying to create an automation runbook to process a cube in Azure. I've tried multiple PowerShell scripts like this one :
$AzureCred = Get-AutomationPSCredential -Name "RefreshTest"
Add-AzureRmAccount -Credential $AzureCred | Out-Null
Invoke-ProcessASDatabase -databasename "MKTGCube" -server "AzureServerName" -RefreshType "Full" -Credential $AzureCred
With that kind of error (despite the fact that I installed the SQLServer module).
Invoke-ProcessASDatabase : The term 'Invoke-ProcessASDatabase' is not
recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or
if a path was included, verify that the path is
correct and try again.
Or this script :
$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
}
}
##Getting the credential which we stored earlier.
$cred = Get-AutomationPSCredential -Name 'CredMat'
## Providing the Server Details
$ServerName = "AzureServerName"
Invoke-ProcessASDatabase -databasename "MKTGCube" -server $ServerName –ProcessType "ProcessFull"
$error[0].Exception.Message
$error[0].Exception.StackTrace
With that error message.
Invoke-ProcessASDatabase : Authentication failed: User ID and Password
are required when user interface is not
available.
At line:32 char:1
Invoke-ProcessASDatabase -databasename "MKTGCube" -server $ServerName ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Invoke-ProcessASDatabase], ArgumentException
FullyQualifiedErrorId : System.ArgumentException,Microsoft.AnalysisServices.PowerShell.Cmdlets.ProcessASDatabase
I think the problem is linked to the credentials because we need to provide ones to access the source database but I have no ideas on how to do it through a PowerShell script. Any idea ?
Thanks a lot.
You need import module Azure.AnalysisServices and SqlServer to your automation account.
You could import these two module from this link and this link.
Note: There is a mistake in your script. You should use Login-AzureASAccount not Add-AzureRmAccount to login, you could use the following example:
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureAnalysisServicesAccount -RolloutEnvironment "southcentralus.asazure.windows.net" -ServicePrincipal -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint -TenantId $Conn.TenantID
Invoke-ProcessTable -Server "asazure://southcentralus.asazure.windows.net/myserver" -TableName "MyTable" -Database "MyDb" -RefreshType "Full"
More information about this please check this blog.

PowerShell script to refresh tabular cube on Azure

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 ?
$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
}
}

Error while running Azure runbook which executes PowerShell command on Virtual Machine

I am trying to execute this code in runbook, using "Invoke-Command" to connect to VM.
$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
# Use the subscription that this Automation account is in
$null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
Get-AzureRmVM | Select Name
$dcred = Get-AutomationPSCredential -Name 'myvm1creds'
Write-Output $DomainCred
$opts = New-PSSessionOption -SkipCACheck
Invoke-Command -Computername 'myVM1' -Credential $dcred -ScriptBlock {Get-Process} -SessionOption $opts
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Getting the below error:
[myVM1] Connecting to remote server myVM1 failed with the following error message : The WinRM client cannot process the
request. If the authentication scheme is different from Kerberos, or if the client computer is not joined to a domain,
then HTTPS transport must be used or the destination machine must be added to the TrustedHosts configuration setting.
Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. You
can get more information about that by running the following command: winrm help config. For more information, see the
about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (myVM1:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : ServerNotTrusted,PSSessionStateBroken
Any idea what have to be done to run powershell script via runbook on Azure Virtual Machines
In Azure runbook, we can't use transport HTTP to connect Azure VMs, because Azure runbook can't add trust host, so we need use HTTPS to connect Azure VMs.
Here are my steps:
1.Create a self-signed certificate.
Use makecert.exe to create it.
2.Config Winrm listen on HTTPS, run this script in CMD:
winrm create winrm/config/Listener?Address=*+Transport=HTTPS #{Port="5986" ;Hostname="jasonvm" ;CertificateThumbprint="98941E137CDF9553CCB0C28D5814EB9EDB1AC87D"}
3.Add port 5986 in Azure NSG inbound rules and windows firewall inbound rules.
4.we can use this runbook to connect Azure VM:
$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
$null = Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionID
Get-AzureRmVM | Select Name
$dcred = Get-AutomationPSCredential -Name 'jasonvm'
Write-Output $DomainCred
$opts = New-PSSession -ConnectionUri 'https://52.185.148.177:5986' -Credential $dcred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
Invoke-Command -Session $opts -ScriptBlock {Get-Process}
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
Here is my result: