Share via


[ADSI]::Exists

Question

Monday, August 13, 2012 12:19 PM

Hi,

I'm running into issues when i create an OU and then check if it exists because of AD replication.

When i create an OU i use the following code and provide the server/dc it should run on since i have more than one DC. But then when i use the [ADSI]::Exists code i can't specify which server and therefore i sometimes get an error that the OU isn't created when it is.
I have even tried to use a loop that runs every second to check if it exists but then it always take 15 seconds before the OU exists and then the scripts take to long to run.

  1. How can i get around this issue? 
  2. Can you specify which DC to run the [ADSI]::Exists against?
  3. Is there another function i can use instead of [ADSI]::Exists to check if the OU exists on the DC i specify?
  4. Is there anyway to tell a PowerShell script to only use the same DC when checking/editing AD information? Or do i have to specify that on all the PowerShell commands i use? (like i do now)
[string]$DC="dc1.test.local"
[string]$Path="dc=test,dc=local"
[string]$Name="someou"New-ADOrganizationalUnit -Path $Path -Name $Name -Server $DCif ([ADSI]::Exists("LDAP://ou=$Name,$Path"))
{
  Write-Host "Created!"
}

All replies (2)

Monday, August 13, 2012 12:28 PM ✅Answered

Use Get-ADOrganisionalUnit as such:

Get-ADOrganizationalUnit $name -Server 'DC'

Grant Ward, a.k.a. Bigteddy


Monday, August 13, 2012 12:50 PM ✅Answered

[adsi]::Exists("LDAP://dc01.net/ou=$name,$path")