Problem with creating SSAS Roles using Powershell

by MSBerlin   Last Updated August 25, 2017 12:06 PM

I'm using powershell to (mass) delete and create roles in SSAS. Deleting is done like this :


$rolecount = $db.Roles.count
    for ($i = 0; $i –lt $rolecount; $i++) {
        Write-host ($db.Roles.item(0).name) 
        $db.Roles.item(0).Drop([Microsoft.AnalysisServices.DropOptions]::AlterOrDeleteDependents)
    }
$db.Update()

and adding like this :


        if (!$db.Roles.Contains($sRole))
        {
                $db.roles.add($sRole) > $null
                $role = $db.Roles.GetByName($sRole)
                $role.update()
        }
        else
        {
            Write-host "[$((get-date).ToString('HH:mm:ss'))] -> Role '$sRole' already exists !"
        }

with $db being an instance of the "Microsoft.AnalysisServices.Database" class. When the role is added permissions etc. are added too in a similiar fashion. 98% of the time that works fine, but ever so often i get the following error message back from SSAS : the object (database role) with the name [insert role name here, its always a different one] already exists in [insert database name here] (i translated the error message, actual wording in english could be different). The script output shows that the same role has been deleted moments ago with no errors. I even put a debug output into the script that counted the number of roles after deletion, it was 0. Has anyone experienced this behavior before ? i've tried for ages to find a way to reliably reproduce the error in order to fix it.



Related Questions


SQL Server AS: Set ImpersonationMode via PS

Updated June 19, 2015 23:02 PM

Server role with permission to create server login

Updated March 04, 2017 13:06 PM



PostgreSQL Role Configuration

Updated September 07, 2017 07:06 AM