Solved

compare email address

Posted on 2016-09-06
4
29 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
4 Comments
 
LVL 39

Assisted Solution

by:footech
footech earned 125 total points
Comment Utility
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
Comment Utility
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 82

Accepted Solution

by:
oBdA earned 250 total points
Comment Utility
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
Comment Utility
Thanks! This was very helpful.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
A procedure for exporting installed hotfix details of remote computers using powershell
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now