Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

Powershell script to check for file if external contact exist in OU

Hello Experts,

I need a powershell script to do this:

i have a txt file with 3000 external contact like this:

toto@mail.com
titi@mailb.com
.....

the script must verify for every contact if it is exist in a specific OU in AD and export the result of no present contact in a txt file.

thanks for your help
Avatar of stefor
stefor
Flag of Sweden image

I have no way to troubleshoot this from where I'm sitting now, but please post the errors you receive and I'll continue working on it.

$OU = "domain.local/Company/Contacts"
$List = Get-Content "C:\temp\contacts.txt"
$Contacts = Get-MailContact -OrganizationalUnit $OU -ResultSize unlimited
$List | foreach {
	IF (-not($Contacts.EmailAddresses -contains $_))
		{
		$_ " is gone!" 
		}
	}

Open in new window

Avatar of cawasaki
cawasaki

ASKER

Hi Stefor,

I have an error:

Unexpected token ' is gone!' in expression or statement.
At C:\temp\PLIP\PLOP.ps1:7 char:17
+         $_ " is gone!" <<<<
    + CategoryInfo          : ParserError: ( is gone!:String) [], ParseExcepti
   on
    + FullyQualifiedErrorId : UnexpectedToken

Open in new window

$OU = "domain.local/Company/Contacts"
$List = Get-Content "C:\temp\contacts.txt"
$Contacts = Get-MailContact -OrganizationalUnit $OU -ResultSize unlimited
$List | foreach {
	IF (-not($Contacts.EmailAddresses -contains $_))
		{
		Write-Host $_ " is gone!" 
		}
	}

Open in new window


Try this, now with added "Write-Host"
no error now but the script is not good:

i have test and put in my txt file 3 adress and only one really exist.

the result in screen:

toto@mail.com  is gone!
titi@maill.fr  is gone!
tata@mail.fr  is gone!


tata@mail.fr is really exist and 2 other not exist.
If you have a file with contacts like this
toto@mail.com
titi@maill.fr
tata@mail.fr

Call it "contacts.txt"

then you can try the folowing code using Adsisearcher.

NOTE: I am using the attribute "mail" here to find if the e-mail address exists. But you can try other attributes like displayName, givenName or even distinguishedName
$contactfiles = Import-Csv "D:\test\contacts.txt" 

foreach ($contact in $contactfiles) {
$contact2search=$contact.header
$searchresult = ([Adsisearcher]"mail=$($contact2search)").Findone()

if ($searchresult) {"TRUE: $contact2search found in AD"}
else {"FALSE: $contact2search not found in AD"}

}

Open in new window

ensermo, i need to check in a specific OU not entire AD.
$OU = "domain.local/Company/Contacts"
$List = Get-Content "C:\temp\contacts.txt"
$i = 0
$y = $List.count
$Contacts = Get-MailContact -OrganizationalUnit $OU -ResultSize unlimited
$Contacts | While ($i -lt $y) { foreach{
		$Contact = $List[$i]
		IF (-not($_.EmailAddresses -contains $Contact))
		{
		Write-Host $Contact "is missing"
		}
		}
		$i++
}

Open in new window

The attribute "mail" doesn't contain all proxy addresses on an object.
STEFOR:

Missing opening '(' after keyword 'foreach'.
Au niveau de C:\temp\PLIP\PLOP.ps1 : 6 Caractère : 40
+ $Contacts | While ($i -lt $y) { foreach <<<< {
    + CategoryInfo          : ParserError: (OpenParenToken:TokenId) [], ParseE
   xception
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

Open in new window

stefor?
Try this one.

Change the variable to include your OU.
$searchresult.SearchRoot="LDAP://OU=MailObjects,DC=domain,DC=local"
$contactfiles = Import-Csv "D:\test\contacts.txt" 

foreach ($contact in $contactfiles) {

$contact2search=$contact.header

$searchresult = [adsisearcher]""
$searchresult.SearchRoot="LDAP://OU=MailObjects,DC=domain,DC=local"
$searchresult.Filter = "proxyAddresses=smtp:$($contact2search)"
$result = $searchresult.FindOne()

if ($result) {"TRUE: $contact2search found in AD"}
else {"FALSE: $contact2search not found in AD"}

}

Open in new window

ensermo

you script dont work:

[PS] C:\temp\PLIP>.\check.ps1
FALSE:  not found in AD
Remember to define where your contact file is
In this example the contact file is located at "D:\test\contacts.txt"
Change it in line -->  $contactfiles = Import-Csv "D:\test\contacts.txt"

Also the first line of the text file should be the "column name" . I have called it "header" in this
Example of contacts.txt

header
test@kpn.com
camera@hotmaill.com
admin@mydomain.com
it-contact@mydomain.nl
...
...
..

enserno, its work very good,

can you add to this script the export of result to a file?

thanks
ASKER CERTIFIED SOLUTION
Avatar of ensermo
ensermo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks work :)