locked
trying to confirm the properties of an object match eachother from get-mailbox RRS feed

  • Question

  • Ok, i give up, with help from jrv getting the syntax right, the script is now formatting and showing the Sip address for each user.  i am trying to compare two properties from each object that is returned, the userprincipal and the emailaddresses that pipes the object to a true false if statement. I am not sure what i am doing wrong. I am guessing it has to do with using the wrong variables in the if statement but i am not sure. I tried foreach and its abreviation % with no luck.


    Get-Mailbox opr4318 |
    Select-Object DisplayName, 
    	Samaccountname, 
    	Userprincipalname, 
            @{n='EmailAddresses';e={$_.EmailAddresses | ?{$_ -match '^sip'}|%{$_ -replace 'sip:',''}}} | 
            
          %{If($_ -eq "$($_.Userprincipalname)"){'True'}else{'False'}}


    Tuesday, April 19, 2016 2:52 PM

Answers

  • Like this?

    Get-Mailbox opr4318 |
    Select-Object DisplayName,
    			Samaccountname,
    			Userprincipalname,
    			@{ n = 'EmailAddresses'; e = { $_.EmailAddresses | ?{ $_ -match '^sip' } | %{ $_ -replace 'sip:', '' } } },
                  		@{n='Matches';e={$_.EmailAddresses -contains "SIP:$($_.UserPrincipalName)" }}
    


    \_(ツ)_/


    • Edited by jrv Tuesday, April 19, 2016 5:09 PM
    • Marked as answer by glacket Tuesday, April 19, 2016 5:56 PM
    Tuesday, April 19, 2016 5:08 PM

All replies

  • Here's an example:

    Get-Mailbox tester1 | 
        Select-Object DisplayName,
                      SamAccountName,
                      UserPrincipalName,
                      WindowsEmailAddress,
                      @{N='AddressMatch';E={
                        If ($_.UserPrincipalName -eq $_.WindowsEmailAddress) {$true} Else {$false}
                      }}

    This example uses a different property than you are, as I don't have SIP addresses in my environment to test with, but the idea is the same.


    Tuesday, April 19, 2016 4:35 PM
  • Like this?

    Get-Mailbox opr4318 |
    Select-Object DisplayName,
    			Samaccountname,
    			Userprincipalname,
    			@{ n = 'EmailAddresses'; e = { $_.EmailAddresses | ?{ $_ -match '^sip' } | %{ $_ -replace 'sip:', '' } } },
                  		@{n='Matches';e={$_.EmailAddresses -contains "SIP:$($_.UserPrincipalName)" }}
    


    \_(ツ)_/


    • Edited by jrv Tuesday, April 19, 2016 5:09 PM
    • Marked as answer by glacket Tuesday, April 19, 2016 5:56 PM
    Tuesday, April 19, 2016 5:08 PM
  • Combination of both previous codes

    Get-Mailbox opr4318 | Select-Object DisplayName, Samaccountname, Userprincipalname, @{ n = 'EmailAddresses'; e = { $_.EmailAddresses | ?{ $_ -match '^sip' } | %{ $_ -replace 'sip:', '' } } }, @{n='Matches';e={ If ($_.Emailaddresses -eq $_.UserprincipleName) {true} Else {false}

    }}


    Tuesday, April 19, 2016 5:16 PM
  • Combination of both previous codes

    Get-Mailbox opr4318 | Select-Object DisplayName, Samaccountname, Userprincipalname, @{ n = 'EmailAddresses'; e = { $_.EmailAddresses | ?{ $_ -match '^sip' } | %{ $_ -replace 'sip:', '' } } }, @{n='Matches';e={ If ($_.Emailaddresses -eq $_.UserprincipleName) {true} Else {false}

    }}


    Not necessary and it won't work.  The result of any logical comparison is always already $true or $false:

    (1 -eq 0).GetType()

    'x' -eq 'y'

    Look at what is returned.  You do not need to also reassign the output.  This is one reason why we use computers.  They already do all of this for us.

    Learn the fundamentals kids.  It will save you a lot of wasted time.


    \_(ツ)_/

    Tuesday, April 19, 2016 5:21 PM
  • Like this?

    Get-Mailbox opr4318 |
    Select-Object DisplayName,
    			Samaccountname,
    			Userprincipalname,
    			@{ n = 'EmailAddresses'; e = { $_.EmailAddresses | ?{ $_ -match '^sip' } | %{ $_ -replace 'sip:', '' } } },
                  		@{n='Matches';e={$_.EmailAddresses -contains "SIP:$($_.UserPrincipalName)" }}


    \_(ツ)_/



    Thats awesome. Inever even thought about using the -contains switch. So you added another property "Matches" and used the -contain switch to populate the property with a true or false based on if it matches or not. And i noticed you used a comma instead of a pipe because it is an additional property, is that correct?
    Tuesday, April 19, 2016 5:30 PM

  • Not necessary and it won't work.  The result of any logical comparison is always already $true or $false:

    (1 -eq 0).GetType()

    'x' -eq 'y'

    Look at what is returned.  You do not need to also reassign the output.  This is one reason why we use computers.  They already do all of this for us.

    Learn the fundamentals kids.  It will save you a lot of wasted time.


    \_(ツ)_/

    Damn haha you got me there , gotta keep learning and practicing
    Gracias
    Tuesday, April 19, 2016 5:31 PM
  • Thats awesome. Inever even thought about using the -contains switch. So you added another property "Matches" and used the -contain switch to populate the property with a true or false based on if it matches or not. And i noticed you used a comma instead of a pipe because it is an additional property, is that correct?
    You got it.

    \_(ツ)_/

    Tuesday, April 19, 2016 5:33 PM
  • Damn haha you got me there , gotta keep learning and practicing
    Gracias

    You will get there. I find that, with a new technology, I have to go back to the books repeatedly to clarify what I am learning. Misinformation creeps in and makes it harder to understand what is happening.


    \_(ツ)_/

    Tuesday, April 19, 2016 5:34 PM
  • I did have one more question, hopefully this is in scope. If i wanted to pipe it out to an if statement could i just use a | element with foreach? would enumerate each object using the same variables($_.emailaddresses,$_.userprincipalname, etc)?
    Tuesday, April 19, 2016 8:51 PM
  • ??? Pipe to am "if"  What does that do.

    Are you saying you want to filter the results?

    Just filter,

    Where{ $_.Matches }


    \_(ツ)_/

    Tuesday, April 19, 2016 9:26 PM
  • That makes more sense. Thanks!
    Wednesday, April 20, 2016 12:00 PM