0

I would like to know if "MyDomain\My-AD-Group" has been added to the permissions on a mailbox (Exchange 2010).

$var = Get-MailboxPermission -identity "My-AD-Group" | select-object user

if($var[1] -like "MyDomain\My-AD-Group"){
    write-host "$var[1] has been added"
}else{
    $false
}

I think it does not work because there's some sort of "column header" displayed by $var[1]:

User
----
MyDomain\My-AD-Group

How do I get rid of that, so my IF \ ELSE would work?

Seth
  • 1,215
  • 15
  • 35
  • 3
    `$var` is still an object. You access the property by using `$var.user` – Scepticalist Sep 11 '21 at 10:47
  • 3
    Try using the `–ExpandProperty` switch when selecting the data. so ```$var = Get-MailboxPermission -identity "My-AD-Group" | select-object –ExpandProperty user``` – Otter Sep 11 '21 at 10:50
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the [this answer](https://stackoverflow.com/a/48809321/45375) to the linked duplicate for details and alternatives. – mklement0 Sep 11 '21 at 14:31

1 Answers1

0

Get-Mailboxpermission returns an object representing the permissions on that mailbox. You can use e.g. Format-List or Get-Member to get an idea of what further properties it has.

As it is an object you can access its properties by using the property name. In your example it would be $var[1].User. If I'm not mistaken the user itself is going to be an object. So you might have to access another property to do your comparison. In addition you're currently only checking the very first entry of the permission list.

What you likely want is more along these lines:

if( ($var | Where-Object {$_.User.Displayname -eq 'MyDomain\My-AD-Group'}) -ne $null){
    Write-Output "Permissions contain the group"
}
Seth
  • 1,215
  • 15
  • 35