Skip to content

Instantly share code, notes, and snippets.

@VertigoRay
Last active December 20, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VertigoRay/6091801 to your computer and use it in GitHub Desktop.
Save VertigoRay/6091801 to your computer and use it in GitHub Desktop.
In my case, I only work within a particular OU in our Domain, so I’ve made it so my $Path can be abbreviated. Forked (https://gist.github.com/VertigoRay/6091753) ... here’s how I handle things:
[string] $RootOU = 'OU=test,DC=domain,DC=com'
[string] $Path = 'OU=foo'
try {
$ou_exists = [adsi]::Exists("LDAP://$Path")
} catch {
# If invalid format, error is thrown.
Write-Debug "Supplied Path is invalid.`n$_"
# It's probably the abbreviated version, so let's tack on the Root OU and confirm exists.
Write-Debug 'Placing Path in Root OU and re-verifying ...'
$Path = "$Path,$RootOU"
try {
$ou_exists = [adsi]::Exists("LDAP://$Path")
} catch {
Throw("Supplied Path is not valid, nor is our attempt to place it in the Root OU:`n$Path")
}
}
if (-not $ou_exists) {
Throw("Supplied Path does not exist:`n$Path")
} else {
Write-Debug "Path Exists (1): $Path"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment