简体   繁体   English

如何获取包含反序列化对象列表的Cmdlet结果的属性(多值属性)(从C#执行Powershell cmdlet)

[英]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. 我正在使用c#执行PowerShell cmdlet并分析cmdlet结果(Collection []。Properties [“ PropertyName”]。value)以获取所需的值。

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. 但是PowerShell只是将它们转换为对象的ArrayList,而我无法获取它们的所有属性。

For ex: Get-MailboxDatabase of Exchange Management Shell cmdlet returns list of mailboxdatabase on exchange server (Collection). 例如:Exchange命令行管理程序cmdlet的Get-MailboxDatabase返回Exchange服务器(集合)上的邮箱数据库列表。 I can able to get properties like name, server etc... from PSObject.Properties["PropertyName"].Value. 我可以从PSObject.Properties [“ PropertyName”]。Value获取属性,例如名称,服务器等。

It also contains a list of database copies (something like below) 它还包含数据库副本的列表(如下所示)

class MailboxDatabase { DatabaseCopy[] DatabaseCopies; 类MailboxDatabase {DatabaseCopy [] DatabaseCopies; } }

the PSObject.Properties["DatabaseCopies"].Value is nothing but an arrayList of objects. PSObject.Properties [“ DatabaseCopies”]。Value只是对象的arrayList。

But all the DatabaseCopy properties are not deserialized (Or- i am not sure how to get them). 但是所有的DatabaseCopy属性都不会反序列化(或者-我不确定如何获取它们)。

I can only seee the Name (tostring of DatabaseCopy from the list of arraylists). 我只能看到名称(从arraylists列表中的DatabaseCopy的字符串)。

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#)? 有关更多详细信息,请查看: 如何在C#中以编程方式从MailboxDatabase获取数据库副本(在C#中包装ExchangeMangementShell cmdlet)?

Regards, Dreamer 问候,梦想家

Even from PowerShell, they are coming as plain strings. 即使从PowerShell,它们也以纯字符串形式出现。

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. cmdlet应该返回PSObject或PSObject列表。

I think the DatabaseCopies is not serialized and just set to plain string. 我认为DatabaseCopies没有序列化,只是设置为纯字符串。 So they cannot be deserialized. 因此它们不能反序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在c#中的Powershell cmdlet之间共享属性和注释? - Is it possible to share properties and comments between Powershell cmdlets in c#? 在C#中执行Powershell Cmdlet代码 - Executing Powershell Cmdlets Code in C# Powershell - 从C#Cmdlet获取变量 - Powershell - Get Variable from C# Cmdlet 从C#使用Invoke-SqlCmd cmdlet执行PowerShell脚本 - Executing PowerShell Script with Invoke-SqlCmd cmdlet from C# 使用反射从动态反序列化 Json 对象获取属性 - .NET Core 3.1 C# - Get Properties From Dynamic Deserialized Json Object with Reflection - .NET Core 3.1 C# 在C#中创建Powershell CmdLets-为什么不使用自动实现的属性 - Creating Powershell CmdLets in C# - why not use auto-implemented properties 如何引用从C#通用处理程序中的javascript接收的反序列化对象属性? - How to reference deserialized object properties received from javascript in C# generic handler? 如何从C#中的列表中获取属性键名? - How to Get Properties key names from a List in c#? 尝试使用 C# 中编写的 Cmdlet 获取 PowerShell 中 IAsyncEnumerable 方法的结果 - Trying to Get Results of an IAsyncEnumerable Method in PowerShell with a Cmdlet Written in C# 如何在 Visual Studio 中从 C# 调用 PowerShell cmdlet - How to call PowerShell cmdlets from C# in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM