Reputation: 25
I'm trying to use some of the PowerShell Azure commands in C#. In case when property of returned object is a single element (string/int/..) it works just great, something like:
foreach (PSObject xx in results)
{
PSMemberInfo ObjectId = xx.Properties["ObjectId"];
PSMemberInfo DisplayName = xx.Properties["DisplayName"];
}
As a result I can access their values by ObjectId.Value or DisplayName.Value. It works great.
However I can't get values if data of returned PowerShell property is not a single element, but a collection of something. For example if I get PS object like:
ExtensionData : System.Runtime.Serialization.ExtensionDataObject
AccountEnabled : True
Addresses : {}
AppPrincipalId : 00000000-0000-0000-0000-000000000000
DisplayName : access_00000000-0000-0000-0000-000000000000
ObjectId : 00000000-0000-0000-0000-000000000000
ServicePrincipalNames : {00000000-0000-0000-0000-000000000000, access_00000000-0000-0000-0000-000000000000}
TrustedForDelegation : False
Note that ServicePrincipalNames and Addresses are lists. In this case xx.Properties["ServicePrincipalNames"]
contains a list, if I simply WriteLine it (Console.WriteLine(xx.Properties["ServicePrincipalNames"].Value);
) I'll see: "System.Collections.Generic.List1[System.String]
" so this looks like a collection, though I can't iterate over it. If I try:
foreach(var g in ...)
Compiler brings an error that xx.Properties["ServicePrincipalNames"].Value
is an object and does not have any enumerators.
Google doesn't help much. Anyone experienced before?
Thanks
Upvotes: 0
Views: 1645
Reputation: 4147
You can explicit cast it into an enumerator:
var principalNames = (List<string>)xx.Properties["ServicePrincipalNames"].Value;
foreach(var principalName in principalNames) {...}
Upvotes: 2