Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - How to bind to AD without displaying the Distinguished Name

Reply
 
Old 04-17-2008   #1 (permalink)
John


 
 

How to bind to AD without displaying the Distinguished Name

Requirement:
Bind to Active Directory domain and if successful continue otherwise output
error.

Problem:
The problem here is when I bind to the domain the Distinguished Name is
displayed on the screen. In the script that I am working on I don't want the
DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
"[void]$LDAPDN" but they do not actually make the bind to the domain. Any
help on binding to a domain without having it display the DN would be
appreciated.

Thanks,

Sample Code:
trap
{
Write-Host "Unable to bind to the domain."
Write-Debug "$_.exception.message"
Exit
}
$LDAPDN = [ADSI]("LDAP://$FQDN")
$LDAPDN
Write-Host "Bind to domain $FQDN was successful."


My System SpecsSystem Spec
Old 04-17-2008   #2 (permalink)
Chris


 
 

RE: How to bind to AD without displaying the Distinguished Name

Hi John ,

I'm confused I think . What if you just left the $LDAPDN out . Then you
would not get any output like you want . Right ?

Thanks
Chris


"John" wrote:
Quote:

> Requirement:
> Bind to Active Directory domain and if successful continue otherwise output
> error.
>
> Problem:
> The problem here is when I bind to the domain the Distinguished Name is
> displayed on the screen. In the script that I am working on I don't want the
> DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
> "[void]$LDAPDN" but they do not actually make the bind to the domain. Any
> help on binding to a domain without having it display the DN would be
> appreciated.
>
> Thanks,
>
> Sample Code:
> trap
> {
> Write-Host "Unable to bind to the domain."
> Write-Debug "$_.exception.message"
> Exit
> }
> $LDAPDN = [ADSI]("LDAP://$FQDN")
> $LDAPDN
> Write-Host "Bind to domain $FQDN was successful."
>
My System SpecsSystem Spec
Old 04-17-2008   #3 (permalink)
Marco Shaw [MVP]


 
 

Re: How to bind to AD without displaying the Distinguished Name

Chris wrote:
Quote:

> Hi John ,
>
> I'm confused I think . What if you just left the $LDAPDN out . Then you
> would not get any output like you want . Right ?
I'm with you there...

Is it you just want particular properties to show when you want to look
at the object?

Marco
My System SpecsSystem Spec
Old 04-17-2008   #4 (permalink)
John


 
 

RE: How to bind to AD without displaying the Distinguished Name

The line $LDAPDN = [ADSI]("LDAP://$FQDN")" does not do the actual bind. You
would either have to call the variable $LDAPDN or simply run
[ADSI]("LDAP://$FQDN")". Either way, if it successfully binds to the domain
it displays the distinguished name, but I don't want that information
displayed. I am wondering if there is a way to bind to the domain without
having the distinguished name displayed.

"Chris" wrote:
Quote:

> Hi John ,
>
> I'm confused I think . What if you just left the $LDAPDN out . Then you
> would not get any output like you want . Right ?
>
> Thanks
> Chris
>
>
> "John" wrote:
>
Quote:

> > Requirement:
> > Bind to Active Directory domain and if successful continue otherwise output
> > error.
> >
> > Problem:
> > The problem here is when I bind to the domain the Distinguished Name is
> > displayed on the screen. In the script that I am working on I don't want the
> > DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
> > "[void]$LDAPDN" but they do not actually make the bind to the domain. Any
> > help on binding to a domain without having it display the DN would be
> > appreciated.
> >
> > Thanks,
> >
> > Sample Code:
> > trap
> > {
> > Write-Host "Unable to bind to the domain."
> > Write-Debug "$_.exception.message"
> > Exit
> > }
> > $LDAPDN = [ADSI]("LDAP://$FQDN")
> > $LDAPDN
> > Write-Host "Bind to domain $FQDN was successful."
> >
My System SpecsSystem Spec
Old 04-17-2008   #5 (permalink)
John


 
 

Re: How to bind to AD without displaying the Distinguished Name

I need to determine whether or not I can bind to a domain. The only way I
know how to do this is to actually try to bind to the domain and if it fails
an errorcode will be trapped. When the code below is successfully binds to
the domain, the distinguished name is output to the screen. Unfortunately do
not want the distinguished name output to the screen.

Look at the code below to see if this makes any more sense.

trap
{
Write-Host "Unable to bind to the domain."
Write-Debug "$_.exception.message"
Exit
}
[ADSI]("LDAP://$FQDN")
Write-Host "Bind to domain $FQDN was successful."

Thanks.


"Marco Shaw [MVP]" wrote:
Quote:

