locked
Look for a value in any of those Custom Attribute of Exchange / ActiveDirectory in PowerShell? RRS feed

  • General discussion

  • Hello,

    In Exchange 2010, every (mailbox) user object has a list of Custom Attributes from 1 to 15.  In PowerShell, it's possible to check if say CustomAttribute2 has a value of "ABC".  But is it possible to check all the Custom Attributes from 1 to 15 for the same value "ABC" no matter where it is in PowerShell?  For example, UserA might have it in CustomAttribute5, and userB might have it in CustomAttribute11, etc.

    And of course, I would like the code to be reusable and usable inside another code or command line.  Could somebody help me?

    Thanks in advance.

    Wednesday, April 1, 2015 4:34 PM

All replies

  • This is possible but we will not write it for you.  You need to post your script and ask a specific question.

    I suggest returning all attributes and looking at each one.

    If this is important I recommend contacting a consultant to do it for you.


    ¯\_(ツ)_/¯

    Wednesday, April 1, 2015 4:40 PM
  • Who gave you the right to represent the "we" you're talking about?  If you don't want to help, you could have left the speech to others.  What a dictator!
    Wednesday, April 1, 2015 4:48 PM
  • This forum is for scripting questions rather than scripting requests.

    What have you tried so far?

    If you are not familiar with scripting, you can post a script request here:

    http://gallery.technet.microsoft.com/scriptcenter/site/requests


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 1, 2015 5:24 PM
  • Based on the following command:

    get-mailbox | where-object {$_.customattribute1 -eq "ABC"}

    I was trying to make something similar and elegant.  But I hit some obstacles because I'm not familiar with PowerShell.  I master quite well JavaScript in which it's possible to add a new method to an existing object.  So my first thought was to create a cmdlet file called customattributeX which would allow me to use a command like this

    get-mailbox | where-object {$_.customattributeX -eq "ABC"}

    The beginning of such file is of course

    Param (
        [string]$eq
    )

    But my questions are:

    1. How to add a new method to mailbox object?  Or is it just impossible in PowerShell?

    2. How am I suppose to populate a list of mailbox objects?  I mean, suppose I have the initial mailboxlist inside the variable $mbl, then could I write instructions like the following?

    $m1 = $mbl | where-object {$_.customattribute1 -eq $eq}
    $m2 = $mbl | where-object {$_.customattribute2 -eq $eq}
    $m3 = $mbl | where-object {$_.customattribute3 -eq $eq}
    ...
    $m15 = $mbl | where-object {$_.customattribute15 -eq $eq}

    And how can I put those $mX inside one list?  Or is it impossible in PowerShell?

    Of course, a trivial solution would be to create a cmdlet which accepts list of "mailbox" objects and a string as parameters, something like

    $mboxlist = get-mailbox
    get-mailbox-from-customattributes -mailboxlist $mboxlist -value "ABC"

    And inside the cmdlet file, I just repeat my instructions above.  But that's really ugly!



    • Edited by Horinius Wednesday, April 1, 2015 7:34 PM
    Wednesday, April 1, 2015 7:31 PM
  • The nice thing about Powershell is you can convert a string to a scriptblock object without much effort.  Once you've got a hold of manipulating the string just convert each one them into a scriptblock and run it.

    Start with this:

    $scriptTemplate = 'Write-Host "{0}"'
    1..15 | % { & ([scriptblock]::Create($scriptTemplate -f $_)) }

    Wednesday, April 1, 2015 9:43 PM
  • Who gave you the right to represent the "we" you're talking about?  If you don't want to help, you could have left the speech to others.  What a dictator!

    As the local dictator it is my duty to explain that you need to learn how these objects are designed.  You can guess or you can learn. What customatributes do you want. On the user, mailbox of distribution list?

    $s = 'ABC'
    get-mailbox | ForEach-Object{ $found=$_.CustomAttribute1 -eq $s -or $_.CustomAttribute2 -eq $s -or $_.CustomAttribute3 -eq $s -or $_.CustomAttribute4 -eq $s -or $_.CustomAttribute5 -eq $s -or $_.CustomAttribute6 -eq $s -or $_.CustomAttribute7 -eq $s New-Object PsObject -Property @{ Mailbox = $_.Alias; Found = $found } }

    You can fill in the redundant fields.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, April 2, 2015 4:54 PM
    Thursday, April 2, 2015 4:53 PM
  • As the local subject to this dictatorship, I still dare to say that your code is not very reusable.
    Friday, April 3, 2015 9:12 PM
  • If you want something more concise, you can use use a loop to cycle through all CustomAttributes.

    PowerShell allows you to use strings to call properties of objects (not methods though).

    So basically you can do silly things like this...

    [string].("Full" + "Na" + "me")

    which has the same effect as:

    [string].FullName

    If I wasn't doing string manipulation in there I could simplify it even further, by doing:

    [string]."FullName"

    And even do things such as:

    $a = "FullName"
    [string].$a

    Basically they all return exactly the same result.

    What this means in your case is that if you don't want to do a -or for each attribute you can just do the following (using jrv's code):

    $s = 'ABC'
    get-mailbox | ForEach-Object {
       for ($i = 1; $i -eq 15; $i++)
       {
          $found = $false
          if ($_."CustomAttribute$i" -eq $s)
          {
              $found = $true
              break
          }
       }
       New-Object PsObject -Property @{ Mailbox = $_.Alias; Found = $found }
    }

    Tuesday, April 7, 2015 12:20 AM