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

Cannot get sAMAcountName ??

244 views
Skip to first unread message

Daniel Schmitz

unread,
Feb 20, 2008, 9:41:00 AM2/20/08
to

Can anybody say me why sAMAccount is empty??


$CountryGroup = New-Object System.DirectoryServices.DirectoryEntry( $LDAP +
"OU=USERS,OU=_DE,DC=me-dus,DC=de" )

# Create a selector and start searching from the Root of AD
$searcher = New-Object
System.DirectoryServices.DirectorySearcher($CountryGroup)
$searcher.Filter = "(&(objectCategory=person)(objectClass=user))"
$searcher.PropertiesToLoad.Add("distinguishedName") >$null
$searcher.PropertiesToLoad.Add("sAMAccountName") >$null
$searcher.PropertiesToLoad.Add("givenname") >$null
$searcher.PropertiesToLoad.Add("sn") >$null
$result = $searcher.FindAll()
"There are $($result.count) objects in the $($CountryGroup.name) domain"

foreach ($person in $result)
{
$prop = $person.Properties
Write-Host $($prop["givenname"])
Write-Host $($prop["sAMAccountName"])
#Write-Host "First Name: $($prop.givenname) Surname: $($prop.sn)
sAMAccountName: $($prop[""[sAMAccountName""])"

if(!(Test-Path ("C:\0\" + $prop.cn)))
{
Write-Host "Directory not found... ($($prop[""[sAMAccountName""]))"
}
}


This script print out "givenName" but not "sAMAccoutName.
I'am new to PowerShell and found this link in Web:

http://blogs.technet.com/bahramr/archive/2008/01/25/powershell-script-to-disable-inactive-accounts-in-active-directory.aspx

I don't see any diffrences :-(((


Brandon Shell [MVP]

unread,
Feb 20, 2008, 9:56:25 AM2/20/08
to
A couple things first
1) when you use Searcher... properties are all lowercase (I know, I know,
but that is the way it is)
2) Filter/Properties can be passed to the constructor.
3) Perhaps you can try the .GetDirectoryEntry() method. It will convert the
search result to a DirectoryEntry Object.

Try this...
##################
$CountryGroup = New-Object System.DirectoryServices.DirectoryEntry(
"LDAP://OU=USERS,OU=_DE,DC=me-dus,DC=de" )
$filter = "(&(objectCategory=user))"
$props = "sAMAccountName","givenname","sn"
$searcher = New-Object
System.DirectoryServices.DirectorySearcher($CountryGroup,$filter,$props)
$result = $searcher.FindAll()


foreach ($person in $result)
{
$prop = $person.Properties

Write-Host $person.properties.givenname
Write-Host $person.properties.samaccountname


#Write-Host "First Name: $($prop.givenname) Surname: $($prop.sn)
sAMAccountName: $($prop[""[sAMAccountName""])"
if(!(Test-Path ("C:\0\" + $prop.cn)))
{
Write-Host "Directory not found... ($($prop[""[sAMAccountName""]))"
}
}

"Daniel Schmitz" <d...@me-dus.de> wrote in message
news:48ABC521-B441-4882...@microsoft.com...

Daniel Schmitz

unread,
Feb 20, 2008, 10:29:01 AM2/20/08
to
Thanks, thanks and thanks for the quick response!!

Point 1) is the solution, that i searched for many hours.

But one question. I write the same code in C# and in C# it works fine with
non lowercase properties!? Why??

Brandon Shell [MVP]

unread,
Feb 20, 2008, 10:54:45 AM2/20/08
to
Not entirely sure... just know from experience. I will ask though.


"Daniel Schmitz" <d...@me-dus.de> wrote in message

news:694F750E-D87E-41AA...@microsoft.com...

Shay Levi

unread,
Mar 2, 2008, 4:38:13 AM3/2/08
to

I found this on Frank Koch's, Free PowerShell Book (Page 29, see link below),
ADSI properties are case-sensitive:

"Please note that the ADSI query is case-sensitive, so name and description
must be written in lower-case letters."


English download (16 MB):
http://blogs.technet.com/chitpro-de/archive/2008/02/28/free-windows-powershell-workbook-server-administration.aspx

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

Brandon Shell [MVP]

unread,
Mar 3, 2008, 7:03:11 PM3/3/08
to
That is actually incorrect... ADSI is not case-sensitive (except in case of
the Provider i.e. LDAP/WinNT.)

There is a bug in Powershell that marks the Collection returned from
DirectorySearcher to be case-sensitive (and it doesnt even do it right. IF
ANYTHING it should be camelcase.)

The Bug is filed on connect.

The work around is to use the item() method on the properties collection.

Like: $user.properties.item('sAMAccountName')

"Shay Levi" <n...@addre.ss> wrote in message
news:8766a944217c08...@news.microsoft.com...

Shay Levi

unread,
Mar 4, 2008, 4:02:01 AM3/4/08
to

Well, this is for Frank to answer. One can conclude that the 'ADSI query
is case-sensitive' not the ADSI type.
What bothers me is even if it is 'case-sensitive', 'sAMAccountName' is not
the same as 'SAMAccountName', so the first should work.

Does 'case-sensitive' means 'lower-case-sensitive'? So many questions :)

Brandon Shell [MVP]

unread,
Mar 4, 2008, 8:15:04 AM3/4/08
to
It is actually quite the opposite. ADSI query is NOT case-sensitive. You can
pass any case you want to the searcher and it will return objects, but in
Powershell (because of a bug) the returned collection IS case-sensitive
(incorrectly so as it is not camelcase.) I suppose you could say it is lower
case-sensitive :)

btw... it is the same for V2 so I am using item() and suggest that all do
the same. Also... go vote for my bug on connect :)

https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=329952&SiteID=99

"Shay Levi" <n...@addre.ss> wrote in message

news:8766a94421c768...@news.microsoft.com...

Shay Levi

unread,
Mar 4, 2008, 8:14:52 AM3/4/08
to

> Also... go vote for my bug on connect :)

I just did.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> It is actually quite the opposite. ADSI query is NOT case-sensitive.

>>>> -p owershell-workbook-server-administration.aspx

0 new messages