newbie needs help accessing ResultPropertyValueCollection

N

Neil Chambers

Hi all,

I'm converting a Powershell script I have into C# but I've hit a hurdle
right out of the gate . . .

I can't seem to access the (string) value of a
ResultPropertyValueCollection - all I get is the object type returned.

In the following example I'm enumerating the SearchResult PropertyName
collection just as a sanity check. The Names do return
as expected (so I know they are being loaded correctly) but when I try to
index on the Properties[] collection I get no useful data back
I've tried everything I can think of and have read as much as possible about
datatype casting but I just can't get it to fly.


___Begin Example____

DirectoryEntry deRoot = new DirectoryEntry(ldapMoniker); //ldapMoniker built
in other procedure
DirectorySearcher deSearch = new DirectorySearcher(deRoot);
foreach (string p in deSearcherProperties) //array of properties defined in
other procedure
{
deSearch.PropertiesToLoad.Add(p);
}
deSearch.Filter = deFilter; //deFilter defined in other procedure
SearchResult r = deSearch.FindOne();

foreach (string strName in r.Properties.PropertyNames)
{
Console.WriteLine(strName + "\n");
//writes the Property Name as expected
Console.WriteLine(r.Propertties[strName] + "\n");
//writes
"System.DirectoryServices.ResultPropertyValueCollection". What I expected
the value of this item: eg "computer name"
Console.WriteLine(r.Properties[strName].tostring() + "\n");
//as above
}

___End Example____

When I run this in Powershell the expected value is returned when I
issue 'r.Properties["propname"]' (the property name I am testing is "cn").
Interestingly enough PoSH also reports the Type of 'r.Properties["cn"]' as
String

Why is this happening and how can I get the expected result?

Cheers,
n
 
W

Willy Denoyette [MVP]

Neil Chambers said:
Hi all,

I'm converting a Powershell script I have into C# but I've hit a hurdle
right out of the gate . . .

I can't seem to access the (string) value of a
ResultPropertyValueCollection - all I get is the object type returned.

In the following example I'm enumerating the SearchResult PropertyName
collection just as a sanity check. The Names do return
as expected (so I know they are being loaded correctly) but when I try to
index on the Properties[] collection I get no useful data back
I've tried everything I can think of and have read as much as possible
about
datatype casting but I just can't get it to fly.


___Begin Example____

DirectoryEntry deRoot = new DirectoryEntry(ldapMoniker); //ldapMoniker
built
in other procedure
DirectorySearcher deSearch = new DirectorySearcher(deRoot);
foreach (string p in deSearcherProperties) //array of properties defined
in
other procedure
{
deSearch.PropertiesToLoad.Add(p);
}
deSearch.Filter = deFilter; //deFilter defined in other procedure
SearchResult r = deSearch.FindOne();

foreach (string strName in r.Properties.PropertyNames)
{
Console.WriteLine(strName + "\n");
//writes the Property Name as expected
Console.WriteLine(r.Propertties[strName] + "\n");
//writes
"System.DirectoryServices.ResultPropertyValueCollection". What I expected
the value of this item: eg "computer name"
Console.WriteLine(r.Properties[strName].tostring() + "\n");
//as above
}

___End Example____

When I run this in Powershell the expected value is returned when I
issue 'r.Properties["propname"]' (the property name I am testing is
"cn"). Interestingly enough PoSH also reports the Type of
'r.Properties["cn"]' as String

Why is this happening and how can I get the expected result?

Cheers,
n


r.Properties[strName] returns a ResultPropertyValueCollection, so you will
have to enumerate or use the indexer syntax to get at the items.


foreach( object val in sc.Properties[strName])
{
Console.WriteLine(" = " + val.ToString());
}

Note that this should be the same in PS.

Willy.
 
N

Neil Chambers

Willy Denoyette said:
Neil Chambers said:
Hi all,

I'm converting a Powershell script I have into C# but I've hit a hurdle
right out of the gate . . .

I can't seem to access the (string) value of a
ResultPropertyValueCollection - all I get is the object type returned.

In the following example I'm enumerating the SearchResult PropertyName
collection just as a sanity check. The Names do return
as expected (so I know they are being loaded correctly) but when I try to
index on the Properties[] collection I get no useful data back
I've tried everything I can think of and have read as much as possible
about
datatype casting but I just can't get it to fly.


___Begin Example____

DirectoryEntry deRoot = new DirectoryEntry(ldapMoniker); //ldapMoniker
built
in other procedure
DirectorySearcher deSearch = new DirectorySearcher(deRoot);
foreach (string p in deSearcherProperties) //array of properties defined
in
other procedure
{
deSearch.PropertiesToLoad.Add(p);
}
deSearch.Filter = deFilter; //deFilter defined in other procedure
SearchResult r = deSearch.FindOne();

foreach (string strName in r.Properties.PropertyNames)
{
Console.WriteLine(strName + "\n");
//writes the Property Name as expected
Console.WriteLine(r.Propertties[strName] + "\n");
//writes
"System.DirectoryServices.ResultPropertyValueCollection". What I expected
the value of this item: eg "computer name"
Console.WriteLine(r.Properties[strName].tostring() +
"\n");
//as above
}

___End Example____

When I run this in Powershell the expected value is returned when I
issue 'r.Properties["propname"]' (the property name I am testing is
"cn"). Interestingly enough PoSH also reports the Type of
'r.Properties["cn"]' as String

Why is this happening and how can I get the expected result?

Cheers,
n


r.Properties[strName] returns a ResultPropertyValueCollection, so you will
have to enumerate or use the indexer syntax to get at the items.


foreach( object val in sc.Properties[strName])
{
Console.WriteLine(" = " + val.ToString());
}

Note that this should be the same in PS.

Willy.

Thanls Willy. Enumeration to the rescue.

Strangely, Powershell doesn't require enumeration on this object. It gets
converted directly to SystemString.

Thanks again mate :)

n
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top