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
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
ASKER
Hi Stefor,
I have an error:
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
$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!"
}
}
Try this, now with added "Write-Host"
ASKER
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.
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
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"}
}
ASKER
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++
}
The attribute "mail" doesn't contain all proxy addresses on an object.
ASKER
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
ASKER
stefor?
Try this one.
Change the variable to include your OU.
$searchresult.SearchRoot=" LDAP://OU= MailObject s,DC=domai n,DC=local "
Change the variable to include your OU.
$searchresult.SearchRoot="
$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"}
}
ASKER
ensermo
you script dont work:
[PS] C:\temp\PLIP>.\check.ps1
FALSE: not found in AD
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
...
...
..
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
...
...
..
ASKER
enserno, its work very good,
can you add to this script the export of result to a file?
thanks
can you add to this script the export of result to a file?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks work :)
Open in new window