Help with a logical operator question for script

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of brian catlin brian catlin 8 months, 2 weeks ago.

  • Author
    Posts
  • #37101
    Profile photo of brian catlin
    brian catlin
    Participant

    I created a little power shell script that basically will list out all the UPN and email in our domain.

    Get-ADUser -Filter 'Enabled -eq $true' -Properties * |Select-Object userPrincipalName,mail |export-csv -NoTypeInformation c:\scripts\UPN.csv

    It does the core of what I want it to do. However, I am needing a condition added.

    if UPN is not equal to email then the email is exported to my list.

    This way we have a list of all the emails that have a different or blank UPN.

    I know this can be done in excel but as I am weaker on the conditionals I figured this would be good to work on.

  • #37104
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    UPN shouldn't be blank

    Are you saying you just want the users where UPN and default email address aren't equal?

  • #37105
    Profile photo of brian catlin
    brian catlin
    Participant

    Yes that's exactly what I am saying.

    I was looking and found the -notMatch but then ran into questions on how to fit that all in there it was not as simple as I thought.

  • #37106
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Are you just after the -and operator? Help about_comparison* has the docs on that I believe.

  • #37107
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    You could use where-object like this

    PS> Get-ADUser -Filter * -SearchBase 'OU=Testing,DC=Manticore,DC=org' -Properties EmailAddress | where {$_.UserPrincipalName -ne $_.EmailAddress }

  • #37108
    Profile photo of brian catlin
    brian catlin
    Participant

    First off. Holy Cow Don Jones!!! I watched all your video and read your books.

    I have been studying CCNA and CCNP of late so my brain has been in a different area. So very rusty in the powershell area though its one of my favorite topics and look forward to hitting it up again.

    To Richard Thanks and the portion you added was exactly what I was looking for. I left out the -searchbase because we have so many OU I was just going to run the command against the domain for a wide sweep.

    I always forget about the where {} that annoys me because its so useful and I have studied it like 5 times and each time I need something like that in a slightly different way I never think to use it! arrrgggg!

    Anyway thanks!

  • #37110
    Profile photo of brian catlin
    brian catlin
    Participant

    some quick notes and questions. I changed this about and this one looks to have worked:

    Get-ADUser -Filter 'Enabled -eq $true' -Properties EmailAddress | where {$_.UserPrincipalName -ne $_.EmailAddress }|Select-Object EmailAddress | export-csv -NoTypeInformation c:\scripts\UPN.csv

    the first time I did |Select-Object mail but nothing came back in my export but the word mail...

    now the current code above works but I have empty spaces in my csv sheet that I converted to excel.

    Is this because it still places a blank place holder there?

    Anyway I think this works I really appreciate the help.

  • #37125
    Profile photo of David Senior
    David Senior
    Participant

    The blank spaces are users who have no value for the attribute EmailAddress.
    Try selecting Name and EmailAddress.
    Like this:

    Get-ADUser -Filter 'Enabled -eq $true' -Properties EmailAddress | where {$_.UserPrincipalName -ne $_.EmailAddress} | select Name,EmailAddress | Export-Csv -Path 'C:\Scripts\UPN.csv' -NoTypeInformation
    
  • #37128
    Profile photo of brian catlin
    brian catlin
    Participant

    Hi David,

    Thanks for the reply and confirming the blanks. Also for the variation of the code.

    I am very thankful for this website. I have been members of a lot of sites but this one is the best and in fact is one of the best forums out there regardless of the technology. So thanks to the founders as well.

    I love learning and I have a deep and profound respect for those that share knowledge and take the time to help out. I do the same with others as well. I am quick to ask questions its true but I also put in a lot of work and take education and learning very seriously.

    So very thankful for this site and the people who manage it as well as those who give their assistance to others.

    =)

You must be logged in to reply to this topic.