Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Help for New-Mailcontact and Set-Contact

$
0
0

Hi,

Objective for the script: 

I have been trying to write a script so I can create New-Mailcontact using CSV file, if not already exist in ActiveDirectory.

If already exist in Active Directory then skip New-MailContact command and  just run Set-Contact.

Problem in the script: -

It does create  new Mailcontact if not already exist in AD.

1) If MailContact exist in the AD it still tries to create that MailContact and then it errors it out since AD would not let it create because it is not unique and then it moves to next entry and does the same until it finds the entry which is new then obviously AD let it creates it.

2) It create a new Mailcontact but it never goes to Set-Contact.

In summary 2 problems

1) It does not skip the entry if that already exist in AD.

2) It never goes to Set-Contact.

===================

$CSV = Import-CSV F:\contacts\contacts1.csv
$Abc =  Get-MailContact -OrganizationalUnit "OU=company_Contacts,OU=Contacts,DC=test,DC=com" $_.alias | Select alias

foreach ($entry in $Csv) {

if ($entry.mailnickname -ne $abc.alias)
{
    New-MailContact -Name $entry.DisplayName -DisplayName $entry.DisplayName -FirstName $entry.givenName -LastName $entry.sn -OrganizationalUnit "OU=company_Contacts,OU=Contacts,DC=test,DC=com" -ExternalEmailAddress $entry.mail -Alias $entry.mailNickname
 }
Else
{ set-contact -identity $entry.mailNickname -Phone $entry.telephoneNumber -MobilePhone $entry.Mobile -Office $entry.physicalDeliveryOfficeName -Title $entry.Title -Department $entry.Department
}
}

========================

Where am I going wrong?



Raman


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>