Last active
February 21, 2020 06:48
-
-
Save garima2510/99c70c5f87c7a54a15f9fc081e883b65 to your computer and use it in GitHub Desktop.
Powershell to asynchronously refresh analysis server model in azure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using namespace System.Net | |
# Input bindings are passed in via param block. | |
param($Request, $TriggerMetadata) | |
# Init defaults | |
$status = [HttpStatusCode]::BadRequest | |
$ErrorActionPreference = "STOP" | |
$RequestBody = $Request.Body | |
if (!$RequestBody) { | |
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | |
StatusCode = $status | |
Body = "Please pass a valid body in the request's body." | |
}) | |
} | |
else { | |
# Retrieve AAS details from request body | |
$AnalysisServerName = $RequestBody.AnalysisServerName | |
$AnalysisServerLocation = $RequestBody.AnalysisServerLocation | |
$ModelName = $RequestBody.ModelName | |
$AASObject = $RequestBody.AASObject | |
#check if AASObject is empty or not. Irrespective of documentation | |
#We need Refresh Type for a call as mandatory param | |
if (!$AASObject -or ($AASObject.Count -eq 0)) { | |
$AASObject = @{ | |
'Type' = 'Full' | |
} | |
} | |
Connect-AzAccount | |
try { | |
$analysisServerResource = Get-AzAnalysisServicesServer -Name $AnalysisServerName | |
Write-Output "Fetching access token to refresh the model" | |
#fetch access token which will be used to refresh the models | |
$Output = New-Object -TypeName hashtable | |
Get-AccessToken -Output $Output #fetch access token using the PS here https://adamtheautomator.com/microsoft-graph-api-powershell/ | |
#request body to be sent to AAS REST API | |
$jsonBody = $AASObject | ConvertTo-Json; | |
Write-Output "The body of the REST API call to refresh the model is" | |
Write-Output $jsonBody | |
$uri = "https://$AnalysisServerLocation.asazure.windows.net/servers/$AnalysisServerName/models/$ModelName/refreshes" | |
Write-Output "Invoking POST $uri" | |
#$Output.AuthHeaders follows the following format | |
#$Output.AuthHeaders = @{ | |
#'Content-Type' = 'application/json' | |
#'Authorization' = "Bearer " + access_token | |
#} | |
$PostSplat = @{ | |
Method = 'POST' | |
Body = $jsonBody | |
Uri = $uri | |
Headers = $Output.AuthHeaders | |
} | |
$results = Invoke-RestMethod @PostSplat | |
Write-Output $results | |
$status = [HttpStatusCode]::OK | |
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | |
StatusCode = $status | |
Body = $results | |
}) | |
} | |
catch { | |
Write-Output "Exception was thrown. Details below." | |
$_ | |
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | |
StatusCode = $status | |
Body = $_ | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment