Remove method not working

i know this is powershell forum mainly, but since i havent received much help elsewhere (https://stackoverflow.com/questions/55112876/why-is-remove-from-role-members-not-working)
https://social.technet.microsoft.com/Forums/en-US/d31dd1e3-dde9-4031-8234-ab3a77487bbd/is-remove-method-bugged?forum=sqlanalysisservices

i wont lose anything by posting here as lost shot at this.

also the code is pretty much powershell so why not :slight_smile: although i can understand if people cant help with this since its also SSAS

For some reason, the Remove() is not working properly. I keep reconnecting the server and checking if the ID gets removed and its not!

$ssasInstance = "<ssas instance name>"
$accountName= "<domain\user>"
Import-Module SqlServer
[Microsoft.AnalysisServices.Server]$SSASserver = New-Object ([Microsoft.AnalysisServices.Server])
$SSASserver.Connect($ssasInstance) 
$role= $SSASserver.Roles["Administrators"]
$role.Members.Remove($accountName)
$role.Update()

there is no errors or anything. i did a conditional statement to check if ID exists, and if it does, remove and output removed!

i do get output

removed ID!
but the ID is not really getting removed!

I have tried ADD, so that i know nothing is wrong with the script, and add works! only remove for some reason is not workingā€¦

on technet, Zoe misunderstood my post and thought i am referring to database members/roles. i am referring to server level administrators

i am using the code from here,

https://stackoverflow.com/questions/43538887/ssas-adding-members-to-server-administrator-role-using-powershell?rq=1

but this is for Adding users. Adding works just fine as i had mentioned, so i simply found that there is also remove(), and instead of add made the code remove. but i check the server and it still doesnt get removed. is the method bugged?

any reason you are not trying to use the built-in powershell cmdlet for this?

assuming you have 5.1 installed.
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localgroupmember?view=powershell-5.1

Here is a post that does it by index: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/633c72de-a4c8-4daa-9bf3-7f2248eeb2fd/programmaticaly-removing-users-from-ssas-database-role?forum=sqlanalysisservices

They also indicate that the Remove method does not work for a name reference, so maybe try:

$ssasInstance = ""
$accountName= ""
Import-Module SqlServer
[Microsoft.AnalysisServices.Server]$SSASserver = New-Object ([Microsoft.AnalysisServices.Server])
$SSASserver.Connect($ssasInstance) 
$role= $SSASserver.Roles["Administrators"]

foreach ($mbr in $role.Members) {
    if ($mbr.Name -eq $accountName) {
        $role.Remove($mbr)
        $role.Update()
     }
}

[quote quote=144287]any reason you are not trying to use the built-in powershell cmdlet for this?

assuming you have 5.1 installed.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/remove-localgroupmember?view=powershell-5.1

[/quote]

  1. hmm interestingā€¦I wasnt aware of it

  2. i tried it, didnt really work eitherā€¦how doe sit even know that its removing from the server? i dont see an SSAS server parameter optionā€¦

[quote quote=144288][/quote]
Method invocation failed because [Microsoft.AnalysisServices.Role] does not contain a method named ā€˜Removeā€™.

ISSUE is now RESOLVED!

https://stackoverflow.com/a/55145118/8397835

Ā 

This is an unfortunate confluence of circumstances: when you do

$role.Members.Add("member")

This works because, under water, the string ā€œmemberā€ is implicitly converted by PowerShell to a RoleMember with a Name of member and an empty Sid. All fine. However, if you then do

$role.Members.Remove("member")

Nothing happens, because you will create a new RoleMember instance, and since RoleMember has no implementation of .Equals() (a fairly bizarre oversight), different instances will never be considered the same.

This means you can only remove members by getting the actual instance in the collection (or by index, I suppose):

$member = $role.Members.Where{$_.Name -eq "member"} | Select -First 1
$role.Members.Remove($member)

Note that you will not get an error if there is no such member (because Remove allows $null, again in a rather bizarre oversight), so you may want to check for $member -eq $null if you want to verify that.

Definitely do not use Remove-LocalGroupMember ā€“ that cmdlet is part of a completely different moduleand removes members from local Windows groups, not SSAS roles.

ahh sorry, i saw ā€œserver level administratorsā€
and interpreted that to be the local administrators group on the windows server.

glad you found an answer