# # # function Write-LogFile([string]$logFileName) { Process { $_ $dt = Get-Date $str = $dt.DateTime + " " + $_ $str | Out-File -FilePath $logFileName -Append } } # , # ArrayList, # , , function Compare-ArrayLists([System.Collections.ArrayList] $ListA, [System.Collections.ArrayList] $ListB) { if ($ListA.Count -ne $ListB.Count) { return $false } else { $CompListA = New-Object System.Collections.ArrayList($null) $CompListB = New-Object System.Collections.ArrayList($null) for ($i=0;$i -lt $ListA.Count;$i++) { if ($ListA[$i].GetType() -ne [String]) { $rc = $CompListA.Add([System.BitConverter]::ToString($ListA[$i])) } else { $rc = $CompListA.Add($ListA[$i]) } if ($ListB[$i].GetType() -ne [String]) { $rc = $CompListB.Add([System.BitConverter]::ToString($ListB[$i])) } else { $rc = $CompListB.Add($ListB[$i]) } } for ($i=0;$i -lt $CompListA.Count;$i++) { if ($CompListB.IndexOf($CompListA[$i]) -lt 0) {return $false} } return $true } } # , (), (). : # # # , , Exchange # # , : function Load-FromDomain([string] $DomainName, [string] $UnitName, [bool]$flagExchangeDomain, [ref]$A_Entries, [bool]$flagContacts) { if (!$flagContacts) { if (!$flagExchangeDomain) { # LDAP, , # . !userAccountControl:1.2.840.113556.1.4.803:=2, # , # , (, AD ) $strFilter = "(&(objectClass=user)(!objectClass=computer)(mail=*)(company=*))"#(!userAccountControl:1.2.840.113556.1.4.803:=2) } else { # Exchange- msExchHideFromAddressLists, $strFilter = "(&(objectClass=user)(!objectClass=computer)(mail=*)(company=*)(!msExchHideFromAddressLists=TRUE))" #(!userAccountControl:1.2.840.113556.1.4.803:=2) } } else { $strFilter = "(&(objectClass=contact))" } $objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://"+$DomainName+"/"+$UnitName) $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = "Subtree" # , , , # - . $colProplist = "employeeID", "employeeType", "objectGUID", "CN", "givenName", "name", "sn", "legacyExchangeDN", "displayName", "mail", "wWWHomePage", "l", "postalCode", "initials", "physicalDeliveryOfficeName", "st", "streetAddress", "ipPhone", "title", "mobile", "department", "pager", "homePhone", "facsimileTelephoneNumber", "userAccountControl", "distinguishedName", "company", "Description", "otherTelephone", "telephoneNumber", "userCertificate" if ($flagExchangeDomain) { $colProplist += "mailNickname" $colProplist += "msExchHideFromAddressLists" } if ($flagContacts) { $colProplist += "info" } foreach ($i in $colPropList) { $rc = $objSearcher.PropertiesToLoad.Add($i) } $colResults = $objSearcher.FindAll() $colResults.Count foreach ($objResult in $colResults) { $objItem = $objResult.Properties # , , , # , . $Entry = New-Object -TypeName System.Object if (!$flagContacts) { $Entry | Add-Member -type NoteProperty -name "GUID" -Value ([System.BitConverter]::ToString($objItem.objectguid[0])).Replace('-','') } else { $Entry | Add-Member -type NoteProperty -name "GUID" -Value ([string]$objItem.info) } # , , .. . # , , - , , # , $UserProperties = $colProplist | Where-Object {($_ -ne "objectGUID") -and ($_ -ne "userCertificate") -and ($_ -ne "otherTelephone")} foreach ($UserProperty in $UserProperties) { if ($objItem.Item($UserProperty) -ne $null) { $Entry | Add-Member -type NoteProperty -name $UserProperty -Value ([string]$objItem.Item($UserProperty)) } } # , if ($objItem.usercertificate -ne $null) { $Certificates = New-Object System.Collections.ArrayList($null) foreach ($Certificate in $objItem.usercertificate) { $rc = $Certificates.Add($Certificate) } $Entry | Add-Member -type NoteProperty -name "userCertificate" -Value ($Certificates) } if ($objItem.othertelephone -ne $null) { $Telephones = New-Object System.Collections.ArrayList($null) foreach ($Telephone in $objItem.othertelephone) { $rc = $Telephones.Add($Telephone) } $Entry | Add-Member -type NoteProperty -name "otherTelephone" -Value ($Telephones) } $A_Entries.Value += $Entry } } # , . # , : # # # # # $A_Users = $A_Contacts = $A_NewContacts = $A_ChangedContacts = $A_ContactsToDelete = @() $LogFileName = "./GetUserContacts.log" $flagExchangeOrganization = $false # # : # ("< >","<>") ("< >","< >","< exchange>") ("< 1>","< >","< Exchange>") ("< 2>"... # # ("admin@litware.inc","password") ("litware.inc","ou=contacts,dc=litware,dc=inc",$true) ("contoso.com","dc=contoso,dc=com",$false) if ($args.Count -lt 3) { break } $UserName = $args[0][0] $Password = $args[0][1] $UserName | Write-LogFile $LogFileName $Password | Write-LogFile $LogFileName $Domain = $args[1][0] $ContactsOU = $args[1][1] $flagExchangeOrganization = $args[1][2] $Domain | Write-LogFile $LogFileName $ContactsOU | Write-LogFile $LogFileName $flagExchangeOrganization | Write-LogFile $LogFileName Load-FromDomain $Domain $ContactsOU $flagExchangeOrganization ([ref]$A_Contacts) $true for ($i=2;$i -lt $args.Count;$i++) { $SrcDomain = $args[$i][0] $SrcOU = $args[$i][1] $SrcExchangeFlag = $args[$i][2] $SrcDomain | Write-LogFile $LogFileName $SrcOU | Write-LogFile $LogFileName $SrcExchangeFlag | Write-LogFile $LogFileName Load-FromDomain $SrcDomain $SrcOU $SrcExchangeFlag ([ref]$A_Users) $false } # , , # , , # . , # - , # . foreach ($User in $A_Users) { $Contact = $A_Contacts | Where-Object {$_.GUID -eq $User.GUID} if ($Contact -eq $null) { $A_NewContacts += $User $A_NewContacts[$A_NewContacts.Length-1].distinguishedName = "" } else { $flagContactAdded = $false $UserProperties = ($User | Get-member -MemberType NoteProperty | Where-Object {($_.Name -ne "distinguishedName") -and ($_.Name -ne "mailNickname")}) foreach ($UserProperty in $UserProperties) { if ($Contact.($UserProperty.Name) -ne $null) { if ($User.($UserProperty.Name).GetType() -ne [System.Collections.ArrayList]) { if ($User.($UserProperty.Name) -ne $Contact.($UserProperty.Name)) { if (!$flagContactAdded) { $NewEntry = New-Object -TypeName System.Object $NewEntry | Add-Member -type NoteProperty -name "distinguishedName" -Value ($Contact.distinguishedname) $flagContactAdded = $true } $NewEntry | Add-Member -type NoteProperty -name ($UserProperty.Name) -Value $User.($UserProperty.Name) } } else { if (!(Compare-ArrayLists $User.($UserProperty.Name) $Contact.($UserProperty.Name))) { if (!$flagContactAdded) { $NewEntry = New-Object -TypeName System.Object $NewEntry | Add-Member -type NoteProperty -name "distinguishedName" -Value ($Contact.distinguishedname) $flagContactAdded = $true } $NewEntry | Add-Member -type NoteProperty -name ($UserProperty.Name) -Value $User.($UserProperty.Name) } } } } if ($flagContactAdded) { $A_ChangedContacts += $NewEntry } } } # , foreach ($Contact in $A_Contacts) { $User = $A_Users | Where-Object {$_.GUID -eq $Contact.GUID} if ($User -eq $null) { $A_ContactsToDelete += $Contact } } # $A_NewContacts # $A_ChangedContacts # $A_ContactsToDelete # foreach ($Contact in $A_ContactsToDelete) { $ContactsOUDN = "LDAP://" + $Domain + "/" + $ContactsOU $objContactsOU = new-object System.DirectoryServices.DirectoryEntry($ContactsOUDN, $Username, $Password, [System.DirectoryServices.AuthenticationTypes]::Secure) $objContactsOU.Delete("contact", $Contact.distinguishedName.Split(",")[0]) "" + $Contact.name | Write-LogFile $LogFileName } # foreach ($Contact in $A_NewContacts) { $ContactsOUDN = "LDAP://" + $Domain + "/" + $ContactsOU $objContactsOU = new-object System.DirectoryServices.DirectoryEntry($ContactsOUDN, $Username, $Password, [System.DirectoryServices.AuthenticationTypes]::Secure) $NewContact = $objContactsOU.Children.Add("CN="+$Contact.CN,"contact") $NewContactProperties = ($Contact | Get-member -MemberType NoteProperty | Where-Object {($_.Name -ne "distinguishedName") ` -and ($_.Name -ne "GUID") -and ($_.Name -ne "CN") -and ($_.Name -ne "otherTelephone")` -and ($_.Name -ne "name") -and ($_.Name -ne "userAccountControl") -and ($_.Name -ne "userCertificate")}) if ($NewContactProperties -ne $null) { foreach ($NewContactProperty in $NewContactProperties) { $NewContact.Put($NewContactProperty.Name,$Contact.($NewContactProperty.Name)) } } if ($Contact.mail -ne $null) { if ($flagExchangeOrganization) { $NewContact.Put("targetAddress", "SMTP:" + $Contact.mail) $NewContact.Put("mailNickname", $Contact.mail.Split("@")[0]) $NewContact.Put("msExchPoliciesExcluded", "{26491CFC-9E50-4857-861B-0CB8DF22B5D7}") } $NewContact.Put("proxyAddresses", "SMTP:" + $Contact.mail) } if ($Contact.userCertificate -ne $null) { $NewContact.PutEx(2, "userCertificate", [Array]$Contact.userCertificate) } if ($Contact.otherTelephone -ne $null) { $NewContact.PutEx(2, "otherTelephone", [Array]$Contact.otherTelephone) } $NewContact.Put("info",$Contact.GUID) $NewContact.SetInfo() " " + $Contact.name | Write-LogFile $LogFileName } # , , , . # , # . , CN (canonical name) # , Rename() foreach ($Contact in $A_ChangedContacts) { $ContactDN = "LDAP://" + $Domain + "/" + $Contact.distinguishedName $ChangedContact = new-object System.DirectoryServices.DirectoryEntry($ContactDN, $Username, $Password, [System.DirectoryServices.AuthenticationTypes]::Secure) $ChangedContactProperties = ($Contact | Get-member -MemberType NoteProperty | Where-Object {($_.Name -ne "distinguishedName") ` -and ($_.Name -ne "GUID") -and ($_.Name -ne "CN") -and ($_.Name -ne "otherTelephone") ` -and ($_.Name -ne "name") -and ($_.Name -ne "userAccountControl") -and ($_.Name -ne "userCertificate")}) if ($ChangedContactProperties -ne $null) { foreach ($ChangedContactProperty in $ChangedContactProperties) { $ChangedContact.Put($ChangedContactProperty.Name,$Contact.($ChangedContactProperty.Name)) " " + $ChangedContactProperty.Name + " " + $Contact.distinguishedName | Write-LogFile $LogFileName } } if ($Contact.userCertificate -ne $null) { $ChangedContact.PutEx(1, "userCertificate", 0) $ChangedContact.PutEx(2, "userCertificate", [Array]$Contact.userCertificate) " userCertificate " + $Contact.distinguishedName | Write-LogFile $LogFileName } if ($Contact.otherTelephone -ne $null) { $ChangedContact.PutEx(1, "otherTelephone", 0) $ChangedContact.PutEx(2, "otherTelephone", [Array]$Contact.otherTelephone) " otherTelephone " + $Contact.distinguishedName | Write-LogFile $LogFileName } if ($Contact.CN -ne $null) { " " + $ChangedContact.distinguishedName + " " + $Contact.CN | Write-LogFile $LogFileName $ChangedContact.Rename("CN="+$Contact.CN) } $ChangedContact.SetInfo() }
Source: https://habr.com/ru/post/136773/
All Articles