I am little bit unsure on what you mean by get value GivenName into a parameter. If you just want the given name from the results then it is easy to get.
var givenName = cmdlet.Properties["GivenName"].Value;
If you looking to get all the properties then you can get it in PSMemberInfoCollection
PSMemberInfoCollection<PSPropertyInfo> props = psObject.Properties;
props.ToList().ForEach(x => {
Console.WriteLine("Name: {0}, Value: {1}", x.Name,x.Value);
});
If you are trying to convert PSObject
into ADUser
then you will have to load the assembly Microsoft.ActiveDirectory.Management
in your c# sharp code, then create an instance of ADUser from the PSObject
Edit1:
So if you want to get the groups or a particular group by index then. Again this just purely based upon hacks. Use all the code with caution. Anyways you can use dynamic keyword to do something interesting. First you can get the GivenName
property just like that. And then your MemberOf property will give you a ValueList which is of type Collection and you get property by an index or you can Enumerate over all the properties available to you.
dynamic adUser = psObject.BaseObject;
Console.WriteLine("GivenName: {0}, Surname: {1}",adUser.GivenName.ToString(),adUser.Surname.ToString());
dynamic memberof = psObject.Properties["MemberOf"].Value;
var firstGroup = memberof.ValueList[0].ToString();
var allGroups = memberof.ValueList;
foreach (var item in allGroups)
{
Console.WriteLine(item.ToString());
}