Solved

compare email address

Posted on 2016-09-06
4
49 Views
Last Modified: 2016-09-06
Can you help me with this if statement. It should check to see if the ad users email address is equal to the email address imported from the csv.

Import-Csv "C:\Scripts\accounts\AmigosList.csv" | ForEach-Object {
$SID = $_.STD_ID_LOCAL
$User1 = Get-Aduser -Filter "SamAccountName -eq $SID" -Property SamAccountName
       
            if($User1.EmailAddress -eq $_.Email1){
                write-host "Email is already correct"
                }
            else{
                write-host "fix the email"
               }
}
0
Comment
Question by:Roccat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 40

Assisted Solution

by:footech
footech earned 125 total points
ID: 41786905
Your If statement should work as is if you add "emailAddress" to the -Property parameter of your Get-ADUser command.
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 125 total points
ID: 41786914
If statement should work, probably need to correct the filter syntax for Get-Aduser and add missing email property..
$User1 = Get-Aduser -Filter {SamAccountName -eq $SID} -pr *

Open in new window

or
$User1 = Get-Aduser -Filter 'SamAccountName -eq $SID' -pr *

Open in new window

0
 
LVL 84

Accepted Solution

by:
oBdA earned 250 total points
ID: 41786920
Can't test it at the moment, but the script below should work.
* Since you have the samAccountName anyway, there's no need for a filter, -Identity is just fine.
* You don't need to specify samAccountName as property, since it's a default property.
* In your own interest, don't use "$SID" for this - it usually means the user's Security Identifier.
* "EmailAddress" is not part of the default set, so you need to specify it in Get-ADUser.
* You won't know for which user the email address is correct or not ...
Import-Csv "C:\Scripts\accounts\AmigosList.csv" | ForEach-Object {
	"Processing $($_.STD_ID_LOCAL) ... "  | Write-Host -ForegroundColor White -NoNewline
	If ($ADUser = Get-Aduser -Identity $_.STD_ID_LOCAL -Property EmailAddress -ErrorAction SilentlyContinue) {
		If ($ADUser.EmailAddress -eq $_.Email1) {
			"email is already correct." | Write-Host -ForegroundColor Green
		} Else {
			"email needs to be fixed." | Write-Host -ForegroundColor Yellow
		}
	} Else {
		"user not found." | Write-Host -ForegroundColor Red
	}
}

Open in new window

0
 

Author Closing Comment

by:Roccat
ID: 41787046
Thanks! This was very helpful.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question