Description
More and more Powershell scripts are using LDAP to locate items within Active Directory, but finding out what that full FQDN (fully qualified domain name) is can be a pain. This script aims to make finding the FQDN for Organizational Units in your Active Directory easier by presenting you with a simple list of all the OU’s and letting you select which one you want. The resulting FQDN is then copied into the clipboard for whatever you need it for.
I recommend adding this Function into your $Profile so you can call it from any Powershell command prompt. To read more about the Powershell $Profile go here:
This function does require the Remote Server Administration Tools to be installed on your workstation.
For additional help, after the function is in your $Profile:
Get-Help Get-OULDaP -Full
Source Code
Function Get-OULDAP {
<#
.SYNOPSIS
Find out the full LDAP FQDN for an OU in your Active Directory and copy it into your clipboard.
.DESCRIPTION
More and more Powershell scripts are using LDAP to locate items within Active Directory, but
finding out what that full FQDN is can be a pain. This script aims to make finding the full
FQDN for Organizational Units in your Active Directory easier by presenting you with a simple
list of all the OU's and letting you select which one you want. The resulting FQDN is then
copied into the clipboard for whatever you need it for.
I recommend adding this Function into your $Profile so you can call it from any Powershell
command prompt. To read more about the Powershell $Profile go here:
http://technet.microsoft.com/en-us/library/ee692764.aspx
.PARAMETER Search
Enter a string value and Get-OULDAP will only present you with a list of OU's that have
that string value in them. The search is NOT case sensitive.
.EXAMPLE
Get-OULDAP
Will present a listing of all of your OU's which you can select from. Enter the
cooresponding number and the FQDN will be copied to your clipboard.
.EXAMPLE
Get-OULDAP -Search Computers
Will present a listing of all of your OU's that have the word "computers" in them.
.LINK
http://technet.microsoft.com/en-us/library/ee692764.aspx
#>
Param (
[string]$Search
)
Import-Module ActiveDirectory -ErrorAction SilentlyContinue
$OUs = Get-ADOrganizationalUnit -filter * | where { $_.distinguishedname -like "*$Search*" } | Sort Name
$MenuItem = 0
cls
Write-Host "Select the OU you want and the LDAP value will be copied to the clipboard.`n"
ForEach ($OU in $OUs)
{ $MenuItem ++
$MenuText = ($OU | Select Name,DistinguishedName | Format-Table -HideTableHeaders | Out-String).Trim()
If ($MenuItem -lt 10)
{ [string]$Select = " $MenuItem"
}
Else
{ [string]$Select = $MenuItem
}
Write-Host "$Select. $MenuText"
}
$Prompt = Read-Host -Prompt "`n`nEnter number of the OU you want"
If (-not $Prompt)
{ Break
}
Try
{ $Prompt = [int]$Prompt
}
Catch
{ Write-Host "`nSorry, invalid entry. Try again!"
Break
}
If ($Prompt -lt 1 -or $Prompt -gt $MenuItem)
{ Write-Host "`nSorry, invalid entry. Try again!"
}
Else
{ Write-Host "`n`n$($OUs[$Prompt - 1].distinguishedName) copied to clipboard"
$($OUs[$Prompt - 1].distinguishedName) | Clip
}
}
2 Spice ups
martin9700
(Martin9700)
2
Minor update: forgot to add a check that the input by the user is in the valid range. So it was fine with you putting in “my doggy is sad” but if you have 15 OU’s and you put in 23 it would have failed. Now it checks for that and exits if it gets an invalid entry.
Neat little function, added it to my profile! If you add this into your $profile, be sure to remove the last line “Get-OULDAP” or it’ll run the command every time you open powershell.
Thanks Brandon, removed that line as it shouldn’t have been there at all (was there for testing!)
Could one of you guys help me? I’m trying to run the script from $profile but all i’m getting is 1. for OU… then it errors and I’m getting a “Cannot index into a null array.” error.
Do you have Remote Server Admin Tools installed? What do you get run Get-AdOrganizationUnit?
installed RSAT…got errors “Unable to find a default server with Active Directory Web Services running.” and followed Microsoft KB’s to install “Active Directory Management Gateway Service” on my two Server 2003 DC’s (main DC is Server 2008R2 and it’s installed there) and nothing happens when i run .\Get-OUldap.ps1 and when I run the Get-AdOrganizationUnit I get the same messages as above…
With the 2008R2 DC in the mix I wouldn’t think you would have any problem and wouldn’t need to install the Gateway Service at all. Unfortunately I don’t know what the fix would be! Good luck and let us know how you fix it! Post in the Powershell forum (bigger audience then anyone reading these comments!)
newwave
(newwave)
9
Such a small thing to need, nice easy way to do it!! 