Solved

compare email address

Posted on 2016-09-06
4
45 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 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

837 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