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: