Windows Vista Forums
Vista Forums Home Join Vista Forums Donate 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

AD Search error

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-04-2007   #1 (permalink)
$hay
Guest


 

AD Search error

I have a text file that contain user names

users.txt
--------------
user1
user2
user3

i use this code to get the user dn

#==============
$Searcher = New-Object DirectoryServices.DirectorySearcher

cat C:\users.txt | % {
$Searcher.Filter =
"(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
$usr = $Searcher.FindOne().GetDirectoryEntry()
$usr.distinguishedname

# do aome other stuff based on user dn
}

#==============


The first iteration returns the first user's dn, but
the second iteration rerurns an error

You cannot call a method on a null-valued expression.
At C:\as400.ps1:62 char:46
+ $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )


--
$hay
http://scriptolog.blogspot.com




My System SpecsSystem Spec
Old 02-04-2007   #2 (permalink)
RichS
Guest


 

RE: AD Search error

Does it work if you just use the second name as input?

can you do a search in AD on that name & find the information?

--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"$hay" wrote:

> I have a text file that contain user names
>
> users.txt
> --------------
> user1
> user2
> user3
>
> i use this code to get the user dn
>
> #==============
> $Searcher = New-Object DirectoryServices.DirectorySearcher
>
> cat C:\users.txt | % {
> $Searcher.Filter =
> "(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
> $usr = $Searcher.FindOne().GetDirectoryEntry()
> $usr.distinguishedname
>
> # do aome other stuff based on user dn
> }
>
> #==============
>
>
> The first iteration returns the first user's dn, but
> the second iteration rerurns an error
>
> You cannot call a method on a null-valued expression.
> At C:\as400.ps1:62 char:46
> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
>
>
> --
> $hay
> http://scriptolog.blogspot.com
>
>
>
>

My System SpecsSystem Spec
Old 02-04-2007   #3 (permalink)
$hay
Guest


 

Re: AD Search error

trying manually :

$Searcher = New-Object DirectoryServices.DirectorySearcher

$Searcher.Filter =
"(&(objectCategory=person)(objectClass=user)(samAccountName=user1))"
$usr = $Searcher.FindOne().GetDirectoryEntry()
$usr.distinguishedname

$Searcher.Filter =
"(&(objectCategory=person)(objectClass=user)(samAccountName=user2))"
$usr = $Searcher.FindOne().GetDirectoryEntry()
$usr.distinguishedname

output:

CN=user1 name,OU=Users,DC=somedomainname
You cannot call a method on a null-valued expression.
At C:\as400.ps1:62 char:45
+ $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
CN=user1 name,OU=Users,DC=somedomainname

this always returns (in addition to the error) the dn for user1
I checked for users existence and they are OK.
--
$hay
http://scriptolog.blogspot.com


"RichS" <RichS@discussions.microsoft.com> wrote in message
news:52EE787C-4A4A-4C12-8E3D-89F20ED82DF9@microsoft.com...
> Does it work if you just use the second name as input?
>
> can you do a search in AD on that name & find the information?
>
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "$hay" wrote:
>
>> I have a text file that contain user names
>>
>> users.txt
>> --------------
>> user1
>> user2
>> user3
>>
>> i use this code to get the user dn
>>
>> #==============
>> $Searcher = New-Object DirectoryServices.DirectorySearcher
>>
>> cat C:\users.txt | % {
>> $Searcher.Filter =
>> "(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
>> $usr = $Searcher.FindOne().GetDirectoryEntry()
>> $usr.distinguishedname
>>
>> # do aome other stuff based on user dn
>> }
>>
>> #==============
>>
>>
>> The first iteration returns the first user's dn, but
>> the second iteration rerurns an error
>>
>> You cannot call a method on a null-valued expression.
>> At C:\as400.ps1:62 char:46
>> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
>>
>>
>> --
>> $hay
>> http://scriptolog.blogspot.com
>>
>>
>>
>>



My System SpecsSystem Spec
Old 02-04-2007   #4 (permalink)
RichS
Guest


 

Re: AD Search error

Do you need to recreate $Searcher each time?
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"$hay" wrote:

> trying manually :
>
> $Searcher = New-Object DirectoryServices.DirectorySearcher
>
> $Searcher.Filter =
> "(&(objectCategory=person)(objectClass=user)(samAccountName=user1))"
> $usr = $Searcher.FindOne().GetDirectoryEntry()
> $usr.distinguishedname
>
> $Searcher.Filter =
> "(&(objectCategory=person)(objectClass=user)(samAccountName=user2))"
> $usr = $Searcher.FindOne().GetDirectoryEntry()
> $usr.distinguishedname
>
> output:
>
> CN=user1 name,OU=Users,DC=somedomainname
> You cannot call a method on a null-valued expression.
> At C:\as400.ps1:62 char:45
> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
> CN=user1 name,OU=Users,DC=somedomainname
>
> this always returns (in addition to the error) the dn for user1
> I checked for users existence and they are OK.
> --
> $hay
> http://scriptolog.blogspot.com
>
>
> "RichS" <RichS@discussions.microsoft.com> wrote in message
> news:52EE787C-4A4A-4C12-8E3D-89F20ED82DF9@microsoft.com...
> > Does it work if you just use the second name as input?
> >
> > can you do a search in AD on that name & find the information?
> >
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "$hay" wrote:
> >
> >> I have a text file that contain user names
> >>
> >> users.txt
> >> --------------
> >> user1
> >> user2
> >> user3
> >>
> >> i use this code to get the user dn
> >>
> >> #==============
> >> $Searcher = New-Object DirectoryServices.DirectorySearcher
> >>
> >> cat C:\users.txt | % {
> >> $Searcher.Filter =
> >> "(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
> >> $usr = $Searcher.FindOne().GetDirectoryEntry()
> >> $usr.distinguishedname
> >>
> >> # do aome other stuff based on user dn
> >> }
> >>
> >> #==============
> >>
> >>
> >> The first iteration returns the first user's dn, but
> >> the second iteration rerurns an error
> >>
> >> You cannot call a method on a null-valued expression.
> >> At C:\as400.ps1:62 char:46
> >> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
> >>
> >>
> >> --
> >> $hay
> >> http://scriptolog.blogspot.com
> >>
> >>
> >>
> >>

>
>
>

My System SpecsSystem Spec
Old 02-04-2007   #5 (permalink)
$hay
Guest


 

Re: AD Search error

OK...problem solved



While i was testing against some test accounts, another sys admin

deleted one of them.



10x Richard, eventually your reply was in the right direction :-)



--
$hay
http://scriptolog.blogspot.com


"RichS" <RichS@discussions.microsoft.com> wrote in message
news:2243AC4C-703E-4D49-8D61-D7FC758AABD7@microsoft.com...
> Do you need to recreate $Searcher each time?
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "$hay" wrote:
>
>> trying manually :
>>
>> $Searcher = New-Object DirectoryServices.DirectorySearcher
>>
>> $Searcher.Filter =
>> "(&(objectCategory=person)(objectClass=user)(samAccountName=user1))"
>> $usr = $Searcher.FindOne().GetDirectoryEntry()
>> $usr.distinguishedname
>>
>> $Searcher.Filter =
>> "(&(objectCategory=person)(objectClass=user)(samAccountName=user2))"
>> $usr = $Searcher.FindOne().GetDirectoryEntry()
>> $usr.distinguishedname
>>
>> output:
>>
>> CN=user1 name,OU=Users,DC=somedomainname
>> You cannot call a method on a null-valued expression.
>> At C:\as400.ps1:62 char:45
>> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
>> CN=user1 name,OU=Users,DC=somedomainname
>>
>> this always returns (in addition to the error) the dn for user1
>> I checked for users existence and they are OK.
>> --
>> $hay
>> http://scriptolog.blogspot.com
>>
>>
>> "RichS" <RichS@discussions.microsoft.com> wrote in message
>> news:52EE787C-4A4A-4C12-8E3D-89F20ED82DF9@microsoft.com...
>> > Does it work if you just use the second name as input?
>> >
>> > can you do a search in AD on that name & find the information?
>> >
>> > --
>> > Richard Siddaway
>> > Please note that all scripts are supplied "as is" and with no warranty
>> > Blog: http://richardsiddaway.spaces.live.com/
>> > PowerShell User Group: http://www.get-psuguk.org.uk
>> >
>> >
>> > "$hay" wrote:
>> >
>> >> I have a text file that contain user names
>> >>
>> >> users.txt
>> >> --------------
>> >> user1
>> >> user2
>> >> user3
>> >>
>> >> i use this code to get the user dn
>> >>
>> >> #==============
>> >> $Searcher = New-Object DirectoryServices.DirectorySearcher
>> >>
>> >> cat C:\users.txt | % {
>> >> $Searcher.Filter =
>> >> "(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
>> >> $usr = $Searcher.FindOne().GetDirectoryEntry()
>> >> $usr.distinguishedname
>> >>
>> >> # do aome other stuff based on user dn
>> >> }
>> >>
>> >> #==============
>> >>
>> >>
>> >> The first iteration returns the first user's dn, but
>> >> the second iteration rerurns an error
>> >>
>> >> You cannot call a method on a null-valued expression.
>> >> At C:\as400.ps1:62 char:46
>> >> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
>> >>
>> >>
>> >> --
>> >> $hay
>> >> http://scriptolog.blogspot.com
>> >>
>> >>
>> >>
>> >>

>>
>>
>>



My System SpecsSystem Spec
Old 02-04-2007   #6 (permalink)
RichS
Guest


 

Re: AD Search error

Glad it is resolved. I was just about to post that I'd actually got round to
testing your code & it worked fine.
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"$hay" wrote:

> OK...problem solved
>
>
>
> While i was testing against some test accounts, another sys admin
>
> deleted one of them.
>
>
>
> 10x Richard, eventually your reply was in the right direction :-)
>
>
>
> --
> $hay
> http://scriptolog.blogspot.com
>
>
> "RichS" <RichS@discussions.microsoft.com> wrote in message
> news:2243AC4C-703E-4D49-8D61-D7FC758AABD7@microsoft.com...
> > Do you need to recreate $Searcher each time?
> > --
> > Richard Siddaway
> > Please note that all scripts are supplied "as is" and with no warranty
> > Blog: http://richardsiddaway.spaces.live.com/
> > PowerShell User Group: http://www.get-psuguk.org.uk
> >
> >
> > "$hay" wrote:
> >
> >> trying manually :
> >>
> >> $Searcher = New-Object DirectoryServices.DirectorySearcher
> >>
> >> $Searcher.Filter =
> >> "(&(objectCategory=person)(objectClass=user)(samAccountName=user1))"
> >> $usr = $Searcher.FindOne().GetDirectoryEntry()
> >> $usr.distinguishedname
> >>
> >> $Searcher.Filter =
> >> "(&(objectCategory=person)(objectClass=user)(samAccountName=user2))"
> >> $usr = $Searcher.FindOne().GetDirectoryEntry()
> >> $usr.distinguishedname
> >>
> >> output:
> >>
> >> CN=user1 name,OU=Users,DC=somedomainname
> >> You cannot call a method on a null-valued expression.
> >> At C:\as400.ps1:62 char:45
> >> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
> >> CN=user1 name,OU=Users,DC=somedomainname
> >>
> >> this always returns (in addition to the error) the dn for user1
> >> I checked for users existence and they are OK.
> >> --
> >> $hay
> >> http://scriptolog.blogspot.com
> >>
> >>
> >> "RichS" <RichS@discussions.microsoft.com> wrote in message
> >> news:52EE787C-4A4A-4C12-8E3D-89F20ED82DF9@microsoft.com...
> >> > Does it work if you just use the second name as input?
> >> >
> >> > can you do a search in AD on that name & find the information?
> >> >
> >> > --
> >> > Richard Siddaway
> >> > Please note that all scripts are supplied "as is" and with no warranty
> >> > Blog: http://richardsiddaway.spaces.live.com/
> >> > PowerShell User Group: http://www.get-psuguk.org.uk
> >> >
> >> >
> >> > "$hay" wrote:
> >> >
> >> >> I have a text file that contain user names
> >> >>
> >> >> users.txt
> >> >> --------------
> >> >> user1
> >> >> user2
> >> >> user3
> >> >>
> >> >> i use this code to get the user dn
> >> >>
> >> >> #==============
> >> >> $Searcher = New-Object DirectoryServices.DirectorySearcher
> >> >>
> >> >> cat C:\users.txt | % {
> >> >> $Searcher.Filter =
> >> >> "(&(objectCategory=person)(objectClass=user)(samAccountName=$_))"
> >> >> $usr = $Searcher.FindOne().GetDirectoryEntry()
> >> >> $usr.distinguishedname
> >> >>
> >> >> # do aome other stuff based on user dn
> >> >> }
> >> >>
> >> >> #==============
> >> >>
> >> >>
> >> >> The first iteration returns the first user's dn, but
> >> >> the second iteration rerurns an error
> >> >>
> >> >> You cannot call a method on a null-valued expression.
> >> >> At C:\as400.ps1:62 char:46
> >> >> + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< )
> >> >>
> >> >>
> >> >> --
> >> >> $hay
> >> >> http://scriptolog.blogspot.com
> >> >>
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>

>
>
>

My System SpecsSystem Spec
Closed Thread
Update your Vista Drivers Update Your Drivers Now!!

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Indexer Error message Steve Vista General 2 03-17-2008 05:05 PM
Desktop search - error 0x80041206 Rein de Jong Vista General 0 01-31-2008 05:43 AM
Search indexer error message Spikey Vista General 1 10-07-2007 01:40 PM
Windows Search Indexer error Atanu Ghose Vista General 1 07-24-2006 12:37 AM


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