Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How to stop the rename the cn again if it had meet the condition

$
0
0

How to stop to rename the CN in Active directory if running the script.

This is my script:

$objSearch = New-Object DirectoryServices.DirectorySearcher
$dateNow = Get-Date
$DomainDNS = "server2008.server1.com" #server 2008
$ADpath = [ADSI]"LDAP://$DomainDNS/ou=power shell test,dc=server1,dc=com"
$objSearch.Filter = '(&(objectClass=user)(objectCategory=person)(!samaccountname=user1))'
$objSearch.SearchRoot = $ADpath
$objSearch.PageSize = 1000
$objSearch.SearchScope = "subtree" 
$objResults = $objSearch.Findall()

write-host 'objResults: ' $objResults

foreach($objResult in $objResults){  

$objuser = $objResult.GetDirectoryEntry()
$name = $objuser.cn
write-host "name:" $name

if (($objuser.cn -notcontains "DA$name")){
    $changeName = "DA" + $name
    write-host 'CHANGE NAME: ' $changeName
    $objuser.rename("cn=$changeName")
    $objuser.CommitChanges()
  }

  else {
    continue
  }

}


The result I get is :

what I want the result is:


Viewing all articles
Browse latest Browse all 15028

Trending Articles



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