none
Azure Analysis Services - Pausing

    Question

  • Hello,

    I am trying to Schedule my Azure SSAS server for it to just run on weekdays between 8 and 6 and i want to pause it for the rest of the time. 

    Here is the script that i am using - 

    param(
    
    [string]
    $environmentName = "AzureCloud",
    
    [string]
    $resourceGroupName = "PowerProd",
    
    [string]
    $serverName ="poweranalysis",
    
    [string]
    $azureProfilePath = "",
    
    [string]
    $azureRunAsConnectionName = "AzureRunAsConnection",
    
    [string]
    $configStr =
    "
    
    [
    
    {
    
         WeekDays:[1,2,3,4,5]    
    
         ,StartTime: 
    ""08:00:00""
    
         ,StopTime: 
    ""17:59:59""
    
         ,Sku: 
    ""S4""
    
        } 
    
    ]
    
    "
    
    )
    
    filter timestamp 
    {"[$(Get-Date -Format G)]:
    $_"}
    
    
    Write-Output "Script started." 
    | timestamp
    
    $VerbosePreference 
    = "Continue"
    
    $ErrorActionPreference 
    = "Stop"
    
    #Authenticate with Azure Automation Run As account (service principal)
    
    $runAsConnectionProfile 
    = Get-AutomationConnection -Name 
    $azureRunAsConnectionName
    
    $environment 
    = Get-AzureRmEnvironment -Name 
    $environmentName
    
    Add-AzureRmAccount -Environment 
    $environment -ServicePrincipal `
    
    -TenantId $runAsConnectionProfile.TenantId `
    
    -ApplicationId 
    $runAsConnectionProfile.ApplicationId `
    
    -CertificateThumbprint ` 
    $runAsConnectionProfile.CertificateThumbprint
    | Out-Null
    
    Write-Output "Authenticated with Automation Run As Account."
    | timestamp
    
    
    #Get current date/time and convert to $scalingScheduleTimeZone
    
    $stateConfig 
    = $scalingSchedule 
    | ConvertFrom-Json
    
    $startTime = Get-Date
    
    Write-Output "Azure Automation local time: 
    $startTime."
    | timestamp
    
    $toTimeZone =
    [System.TimeZoneInfo]::FindSystemTimeZoneById($scalingScheduleTimeZone)
    
    Write-Output "Time zone to convert to: 
    $toTimeZone."
    | timestamp
    
    $newTime =
    [System.TimeZoneInfo]::ConvertTime($startTime,
    $toTimeZone)
    
    Write-Output "Converted time: $newTime."
    | timestamp
    
    $startTime =
    $newTime
    
    
    #Get current day of week based on converted start time
    
    $currentDayOfWeek 
    = [Int]($startTime).DayOfWeek
    
    Write-Output "Current day of week: $currentDayOfWeek."
    | timestamp
    
    
    # Get the scaling schedule for the current day of week
    
    $dayObjects =
    $stateConfig | Where-Object
    {$_.WeekDays
    -contains $currentDayOfWeek
    } `
    
    |Select-Object Sku, `
    
    @{Name="StartTime"; Expression
    = {[datetime]::ParseExact(($startTime.ToString("yyyy:MM:dd")+”:”+$_.StartTime),"yyyy:MM:dd:HH:mm:ss",
    [System.Globalization.CultureInfo]::InvariantCulture)}}, `
    
    @{Name="StopTime"; Expression
    = {[datetime]::ParseExact(($startTime.ToString("yyyy:MM:dd")+”:”+$_.StopTime),"yyyy:MM:dd:HH:mm:ss",
    [System.Globalization.CultureInfo]::InvariantCulture)}}
    
    
    # Get the server object
    
    $asSrv = Get-AzureRmAnalysisServicesServer
    -ResourceGroupName 
    $resourceGroupName -Name 
    $serverName
    
    Write-Output "AAS server name: $($asSrv.Name)"
    | timestamp
    
    Write-Output "Current server status: 
    $($asSrv.State), sku:
    $($asSrv.Sku.Name)"
    | timestamp
    
    
    if($dayObjects
    -ne $null)
    { # Scaling schedule found for this day
    
    # Get the scaling schedule for the current time. If there is more than one available, pick the first
    
    $matchingObject 
    = $dayObjects 
    | Where-Object { 
    ($startTime 
    -ge $_.StartTime)
    -and ($startTime
    -lt $_.StopTime)
    } | Select-Object
    -First 1
    
    if($matchingObject
    -ne $null)
    
    {
    
    Write-Output "Scaling schedule found. Check if server is paused and if current sku is matching..."
    | timestamp
    
    if($asSrv.State
    -eq "Paused")
    
    {
    
    Write-Output "Server was paused. Resuming!"
    | timestamp
    
    $asSrv | Resume-AzureRmAnalysisServicesServer
    
    Write-Output "Server resumed." 
    | timestamp
    
    }
    
    if($asSrv.Sku.Name
    -ne $matchingObject.Sku)
    
    {
    
    Write-Output "Server is not in the sku of the scaling schedule. Changing!"
    | timestamp
    
    $asSrv = Set-AzureRmAnalysisServicesServer
    -Name $asSrv.Name
    -ResourceGroupName 
    $resourceGroupName -Sku 
    $matchingObject.Sku
    
    Write-Output "Change to edition/tier as specified in scaling schedule initiated..."
    | timestamp
    
    $asSrv = Get-AzureRmAnalysisServicesServer
    -ResourceGroupName 
    $resourceGroupName -Name 
    $serverName
    
    Write-Output "Current server state: 
    $($asSrv.State), sku:
    $($asSrv.Sku.Name)"
    | timestamp
    
    }
    
    else
    
    {
    
    Write-Output "Current server sku matches the scaling schedule already. Exiting..."
    | timestamp
    
    }
    
    }
    
    else {
    # Scaling schedule not found for current time
    
    Write-Output "No matching scaling schedule time slot for this time found. Check if the server is paused..."
    | timestamp
    
    if($asSrv.State
    -ne "Paused")
    
    {
    
    Write-Output "Server not paused. Pausing!"
    | timestamp
    
    $asSrv | Suspend-AzureRmAnalysisServicesServer
    
    Write-Output "Server paused." 
    | timestamp
    
    $asSrv = Get-AzureRmAnalysisServicesServer
    -ResourceGroupName 
    $resourceGroupName -Name 
    $serverName
    
    Write-Output "Current server sate: $($asSrv.State), sku:
    $($asSrv.Sku.Name)"
    | timestamp
    
    }
    
    else
    
    {
    
    Write-Output "Server paused already. Exiting..."
    | timestamp
    
    }
    
    }
    
    }
    
    else # Scaling schedule not found for this day
    
    {
    
    Write-Output "No matching scaling schedule for this day found. Check if the server is paused..."
    | timestamp
    
    if($asSrv.State
    -ne "Paused")
    
    {
    
    Write-Output "Server not paused. Pausing!"
    | timestamp
    
    $asSrv | Suspend-AzureRmAnalysisServicesServer
    
    Write-Output "Server paused." 
    | timestamp
    
    $asSrv = Get-AzureRmAnalysisServicesServer
    -ResourceGroupName 
    $resourceGroupName -Name 
    $serverName
    
    Write-Output "Current server state: 
    $($asSrv.State), sku:
    $($asSrv.Sku.Name)"
    | timestamp
    
    }
    
    else
    
    {
    
    Write-Output "Server paused already. Exiting..."
    | timestamp
    
    }
    
    } 
    
    
    Write-Output "Script finished."
    | timestamp
    

    But i keep getting an error saying - 

    The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot bind argument to parameter 'InputObject' because it is null.

    Can you please help me and let me know how i can get around this.

    Thank you very much..!  


    • Edited by Surya Gari Thursday, December 6, 2018 10:01 PM Updated formatting
    Thursday, December 6, 2018 5:07 PM

All replies

  • Ask the author of the script to fix it for you.

    (Fixing scripts written by others is beyond the scope of this forum)


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by jrv Thursday, December 6, 2018 9:50 PM
    Thursday, December 6, 2018 7:38 PM
    Moderator
  • Please do not post images of code.  Use the code posting tool provided on the edit bar.


    \_(ツ)_/

    Thursday, December 6, 2018 9:50 PM
  • Its not an image. I pasted it from Azure Runbook.
    Thursday, December 6, 2018 9:53 PM
  • Its not an image. I pasted it from Azure Runbook.

    Please only post correctly formatted code with the code posting tool.  Colorized code from a browser or other HTML source cannot be copied correctly and is very hard to read.


    \_(ツ)_/

    Thursday, December 6, 2018 9:58 PM
  • Thanks for your responses. Found a work around.

    Friday, December 7, 2018 9:22 PM