0

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

1 Answer 1

2

You can explicit cast it into an enumerator:

var principalNames = (List<string>)xx.Properties["ServicePrincipalNames"].Value;
foreach(var principalName in principalNames) {...}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.