how to get the master OU for every user


hello all,

i working on script add master ou name users extensionattribute8 failed this.

e.g. have these users distinguishedname

1- cn=user1,ou=10289,ou=students,dc=mydomain,dc=com

2- cn=user2,ou=10335,ou=staff,dc=mydomain,dc=com

3- cn=user3,ou=teachers,dc=mydomain,dc=com

so how can have ou after dc=mydomain,dc=com (from right)  whatever user exist on child ou

for example need first user1 add student word value extensionattribute8

and for  user2 add staff word value extensionattribute8

and for  user3 add teachers word value extensionattribute8

thanks lot in advance

let's go through script.

$newusers = get-qaduser -createdafter (get-date).adddays(-3)  -sizelimit 0 -includedproperties comment,samaccountname,networkaddress,parentcontainer  | where {(($_.userprincipalname -like "*@mydomain.local") -and ($_.mail -notlike "*@mydomain.co")) -and ($_.parentcontainer -notmatch "parents|service accounts|hei")} 

this line gives list of users match criteria. these users objects not strings nor ints nor whatever simple type is

these objects, have list of specific properties, specified in -includedproperties parameter

now try command

$newusers | get-member

you'll see list of properties , methods user objects contain. 1 of them should called distinguishedname (if it's not there, add list of includedproperties)

now this

foreach ($user in $newusers){

$user

}

you'll see object , of properties.

now

foreach ($user in $newusers){

$user.distinguishedname

}

now you'll see list of distinguished names of users , proper value of -split should get

so code should this:

foreach ($user in $newusers){ $do = "@mydomain.com" $ouname = $user.distinguishedname -split 'ou=' | select -last 1 | % {$_ -split ','} | select -first 1

....



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...