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
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