Windows Vista Forums
Vista Forums Home Join Vista Forums Webcasts Windows 7 Forum Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

How to Get X.500 Distinguished path from samAccountNames?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-04-2006   #1 (permalink)
=?Utf-8?B?S2FseWFu?=
Guest


 

How to Get X.500 Distinguished path from samAccountNames?

Hi All,

I have a list of user id's (samAccountNames) from which i need to get the
LDAP paths foreach user, Is there any possible ways to achieve this via
PowerShell RC2?
My intention is to put all samAccountNames into an CSV File, Import it into
PS, Get the LDAP Paths, Export to the same CSV File into a different column.

I hope this should be possible but i'm unable to guess how to achieve this!!

My System SpecsSystem Spec
Old 10-05-2006   #2 (permalink)
Brandon Shell
Guest


 

Re: How to Get X.500 Distinguished path from samAccountNames?

This still works on RC2 for me:

Code:
------
$user = "MyUserName"
$root = New-Object System.DirectoryServices.DirectoryEntry
$objCommand = New-object -comobject "ADODB.Command"
$objConnection = New-object -comobject "ADODB.Connection"
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

$query =
"<LDAP://$($root.distinguishedName)>;(&(objectCategory=user)(samAccountName=$user));distinguishedName,adspath;subtree"
$objCommand.CommandText = $query
$rs = $objCommand.Execute()

While(!($rs.EOF)) {
$rs.fields.item('distinguishedName').value
$rs.MoveNext()
}
---------

"Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
news:1DACCC25-B45E-49A2-A89B-0EA9982AD805@microsoft.com...
> Hi All,
>
> I have a list of user id's (samAccountNames) from which i need to get the
> LDAP paths foreach user, Is there any possible ways to achieve this via
> PowerShell RC2?
> My intention is to put all samAccountNames into an CSV File, Import it
> into
> PS, Get the LDAP Paths, Export to the same CSV File into a different
> column.
>
> I hope this should be possible but i'm unable to guess how to achieve
> this!!


My System SpecsSystem Spec
Old 10-05-2006   #3 (permalink)
Brandon Shell
Guest


 

Re: How to Get X.500 Distinguished path from samAccountNames?

This may be a little faster than my other example:
Code:
-------------------------------------------------
$user = "MyUserName"
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.Filter = "(&(objectCategory=user)(samAccountName=$user))"
$userAry = $ds.findall()
Foreach($user in $userAry) {
$user.Path
}
-------------------------------------------------
"Brandon Shell" <tshell.mask@gmail.com> wrote in message
news:ebvtW2K6GHA.4112@TK2MSFTNGP04.phx.gbl...
> This still works on RC2 for me:
>
> Code:
> ------
> $user = "MyUserName"
> $root = New-Object System.DirectoryServices.DirectoryEntry
> $objCommand = New-object -comobject "ADODB.Command"
> $objConnection = New-object -comobject "ADODB.Connection"
> $objConnection.Provider = "ADsDSOObject"
> $objConnection.Open("Active Directory Provider")
> $objCommand.ActiveConnection = $objConnection
>
> $query =
> "<LDAP://$($root.distinguishedName)>;(&(objectCategory=user)(samAccountName=$user));distinguishedName,adspath;subtree"
> $objCommand.CommandText = $query
> $rs = $objCommand.Execute()
>
> While(!($rs.EOF)) {
> $rs.fields.item('distinguishedName').value
> $rs.MoveNext()
> }
> ---------
>
> "Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
> news:1DACCC25-B45E-49A2-A89B-0EA9982AD805@microsoft.com...
>> Hi All,
>>
>> I have a list of user id's (samAccountNames) from which i need to get the
>> LDAP paths foreach user, Is there any possible ways to achieve this via
>> PowerShell RC2?
>> My intention is to put all samAccountNames into an CSV File, Import it
>> into
>> PS, Get the LDAP Paths, Export to the same CSV File into a different
>> column.
>>
>> I hope this should be possible but i'm unable to guess how to achieve
>> this!!

>


My System SpecsSystem Spec
Old 10-05-2006   #4 (permalink)
=?Utf-8?B?SmltIEhvbGJhY2g=?=
Guest


 

RE: How to Get X.500 Distinguished path from samAccountNames?

function get-ADEntry {
param ($samAccount)
$local:Searcher = New-Object DirectoryServices.DirectorySearcher
$Searcher.Filter =
"(&(objectCategory=person)(objectClass=user)(samAccountName=$samAccount))"
$Searcher.FindOne().GetDirectoryEntry()
}

get-content input.txt|foreach { get-adentry -samaccount $_}| select
@{Name="samaccount";
expression={$_.samaccountname}},@{Name="DN";expression={$_.distinguishedname}} | export-csv output.csv -noTypeInformation

---
Jim Holbach

"Kalyan" wrote:

> Hi All,
>
> I have a list of user id's (samAccountNames) from which i need to get the
> LDAP paths foreach user, Is there any possible ways to achieve this via
> PowerShell RC2?
> My intention is to put all samAccountNames into an CSV File, Import it into
> PS, Get the LDAP Paths, Export to the same CSV File into a different column.
>
> I hope this should be possible but i'm unable to guess how to achieve this!!

My System SpecsSystem Spec
Old 10-05-2006   #5 (permalink)
=?Utf-8?B?S2FseWFu?=
Guest


 

RE: How to Get X.500 Distinguished path from samAccountNames?

Ohh That worked and retrived my LDAP paths well but when i exported to csv.
ionly came to see the following for two sample usernames.

IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2

Any help?

"Jim Holbach" wrote:

> function get-ADEntry {
> param ($samAccount)
> $local:Searcher = New-Object DirectoryServices.DirectorySearcher
> $Searcher.Filter =
> "(&(objectCategory=person)(objectClass=user)(samAccountName=$samAccount))"
> $Searcher.FindOne().GetDirectoryEntry()
> }
>
> get-content input.txt|foreach { get-adentry -samaccount $_}| select
> @{Name="samaccount";
> expression={$_.samaccountname}},@{Name="DN";expression={$_.distinguishedname}} | export-csv output.csv -noTypeInformation
>
> ---
> Jim Holbach
>
> "Kalyan" wrote:
>
> > Hi All,
> >
> > I have a list of user id's (samAccountNames) from which i need to get the
> > LDAP paths foreach user, Is there any possible ways to achieve this via
> > PowerShell RC2?
> > My intention is to put all samAccountNames into an CSV File, Import it into
> > PS, Get the LDAP Paths, Export to the same CSV File into a different column.
> >
> > I hope this should be possible but i'm unable to guess how to achieve this!!

My System SpecsSystem Spec
Old 10-05-2006   #6 (permalink)
=?Utf-8?B?S2FseWFu?=
Guest


 

Re: How to Get X.500 Distinguished path from samAccountNames?

Brandon, great to see your reply. Worked like a charm:-)

I had a problem putting the path to a variable as follows
$user = "MyUserName"
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.Filter = "(&(objectCategory=user)(samAccountName=$user))"
$userAry = $ds.findall()
Foreach($user in $userAry) {
$user.Path
$usrprop = [ADSI]("$user.path")
}

But the $usrprop returning an error saying unable to retrive
classid{..............} etc!!

Am i doing something wrong?


"Brandon Shell" wrote:

> This may be a little faster than my other example:
> Code:
> -------------------------------------------------
> $user = "MyUserName"
> $ds = New-Object System.DirectoryServices.DirectorySearcher
> $ds.Filter = "(&(objectCategory=user)(samAccountName=$user))"
> $userAry = $ds.findall()
> Foreach($user in $userAry) {
> $user.Path
> }
> -------------------------------------------------
> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
> news:ebvtW2K6GHA.4112@TK2MSFTNGP04.phx.gbl...
> > This still works on RC2 for me:
> >
> > Code:
> > ------
> > $user = "MyUserName"
> > $root = New-Object System.DirectoryServices.DirectoryEntry
> > $objCommand = New-object -comobject "ADODB.Command"
> > $objConnection = New-object -comobject "ADODB.Connection"
> > $objConnection.Provider = "ADsDSOObject"
> > $objConnection.Open("Active Directory Provider")
> > $objCommand.ActiveConnection = $objConnection
> >
> > $query =
> > "<LDAP://$($root.distinguishedName)>;(&(objectCategory=user)(samAccountName=$user));distinguishedName,adspath;subtree"
> > $objCommand.CommandText = $query
> > $rs = $objCommand.Execute()
> >
> > While(!($rs.EOF)) {
> > $rs.fields.item('distinguishedName').value
> > $rs.MoveNext()
> > }
> > ---------
> >
> > "Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
> > news:1DACCC25-B45E-49A2-A89B-0EA9982AD805@microsoft.com...
> >> Hi All,
> >>
> >> I have a list of user id's (samAccountNames) from which i need to get the
> >> LDAP paths foreach user, Is there any possible ways to achieve this via
> >> PowerShell RC2?
> >> My intention is to put all samAccountNames into an CSV File, Import it
> >> into
> >> PS, Get the LDAP Paths, Export to the same CSV File into a different
> >> column.
> >>
> >> I hope this should be possible but i'm unable to guess how to achieve
> >> this!!

> >

>
>

My System SpecsSystem Spec
Old 10-06-2006   #7 (permalink)
Brandon Shell
Guest


 

Re: How to Get X.500 Distinguished path from samAccountNames?

This should work find, I dont see a need to type it.
$path = $user.path

Now... if typing is your thing then I think you need to do this:
$path = [ADSI]($user.path) # No need for "" as this is a already acts
like a string.
# This will get you an object with the property of distinguishedName so
you have to do this to get just the DN
$path.distinguishedName

"Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
news:C61EFAE1-D1EB-4FDB-99A2-362A2B5D7BDF@microsoft.com...
> Brandon, great to see your reply. Worked like a charm:-)
>
> I had a problem putting the path to a variable as follows
> $user = "MyUserName"
> $ds = New-Object System.DirectoryServices.DirectorySearcher
> $ds.Filter = "(&(objectCategory=user)(samAccountName=$user))"
> $userAry = $ds.findall()
> Foreach($user in $userAry) {
> $user.Path
> $usrprop = [ADSI]("$user.path")
> }
>
> But the $usrprop returning an error saying unable to retrive
> classid{..............} etc!!
>
> Am i doing something wrong?
>
>
> "Brandon Shell" wrote:
>
>> This may be a little faster than my other example:
>> Code:
>> -------------------------------------------------
>> $user = "MyUserName"
>> $ds = New-Object System.DirectoryServices.DirectorySearcher
>> $ds.Filter = "(&(objectCategory=user)(samAccountName=$user))"
>> $userAry = $ds.findall()
>> Foreach($user in $userAry) {
>> $user.Path
>> }
>> -------------------------------------------------
>> "Brandon Shell" <tshell.mask@gmail.com> wrote in message
>> news:ebvtW2K6GHA.4112@TK2MSFTNGP04.phx.gbl...
>> > This still works on RC2 for me:
>> >
>> > Code:
>> > ------
>> > $user = "MyUserName"
>> > $root = New-Object System.DirectoryServices.DirectoryEntry
>> > $objCommand = New-object -comobject "ADODB.Command"
>> > $objConnection = New-object -comobject "ADODB.Connection"
>> > $objConnection.Provider = "ADsDSOObject"
>> > $objConnection.Open("Active Directory Provider")
>> > $objCommand.ActiveConnection = $objConnection
>> >
>> > $query =
>> > "<LDAP://$($root.distinguishedName)>;(&(objectCategory=user)(samAccountName=$user));distinguishedName,adspath;subtree"
>> > $objCommand.CommandText = $query
>> > $rs = $objCommand.Execute()
>> >
>> > While(!($rs.EOF)) {
>> > $rs.fields.item('distinguishedName').value
>> > $rs.MoveNext()
>> > }
>> > ---------
>> >
>> > "Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
>> > news:1DACCC25-B45E-49A2-A89B-0EA9982AD805@microsoft.com...
>> >> Hi All,
>> >>
>> >> I have a list of user id's (samAccountNames) from which i need to get
>> >> the
>> >> LDAP paths foreach user, Is there any possible ways to achieve this
>> >> via
>> >> PowerShell RC2?
>> >> My intention is to put all samAccountNames into an CSV File, Import it
>> >> into
>> >> PS, Get the LDAP Paths, Export to the same CSV File into a different
>> >> column.
>> >>
>> >> I hope this should be possible but i'm unable to guess how to achieve
>> >> this!!
>> >

