Merhaba,
Bazı durumlarda teknik olarak olmasada organik olarak bağlılıklarımızın bulunduğu firmalar, kuruluşlar oluyor ve yapımızda bu kuruluşların çalışanlarına Skype,Outlook contact’larından erişebilme aradağında bulabilme ihtiyacı doğuyor. Tam bu aşamada ilgili kuruluşlardan gerekli bilgileri csv formatında talep edip yazdığım bu scripti kullanabilirsiniz.
Script Ne Yapar?
Elde ettiğiniz csv formatındaki dosyayı satır satır okur ve her satırda şu kontrolleri yapar;
Import-Module ActiveDirectory Function Compare-ObjectProperties { Param( [PSObject]$ReferenceObject, [PSObject]$DifferenceObject ) $objprops = "givenName","sn","mail" #Will check user properties $diffs = @() $Date = Get-Date foreach ($objprop in $objprops) { $diff = Compare-Object $ReferenceObject $DifferenceObject -Property $objprop # Compare two object and return differencies. if ($diff) { $diffprops = @{ PropertyName=$objprop RefValue=($diff | ? {$_.SideIndicator -eq '<='} | % $($objprop)) DiffValue=($diff | ? {$_.SideIndicator -eq '=>'} | % $($objprop)) } $DN= $ReferenceObject | select DistinguishedName $GivenName = $DifferenceObject | select givenName $sn = $DifferenceObject | select sn $proxyAddresses = $DifferenceObject| select sipAddress $DisplayName = $GivenName.GivenName+" "+$sn.sn Set-ADObject -Identity $DN -Replace @{$objprop = $diffprops.DiffValue.ToString();'DisplayName'=$DisplayName;'proxyAddresses'="sip:"+$proxyAddresses.sipAddress;'msRTCSIP-PrimaryUserAddress'="sip:"+$proxyAddresses.sipAddress} # Set user properties as a result of compare. $DisplayName + " USER ARRANGED " + $Date >> C:\Util\editedcontacts.txt # Log changes. Write-Host -ForegroundColor Yellow $DisplayName " ARRANGED" $diffs += New-Object PSObject -Property $diffprops # Output information optional. } } #if ($diffs) {return ($diffs | Select PropertyName,RefValue,DiffValue)} } function Contact-Import { param( [Parameter(Mandatory=$true, Position=0)] [string[]]$path, [Parameter(Mandatory=$true, Position=1)] $ouDN ) $users = Import-Csv $path -Encoding ASCII foreach($user in $users) { if(!($user.sipAddress -cmatch '@xxx.com.tr' -or $user.sipAddress -eq $null)) { try { New-ADObject -Name $user.uid -Type contact -OtherAttributes @{'givenName'=$user.givenName;'sn'=$user.sn;'mail'=$user.mail;'DisplayName'=$user.givenName+" "+$user.sn;'msExchExtensionCustomAttribute1'=$user.uid;'proxyAddresses'='sip:'+$user.sipAddress;'msRTCSIP-PrimaryUserAddress'='sip:'+$user.sipAddress} -Path $ouDN -ErrorAction Stop Write-Host -ForegroundColor Green $user.givenName $user.sn "CREATED" #if($user.sipAddress -ne "") # { # $uname = $user.uid # $currentuser = Get-ADObject -Filter {(objectClass -eq "contact") -and (name -eq $uname)} -Properties * -SearchBase "OU=OtherRecipients,DC=ttd,DC=local" #$DN = $currentuser | select DistinguishedName # Set-ADObject -Identity $DN -Replace @{'proxyAddresses'='sip:'+$user.sipAddress} # } } catch { $errormessage = $_.Exception.Message if($errormessage -contains 'An attempt was made to add an object to the directory with a name that is already in use') { $uname = $user.uid $currentuser = Get-ADObject -Filter {(objectClass -eq "contact") -and (name -eq $uname)} -Properties * -SearchBase $ouDN Compare-ObjectProperties $currentuser $user }else{ Write-Host $errormessage } } } } } function Contact-Delete { param( [Parameter(Mandatory=$true, Position=0)] [string[]]$path, [Parameter(Mandatory=$true, Position=1)] $ouDN ) $users = Import-Csv $path -Encoding ASCII $currentusers = Get-ADObject -Filter 'objectClass -eq "contact"' -Properties * -SearchBase $ouDN $Date = Get-Date foreach($cuser in $currentusers) { $a = $users | Where-Object {$_.uid -eq $cuser.name} if(!$a) { $DN = $cuser.DistinguishedName Remove-ADObject -Identity $DN -Confirm:$false -ErrorAction SilentlyContinue $cuser.givenName.toString() + " USER DELETED " + $date >> C:\Util\editedcontacts.txt Write-Host -ForegroundColor Red $cuser.DisplayName.toString() " DELETED" } } } Contact-Import -path C:\contacts.csv -ouDN "OU=OtherRecipients,DC=ttd,DC=local" # Create contacts in CSV file which are have Sip address and does not have xxx.com.tr Contact-Delete -path C:\contacts.csv -ouDN "OU=OtherRecipients,DC=ttd,DC=local" # Delete contacts from AD, if does not exist in related CSV file.