Select specific value from Multi-Value attribute

Welcome Forums General PowerShell Q&A Select specific value from Multi-Value attribute

Viewing 2 reply threads
  • Author
    Posts
    • #192727
      Participant
      Topics: 28
      Replies: 44
      Points: 165
      Rank: Participant

      I need to be able to iterate through the values in a multi-value property (AD or Exchange) and set a variable with a specific value if it exists.

      I have read numerous articles on how to add, remove specific values from/to a multi-value property but haven't been able to successfully figure out how to capture a specific one into a variable for use later in the script.

      Example: Set variable if the address @contoso.com exists

      ProxyAddresses (AD) or EmailAddresses (Exchange) loop through these values and set $var1 to the value for the @contoso.com address if it exists.

      smtp: Jane.Hancock@northwinds.com
      smtp: Jane.Hancock@northwinds.net
      smtp: Jane.Hancock@contoso.net
      smtp: Jane.Hancock@contoso.com$

      I have this much:

      $var = get-mailbox jhancock | select EmailAddresses
      
      If($var -like "*@contoso.com")
      {
      
         What do I do here to capture the 'Jane.Hancock@contoso.com' into $var1?
      
      }
      
      
    • #192736
      Participant
      Topics: 9
      Replies: 25
      Points: 152
      Helping Hand
      Rank: Participant

      It might not be the most efficient, but I'd do ForEach ($add in $Var) {IF($Add -like "*@contoso.com") {$var1 = $Add}}.

       

      • #192742
        Participant
        Topics: 28
        Replies: 44
        Points: 165
        Rank: Participant

        At this point as long as it works, I will take it.   I can revise for efficiency at a later date.

        Thank you

         

    • #192772
      Participant
      Topics: 10
      Replies: 1381
      Points: 1,509
      Helping Hand
      Rank: Community Hero
      $mailboxes = @()
      $mailboxes += [pscustomobject]@{
          DisplayName = 'Jane Hancock'
          SamAccountName = 'jhancock'
          FirstName = 'Jane'
          LastName = 'Hancock'
          EmailAddresses = @(
              'smtp: Jane.Hancock@northwinds.com'
              'smtp: Jane.Hancock@northwinds.net',
              'smtp: Jane.Hancock@contoso.net',
              'smtp: Jane.Hancock@contoso.com$'
          )
      }
      
      $mailboxes += [pscustomobject]@{
          DisplayName = 'James Smith'
          SamAccountName = 'smith'
          FirstName = 'James'
          LastName = 'Smith'
          EmailAddresses = @(
              'smtp: James.Smith@northwinds.com'
              'smtp: James.Smith@northwinds.net',
              'smtp: James.Smith@contoso.net',
              'smtp: James.Smith@contoso.com$'
          )
      }
      
      $selectedUser = $mailboxes | Where {$_.SamAccountName -eq 'jhancock'}
      
      $contosoEmail = $selectedUser.EmailAddresses | Where {$_ -like '*@contoso.com*'}
      
      $contosoEmail
      
Viewing 2 reply threads
  • You must be logged in to reply to this topic.