>>
>>


My System SpecsSystem Spec
Old 10-06-2006   #8 (permalink)
=?Utf-8?B?SmltIEhvbGJhY2g=?=
Guest


 

RE: How to Get X.500 Distinguished path from samAccountNames?

Export-csv exports objects. Looks like you're piping hash tables to
export-csv. Get rid of the -noTypeInformation switch and the first line of
the csv file will show the type of the object you've exported.

So for example:

PS> $a=@{1=2}
PS> $a|export-csv hash.csv
PS > get-content hash.csv
#TYPE System.Collections.Hashtable
IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,1

But if I run the code I posted (without the -noTypeInformation switch) and
then check the file it created:

PS> get-content output.csv
#TYPE System.Management.Automation.PSCustomObject
samaccount,DN
user1,"CN=One\, User,OU=Testing,OU=Users,OU=Corp,DC=cmcsg,DC=com"
user2,"CN=Two\, User,OU=Testing,OU=Users,OU=Corp,DC=cmcsg,DC=com"

Can you post the exact code you're using?

---
Jim Holbach

"Kalyan" wrote:

> Ohh That worked and retrived my LDAP paths well but when i exported to csv.
> ionly came to see the following for two sample usernames.
>
> IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
> False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
> False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
>
> Any help?
>
> "Jim Holbach" wrote:
>
> > function get-ADEntry {
> > param ($samAccount)
> > $local:Searcher = New-Object DirectoryServices.DirectorySearcher
> > $Searcher.Filter =
> > "(&(objectCategory=person)(objectClass=user)(samAccountName=$samAccount))"
> > $Searcher.FindOne().GetDirectoryEntry()
> > }
> >
> > get-content input.txt|foreach { get-adentry -samaccount $_}| select
> > @{Name="samaccount";
> > expression={$_.samaccountname}},@{Name="DN";expression={$_.distinguishedname}} | export-csv output.csv -noTypeInformation
> >
> > ---
> > Jim Holbach
> >
> > "Kalyan" wrote:
> >
> > > Hi All,
> > >
> > > I have a list of user id's (samAccountNames) from which i need to get the
> > > LDAP paths foreach user, Is there any possible ways to achieve this via
> > > PowerShell RC2?
> > > My intention is to put all samAccountNames into an CSV File, Import it into
> > > PS, Get the LDAP Paths, Export to the same CSV File into a different column.
> > >
> > > I hope this should be possible but i'm unable to guess how to achieve this!!

My System SpecsSystem Spec
Old 10-06-2006   #9 (permalink)
Brandon Shell
Guest


 

Re: How to Get X.500 Distinguished path from samAccountNames?

Im not sure... His code works for me.

If you dont mind... whats the text file look like? I wonder if you have
special characters mucking up the works.

Also... This happens to me sometimes, Are you sure you reformated the code
so its valid. Newsreaders are not friendly when trying to maintain format of
text.

"Kalyan" <Kalyan@discussions.microsoft.com> wrote in message
news:BF4BD901-C2EC-47E5-BE79-9340A563B694@microsoft.com...
> Ohh That worked and retrived my LDAP paths well but when i exported to
> csv.
> ionly came to see the following for two sample usernames.
>
> IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
> False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
> False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
>
> Any help?
>
> "Jim Holbach" wrote:
>
>> function get-ADEntry {
>> param ($samAccount)
>> $local:Searcher = New-Object DirectoryServices.DirectorySearcher
>> $Searcher.Filter =
>> "(&(objectCategory=person)(objectClass=user)(samAccountName=$samAccount))"
>> $Searcher.FindOne().GetDirectoryEntry()
>> }
>>
>> get-content input.txt|foreach { get-adentry -samaccount $_}| select
>> @{Name="samaccount";
>> expression={$_.samaccountname}},@{Name="DN";expression={$_.distinguishedname}}
>> | export-csv output.csv -noTypeInformation
>>
>> ---
>> Jim Holbach
>>
>> "Kalyan" wrote:
>>
>> > Hi All,
>> >
>> > I have a list of user id's (samAccountNames) from which i need to get
>> > the
>> > LDAP paths foreach user, Is there any possible ways to achieve this via
>> > PowerShell RC2?
>> > My intention is to put all samAccountNames into an CSV File, Import it
>> > into
>> > PS, Get the LDAP Paths, Export to the same CSV File into a different
>> > column.
>> >
>> > I hope this should be possible but i'm unable to guess how to achieve
>> > this!!


