powershell - Script create contacts in Active Directory does not work -
i running script in test environment, works run script in production environment, not work
when starting on domain controller, under control of windows server 2008 r2 standard or enterprise.
you must provide value expression on right-hand side of '-' operator. @ c:\scripts\galsync.ps1:89 char:14 + $targetcred - <<<< properties targetaddress + categoryinfo : parsererror: (:) [], parseexception + fullyqualifiederrorid : expectedvalueexpression
89 str $targetcred -properties targetaddress
### --- galsync.ps1 --- # # written carol wapshere # # manages contacts in 2 domains based on mail-enabled users in other domain. # - contacts created new users. # - contacts deleted if source user no longer meets filter requirements. # - contacts updated changed information. # # notes: # - requires rsat roles , features installed. ref http://blogs.technet.com/heyscriptingguy/archive/2010/01/25/hey-scripting- guy-january-25-2010.aspx # - attribute deletions not replicated - attribute adds , changes. # - user account needed in each domain permission create contacts. # - passwords these user accounts must stored in secure files using command: # read-host -assecurestring | convertfrom-securestring | out-file c:\scripts\filename.txt # ### --- global definitions --- $domain_1 = "192.168.50.11" $domain_2 = "192.168.11.10" $ou_contacts_1 = "ou=contact,ou=selta,dc=rvsco,dc=ru" $ou_contacts_2 = "ou=contact,ou=rvsco,dc=selta,dc=ru" $user_1 = "rvsadmin@rvsco.ru" $user_2 = "seltaroot@selta.ru" $pwfile_1 = "c:\scripts\rvsco.txt" $pwfile_2 = "c:\scripts\selta.txt" ## following list of attributes copied user contact $arrattribs = 'displayname','company','givenname','mobile','postaladdress','postalcode','sn','st','streetaddress','telephonenumber','title' ,'mail','c','co','l','facsimiletelephonenumber','physicaldeliveryofficename' ## following filter used get-adobject decide users have contacts. $strselectusers = 'objectclass -eq "user" -and homemdb -like "*" -and -not useraccountcontrol -bor 2 -and -not msexchhidefromaddresslists -eq $true -and -not displayname -eq "administrator"' ### --- function add, delete , modify contacts in target domain based on source users --- function synccontacts { param($sourcedc, $sourceuser, $sourcepwfile, $targetdc, $targetuser, $targetpwfile, $targetou) end { $colusers = @() $colcontacts = @() $coladdcontact = @() $coldelcontact = @() $colupdcontact = @() $arrusermail = @() $arrcontactmail = @() write-host "enumerating..." ### enumerate users $password = get-content $sourcepwfile | convertto-securestring $sourcecred = new-object -typename system.management.automation.pscredential -argumentlist $sourceuser,$password $colusers = get-adobject -filter $strselectusers -properties * -server $sourcedc -credential $sourcecred if ($colusers.count -eq 0) { write-host "no users found in source domain!" break } foreach ($user in $colusers) { $arrusermail += $user.mail } ### enumerate contacts $password = get-content $targetpwfile | convertto-securestring $targetcred = new-object -typename system.management.automation.pscredential -argumentlist $targetuser,$password $colcontacts = get-adobject -filter 'objectclass -eq "contact"' -searchbase $targetou -server $targetdc -credential $targetcred -properties targetaddress foreach ($contact in $colcontacts) { $straddress = $contact.targetaddress -replace "smtp:","" $arrcontactmail += $straddress } ### find contacts add , update foreach ($user in $colusers) { if ($arrcontactmail -contains $user.mail) { write-host "contact found " $user.mail $colupdcontact += $user } else { write-host "no contact found " $user.mail $coladdcontact += $user } } ### find contacts delete foreach ($address in $arrcontactmail) { if ($arrusermail -notcontains $address) { $coldelcontact += $address write-host "contact deleted " $address } } write-host "" write-host "updating ...." ### adds foreach ($user in $coladdcontact) { write-host "adding contact " $user.mail $targetaddress = "smtp:" + $user.mail $alias = "c-" + $user.mail.split("@")[0] $hashattribs = @{'targetaddress' = $targetaddress} $hashattribs.add("mailnickname", $alias) foreach ($attrib in $arrattribs) { if ($user.$attrib -ne $null) { $hashattribs.add($attrib, $user.$attrib) } } new-adobject -name $user.displayname -type contact -path $targetou -description $user.description -server $targetdc -credential $targetcred -otherattributes $hashattribs } ### updates foreach ($user in $colupdcontact) { write-host "verifying contact " $user.mail $strfilter = "targetaddress -eq ""smtp:" + $user.mail + """" $colcontacts = get-adobject -filter $strfilter -searchbase $targetou -server $targetdc -credential $targetcred - properties * foreach ($contact in $colcontacts) { $hashattribs = @{} foreach ($attrib in $arrattribs) { if ($user.$attrib -ne $null -and $user.$attrib -ne $contact.$attrib) { write-host " changing " $attrib write-host " before: " $contact.$attrib write-host " after: " $user.$attrib $hashattribs.add($attrib, $user.$attrib) } } if ($hashattribs.count -gt 0) { set-adobject -identity $contact -server $targetdc -credential $targetcred -replace $hashattribs } } } ### deletes foreach ($contact in $coldelcontact) { write-host "deleting contact " $contact $strfilter = "targetaddress -eq ""smtp:" + $contact + """" get-adobject -filter $strfilter -searchbase $targetou -server $targetdc -credential $targetcred | remove-adobject - server $targetdc -credential $targetcred -confirm:$false } } } ### --- main --- start-transcript galsync.log if(@(get-module | where-object {$_.name -eq "activedirectory"} ).count -eq 0) {import-module activedirectory} write-host "domain1 users --> domain2 contacts" synccontacts -sourcedc $domain_1 -sourceuser $user_1 -sourcepwfile $pwfile_1 -targetdc $domain_2 -targetuser $user_2 -targetpwfile $pwfile_2 -targetou $ou_contacts_2 write-host "" write-host "domain2 users --> domain1 contacts" synccontacts -sourcedc $domain_2 -sourceuser $user_2 -sourcepwfile $pwfile_2 -targetdc $domain_1 -targetuser $user_1 -targetpwfile $pwfile_1 -targetou $ou_contacts_1 stop-transcript
there line breaks in middle of statements causing trouble. need concatenate lines.
line 87 , 89 should on 1 line only. this:
$colcontacts = get-adobject -filter 'objectclass -eq "contact"' -searchbase $targetou -server $targetdc -credential $targetcred -properties targetaddress
the same line 143 , 145. should belong together.
new-adobject -name $user.displayname -type contact -path $targetou -description $user.description -server $targetdc -credential $targetcred -otherattributes $hashattribs
likewise line 155 , 157 should be
$colcontacts = get-adobject -filter $strfilter -searchbase $targetou -server $targetdc -credential $targetcred -properties *
and line 185 , 187
get-adobject -filter $strfilter -searchbase $targetou -server $targetdc -credential $targetcred | remove-adobject -server $targetdc -credential $targetcred -confirm:$false
line 200 , 202
synccontacts -sourcedc $domain_1 -sourceuser $user_1 -sourcepwfile $pwfile_1 -targetdc $domain_2 -targetuser $user_2 -targetpwfile $pwfile_2 -targetou $ou_contacts_2
line 206 , 208
synccontacts -sourcedc $domain_2 -sourceuser $user_2 -sourcepwfile $pwfile_2 -targetdc $domain_1 -targetuser $user_1 -targetpwfile $pwfile_1 -targetou $ou_contacts_1
there might more need through script other invalid linebreaks
best regards fridden
Comments
Post a Comment