Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check if domain exists

746 views
Skip to first unread message

Paul B. (USAF)

unread,
Oct 17, 2009, 2:43:01 PM10/17/09
to
I want to use the below code (or something similar) to check if a particular
AD Domain exists:

###Code Example###
function verify_domain
{
$erroractionpreference = "SilentlyContine"

$exist = [ADSI]::Exists("LDAP://" + $dn)

if ($exist -ne 'True')
{
do something
}
}
### CODE BLOCK END###

Works no problem when querying an OU DN path (i.e.
OU=paul,DC=domain,DC=lcl). However, if I just pass a Domain DN (i.e.
DC=domain,DC=lcl), the i get what i think is a referral error however but
shouldn't the $erroractionpreference = "silentlycontinue" prevent that. The
actual error I get is:

Exception calling "Exists" with "1" argument(s): "A referral was returned
from the server"

My DC DNS is configured to forward "All other domains" to another DNS server
so I can kinda understand the response. So...how do I account for this
response and give a more "friendly" message and script exit. If necessary to
help, I can provide the entire code. Thanks in advance.


Paul B. (USAF)

unread,
Oct 17, 2009, 10:21:01 PM10/17/09
to
Nevermind...I figured it out. So to share with all who may search this board
with the same problem, I'll post the "working" code. Since the referral was
an exception AND NOT an error, I had to add a TRAP statement.

###WORKING CODE EXAMPLE###


function verify_domain
{
$erroractionpreference = "SilentlyContine"

> TRAP
> {
> CONTINUE


> }

$exist = [ADSI]::Exists("LDAP://" + $dn)

if ($exist -ne 'True')
{
do something
}
}

### WORKING CODE BLOCK END###

Bob Landau

unread,
Oct 18, 2009, 11:28:01 AM10/18/09
to
This is how had to handle a block of code using WMI where the particular
property didn't exist.

btw While it doesn't make a difference in your code snippet. You should be
setting back to continue before exiting. If this were ever dot-sourced you'd
be eliminating any warnings

$erroractionpreference="Continue"

Larry__Weiss

unread,
Oct 18, 2009, 12:52:00 PM10/18/09
to
Point taken about the possibility of the code, if dot-sourced, persisting a
setting as an unwanted side-effect, but shouldn't the better technique be to
save off the current setting and then reset it to its saved value, instead of
making an assumption about what it was set to?

- Larry

0 new messages