forked from gangstanthony/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpand-OU.ps1
More file actions
24 lines (19 loc) · 733 Bytes
/
Expand-OU.ps1
File metadata and controls
24 lines (19 loc) · 733 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# usage:
# Expand-OU domain.com/company/sales/users
#
# output:
# OU=users,OU=sales,OU=company,DC=domain,DC=com
function Expand-OU ($searchroot) {
$searchrootarray = $searchroot.split('/') | ? {$_ -and $_ -notmatch '^(?:\s+)?$'}
$dn = ([adsi]"LDAP://$($searchrootarray[0])").distinguishedname.ToString()
$searchrootarray = $searchrootarray | select -Skip 1
foreach ($obj in $searchrootarray) {
$query = Get-ADObject -Filter * -SearchBase $dn -SearchScope OneLevel | select name, distinguishedname
if ($obj -in $query.name) {
$dn = ($query | ? name -eq $obj).distinguishedname
} else {
throw "could not find '$obj' in '$dn'"
}
}
Write-Output $dn
}