My System SpecsSystem Spec
Old 10-06-2006   #10 (permalink)
=?Utf-8?B?S2FseWFu?=
Guest


 

RE: How to Get X.500 Distinguished path from samAccountNames?

Oops, I've exported the file to c:\ and is looking for it in my working
directory.. The file that i showed you is actually created earlier by some
other script by mistake..

Finally resolved, the code that you posted absolutely works fine
Holbach...Thank You

"Jim Holbach" wrote:

> Export-csv exports objects. Looks like you're piping hash tables to
> export-csv. Get rid of the -noTypeInformation switch and the first line of
> the csv file will show the type of the object you've exported.
>
> So for example:
>
> PS> $a=@{1=2}
> PS> $a|export-csv hash.csv
> PS > get-content hash.csv
> #TYPE System.Collections.Hashtable
> IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
> False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,1
>
> But if I run the code I posted (without the -noTypeInformation switch) and
> then check the file it created:
>
> PS> get-content output.csv
> #TYPE System.Management.Automation.PSCustomObject
> samaccount,DN
> user1,"CN=One\, User,OU=Testing,OU=Users,OU=Corp,DC=cmcsg,DC=com"
> user2,"CN=Two\, User,OU=Testing,OU=Users,OU=Corp,DC=cmcsg,DC=com"
>
> Can you post the exact code you're using?
>
> ---
> Jim Holbach
>
> "Kalyan" wrote:
>
> > Ohh That worked and retrived my LDAP paths well but when i exported to csv.
> > ionly came to see the following for two sample usernames.
> >
> > IsReadOnly,IsFixedSize,IsSynchronized,Keys,Values,SyncRoot,Count
> > False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
> > False,False,False,System.Collections.Hashtable+KeyCollection,System.Collections.Hashtable+ValueCollection,System.Object,2
> >
> > Any help?
> >
> > "Jim Holbach" wrote:
> >
> > > function get-ADEntry {
> > > param ($samAccount)
> > > $local:Searcher = New-Object DirectoryServices.DirectorySearcher
> > > $Searcher.Filter =
> > > "(&(objectCategory=person)(objectClass=user)(samAccountName=$samAccount))"
> > > $Searcher.FindOne().GetDirectoryEntry()
> > > }
> > >
> > > get-content input.txt|foreach { get-adentry -samaccount $_}| select
> > > @{Name="samaccount";
> > > expression={$_.samaccountname}},@{Name="DN";expression={$_.distinguishedname}} | export-csv output.csv -noTypeInformation
> > >
> > > ---
> > > Jim Holbach
> > >
> > > "Kalyan" wrote:
> > >
> > > > Hi All,
> > > >
> > > > I have a list of user id's (samAccountNames) from which i need to get the
> > > > LDAP paths foreach user, Is there any possible ways to achieve this via
> > > > PowerShell RC2?
> > > > My intention is to put all samAccountNames into an CSV File, Import it into
> > > > PS, Get the LDAP Paths, Export to the same CSV File into a different column.
> > > >
> > > > I hope this should be possible but i'm unable to guess how to achieve this!!

My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to bind to AD without displaying the Distinguished Name John PowerShell 11 04-18-2008 09:02 AM
Extract hostname from distinguished name cmyers PowerShell 11 02-07-2008 08:44 AM
BUG? (Test-Path $path -IsValid) and empty $path =?Utf-8?B?Um9tYW4gS3V6bWlu?= PowerShell 1 08-28-2006 12:10 PM
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path Adam Milazzo [MSFT] PowerShell 2 08-12-2006 03:14 AM
Binding(string path) - what is the syntax for the path? Jason Dolinger Avalon 2 01-10-2006 03:52 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51