简体   繁体   中英

How to get the properties of Cmdlet results which contain list of deserialized objects (multi-valued properties)(executing powershell cmdlets from c#)

I am using c# to execute PowerShell cmdlets and parsing the cmdlet results (Collection[].Properties["PropertyName"].value) to get the required values.

But some of the results contains a list of deserialized objects. But PowerShell is simply converting these to ArrayList of objects and i am unable to get all the properties of them.

For ex: Get-MailboxDatabase of Exchange Management Shell cmdlet returns list of mailboxdatabase on exchange server (Collection). I can able to get properties like name, server etc... from PSObject.Properties["PropertyName"].Value.

It also contains a list of database copies (something like below)

class MailboxDatabase { DatabaseCopy[] DatabaseCopies; }

the PSObject.Properties["DatabaseCopies"].Value is nothing but an arrayList of objects.

But all the DatabaseCopy properties are not deserialized (Or- i am not sure how to get them).

I can only seee the Name (tostring of DatabaseCopy from the list of arraylists).

Can any one please help me how to get all the deserialized properties of multi-valued results?

For more details you can look at: How to get DatabaseCopies from MailboxDatabase programmatically in C# (wrapped ExchangeMangementShell cmdlets in c#)?

Regards, Dreamer

Even from PowerShell, they are coming as plain strings.

PS C:\> $md.type
PS C:\> $md.DatabaseCopies.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ArrayList                                System.Object


PS C:\> $md.DatabaseCopies[0].gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

PS C:\> $md.gettype()
Method invocation failed because [Deserialized.Microsoft.Exchange.Data.Directory.SystemConfiguration.MailboxDatabase] doesn't contain a method named 'gettype'.

So, looks like I don't have any choice. I will looking for work around for my use case.

The cmdlets should return the PSObject or a list of PSObject.

I think the DatabaseCopies is not serialized and just set to plain string. So they cannot be deserialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM