Go Premium for a chance to win a PS4. Enter to Win


SSAS: Automating SQL Server Agent service logon as administrator

Published on
3,448 Points
Last Modified:
Dorababu M
7+ years of experience in IT. Working as senior developer in Wallero, Hyderabad from past one year.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that with a PowerShell script.

Connect to your specific SQL analysis instance and go through properties to security.

You should see that there is no Service Agent in the list of Server Administrators. 

Figure 1 - Before

After executing my PowerShell script (below), the agent has been added.

Figure 2 - After script

The following PowerShell function will get the SQL service logon name associated with the specified computer and will add that user to the Analysis Server administrator group.

Function Add-SqlServiceLogonAccount
    [string] $SqlServerInstance = $env:computername,
    [string]  $AnalysisServerInstance,
    $ComputerName = $env:computername
        $ValidAnalysis = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")         | Out-String
            throw "Unable to find either Microsoft.AnalysisServices.AdomdClient or Microsoft.AnalysisServices in GAC"

        $SqlInstance =  $SqlServerInstance.Split("\")
#Getting the required SQL services running for both default localhost and as well for new Instances
        if($SqlServerInstance -match "\\")
            $SqlInstance = "SQLAgent`$$($SqlInstance[1])"
            $SqlInstance = "SQLSERVERAGENT"
            $SqlServiceDetails = Get-WmiObject -Class Win32_Service -ComputerName $ComputerName |
            select name,DisplayName, StartName, State |
            Where {$_.name -eq "$SqlInstance"  -and $_.State -eq "Running" }
        [string]$loginName = [string]::Empty
        if($SqlServiceDetails -ne $null)
            if($SqlServiceDetails.Name -eq $SqlInstance)
                $loginName = $SqlServiceDetails.StartName
            $Targetserver = new-Object Microsoft.AnalysisServices.Server
            #Getting members under the role Administrators
            $administrators = $Targetserver.Roles["Administrators"]
        #checking for the existence of loginname, if not exists adding member to Administrators group
            if ($administrators.Members.Name -notcontains $loginName) 
                Write-Host "Adding the agent logon account $loginName to the Administrators group"
                $administrators.Members.Add($loginName) | Out-Null
                Write-Host "Adding the agent logon account $loginName to the Administrators group"
                Write-Verbose "$loginName was already added to the Administrators group"
    throw $_.Message

Here are some examples of the above script in use.  Check the instance after running to ensure the user has been added correctly (as indicated in Figure 2 above)

Example 1 - Default SQL and Analysis instance

Add-SqlServiceLogonAccount -SqlServerInstance "localhost" -AnalysisServerInstance "localhost"

Example 2 - Named SQL and Analysis instance

Add-SqlServiceLogonAccount -SqlServerInstance "pc-name\local" -AnalysisServerInstance "pc-name\local"

Author:Dorababu M

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month