> Chris wrote:
Quote:

> > Hi John ,
> >
> > I'm confused I think . What if you just left the $LDAPDN out . Then you
> > would not get any output like you want . Right ?
>
> I'm with you there...
>
> Is it you just want particular properties to show when you want to look
> at the object?
>
> Marco
>
My System SpecsSystem Spec
Old 04-17-2008   #6 (permalink)
Karl Mitschke


 
 

RE: How to bind to AD without displaying the Distinguished Name

Hello John,
Quote:

> The line $LDAPDN = [ADSI]("LDAP://$FQDN")" does not do the actual
> bind. You would either have to call the variable $LDAPDN or simply
> run [ADSI]("LDAP://$FQDN")". Either way, if it successfully binds to
> the domain it displays the distinguished name, but I don't want that
> information displayed. I am wondering if there is a way to bind to
> the domain without having the distinguished name displayed.
>
> "Chris" wrote:
>
Quote:

>> Hi John ,
>>
>> I'm confused I think . What if you just left the $LDAPDN out . Then
>> you would not get any output like you want . Right ?
>>
>> Thanks
>> Chris
>> "John" wrote:
>>
Quote:

>>> Requirement:
>>> Bind to Active Directory domain and if successful continue otherwise
>>> output
>>> error.
>>> Problem:
>>> The problem here is when I bind to the domain the Distinguished Name
>>> is
>>> displayed on the screen. In the script that I am working on I don't
>>> want the
>>> DN to be displayed. I have tried the commands "$LDAPDN | out-null"
>>> and
>>> "[void]$LDAPDN" but they do not actually make the bind to the
>>> domain. Any
>>> help on binding to a domain without having it display the DN would
>>> be
>>> appreciated.
>>> Thanks,
>>>
>>> Sample Code:
>>> trap
>>> {
>>> Write-Host "Unable to bind to the domain."
>>> Write-Debug "$_.exception.message"
>>> Exit
>>> }
>>> $LDAPDN = [ADSI]("LDAP://$FQDN")
>>> $LDAPDN
>>> Write-Host "Bind to domain $FQDN was successful."
John;

I'm confused too - you say "Requirement: Bind to Active Directory domain
and if successful continue otherwise output error.

You have that - your "Write-Host "Bind to domain $FQDN was successful." can
be looked upon as redundant - you won't see the DN if it doesn't bind, so
you KNOW it binds, when it does, no need to bind and then add an additional
message - look at it like ADSI is doing the work for you, and if you don't
like the output, change it like this:

$LDAPDN |fl @{ expression={$_.distinguishedName } ;label="Bind to domain
was successful" }


My System SpecsSystem Spec
Old 04-17-2008   #7 (permalink)
Chris


 
 

RE: How to bind to AD without displaying the Distinguished Name

What I usually do is this :

$domain = "LDAP://dc=foo,dc=com"
$root = new-object System.directoryservices.DirectoryEntry($domain)

At that point if I'm successful I'm bound to the domain .There will be no
output if I'm successful and If I'm not I will get an error .


From there you can run create a searcher object to do whatever .

Does that do anything for ya ?

Chris


"John" wrote:
Quote:

> The line $LDAPDN = [ADSI]("LDAP://$FQDN")" does not do the actual bind. You
> would either have to call the variable $LDAPDN or simply run
> [ADSI]("LDAP://$FQDN")". Either way, if it successfully binds to the domain
> it displays the distinguished name, but I don't want that information
> displayed. I am wondering if there is a way to bind to the domain without
> having the distinguished name displayed.
>
> "Chris" wrote:
>
Quote:

> > Hi John ,
> >
> > I'm confused I think . What if you just left the $LDAPDN out . Then you
> > would not get any output like you want . Right ?
> >
> > Thanks
> > Chris
> >
> >
> > "John" wrote:
> >
Quote:

> > > Requirement:
> > > Bind to Active Directory domain and if successful continue otherwise output
> > > error.
> > >
> > > Problem:
> > > The problem here is when I bind to the domain the Distinguished Name is
> > > displayed on the screen. In the script that I am working on I don't want the
> > > DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
> > > "[void]$LDAPDN" but they do not actually make the bind to the domain. Any
> > > help on binding to a domain without having it display the DN would be
> > > appreciated.
> > >
> > > Thanks,
> > >
> > > Sample Code:
> > > trap
> > > {
> > > Write-Host "Unable to bind to the domain."
> > > Write-Debug "$_.exception.message"
> > > Exit
> > > }
> > > $LDAPDN = [ADSI]("LDAP://$FQDN")
> > > $LDAPDN
> > > Write-Host "Bind to domain $FQDN was successful."
> > >
My System SpecsSystem Spec
Old 04-17-2008   #8 (permalink)
Marco Shaw [MVP]


 
 

Re: How to bind to AD without displaying the Distinguished Name

John wrote:
Quote:

> I need to determine whether or not I can bind to a domain. The only way I
> know how to do this is to actually try to bind to the domain and if it fails
> an errorcode will be trapped. When the code below is successfully binds to
> the domain, the distinguished name is output to the screen. Unfortunately do
> not want the distinguished name output to the screen.
OK, didn't know there was a difference. How can one tell?

How about if you do a redirect of std out "> out.tmp"?
My System SpecsSystem Spec
Old 04-17-2008   #9 (permalink)
John


 
 

RE: How to bind to AD without displaying the Distinguished Name

Chris,

That doesn't actually make a bind to the domain. You would have to have a
"$root" on the next line, where it would actually make the bind. You can
validate this by putting an invalid domain in variable $domain. If it
actually made the bind in the line "$root = new-object
System.directoryservices.DirectoryEntry($domain)" it would take longer and
eventually error out.


"Chris" wrote:
Quote:

> What I usually do is this :
>
> $domain = "LDAP://dc=foo,dc=com"
> $root = new-object System.directoryservices.DirectoryEntry($domain)
>
> At that point if I'm successful I'm bound to the domain .There will be no
> output if I'm successful and If I'm not I will get an error .
>
>
> From there you can run create a searcher object to do whatever .
>
> Does that do anything for ya ?
>
> Chris
>
>
> "John" wrote:
>
Quote:

> > The line $LDAPDN = [ADSI]("LDAP://$FQDN")" does not do the actual bind. You
> > would either have to call the variable $LDAPDN or simply run
> > [ADSI]("LDAP://$FQDN")". Either way, if it successfully binds to the domain
> > it displays the distinguished name, but I don't want that information
> > displayed. I am wondering if there is a way to bind to the domain without
> > having the distinguished name displayed.
> >
> > "Chris" wrote:
> >
Quote:

> > > Hi John ,
> > >
> > > I'm confused I think . What if you just left the $LDAPDN out . Then you
> > > would not get any output like you want . Right ?
> > >
> > > Thanks
> > > Chris
> > >
> > >
> > > "John" wrote:
> > >
> > > > Requirement:
> > > > Bind to Active Directory domain and if successful continue otherwise output
> > > > error.
> > > >
> > > > Problem:
> > > > The problem here is when I bind to the domain the Distinguished Name is
> > > > displayed on the screen. In the script that I am working on I don't want the
> > > > DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
> > > > "[void]$LDAPDN" but they do not actually make the bind to the domain. Any
> > > > help on binding to a domain without having it display the DN would be
> > > > appreciated.
> > > >
> > > > Thanks,
> > > >
> > > > Sample Code:
> > > > trap
> > > > {
> > > > Write-Host "Unable to bind to the domain."
> > > > Write-Debug "$_.exception.message"
> > > > Exit
> > > > }
> > > > $LDAPDN = [ADSI]("LDAP://$FQDN")
> > > > $LDAPDN
> > > > Write-Host "Bind to domain $FQDN was successful."
> > > >
My System SpecsSystem Spec
Old 04-17-2008   #10 (permalink)
Chris


 
 

RE: How to bind to AD without displaying the Distinguished Name

Here we go this should help :

$domain = "LDAP://dc=foo,dc=com"
$root = new-object System.directoryservices.DirectoryEntry($domain)

if ($root.Name)

{
"Great!"
}
else
{
"Not So Great!"
}


You can substitue this with something else or if its "true" then just go on
from there .

How about that ?

Thanks
Chris



"John" wrote:
Quote:

> Requirement:
> Bind to Active Directory domain and if successful continue otherwise output
> error.
>
> Problem:
> The problem here is when I bind to the domain the Distinguished Name is
> displayed on the screen. In the script that I am working on I don't want the
> DN to be displayed. I have tried the commands "$LDAPDN | out-null" and
> "[void]$LDAPDN" but they do not actually make the bind to the domain. Any
> help on binding to a domain without having it display the DN would be
> appreciated.
>
> Thanks,
>
> Sample Code:
> trap
> {
> Write-Host "Unable to bind to the domain."
> Write-Debug "$_.exception.message"
> Exit
> }
> $LDAPDN = [ADSI]("LDAP://$FQDN")
> $LDAPDN
> Write-Host "Bind to domain $FQDN was successful."
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Extract hostname from distinguished name PowerShell
in a bind Vista mail
Bind to LDAP Directory PowerShell
How to Get X.500 Distinguished path from samAccountNames? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46