微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Exchange 命令行管理程序在 c# 中获取正确的 cmdlet 返回对象

如何解决使用 Exchange 命令行管理程序在 c# 中获取正确的 cmdlet 返回对象

我正在使用 c# 和 powershell 访问 Exchange 命令行管理程序。这工作正常。我可以通过属性获得 PSObjects 作为回报......但我没有成功的是获得用对象定义的 C# 对象。例如:

get-mailBox | get-mailBoxpermission

在管理 shell 中给我一长串用户和权限列表;很好。

根据微软文档,get-mailBoxpermission 返回一个 MailBoxAcePresentationObject

https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/ff326162(v=exchg.140)

对象文档在这里https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/ff320324(v=exchg.140)

并且该程序集位于 Microsoft.Exchange.Management(在 Microsoft.Exchange.Management.dll 中)

所以我也在我的应用程序中引用了dll文件;现在可以在我的代码中访问根据文档的返回对象。 Intellisense 识别出 MailBoxAcePresentationObject;

当我在 c# 中使用以下内容时:

                    powershell = PowerShell.Create();
                    command = new PSCommand();
                    command.AddCommand("Import-PSSession");
                    command.AddParameter("Session",tempResultGetSession[0]);
                    powershell.Commands = command;
                    powershell.Runspace = runspace;

                    var tempResultImportSession = powershell.Invoke();


                    var powershell2 = PowerShell.Create();
                    powershell2.AddCommand("get-mailBox");
                    powershell2.AddStatement().AddCommand("get-mailBoxpermission");
                    powershell2.Runspace = runspace;

                    var tempMailBoxPermissionPSObjects = powershell2.Invoke();

tempMailBoxPermissionPSObjects 现在包含很多变量。但不是来自 MailBoxAcePresentationObject 的变量。

我尝试将 PSObjects 转换为 MailBoxAcePresentationObject,但没有成功。我试图将 PSObject.BaSEObject 和 PSObject.ImmediateBaSEObject 转换为 MailBoxAcePresentationObject,但也没有成功;

当我遍历 PSObject 的类型名时,我得到以下信息:

typename: Deserialized.Microsoft.Exchange.Data.Directory.Management.MailBox
typename: Deserialized.Microsoft.Exchange.Data.Directory.Management.mailenabledOrgPerson
typename: Deserialized.Microsoft.Exchange.Data.Directory.Management.mailenabledRecipient
typename: Deserialized.Microsoft.Exchange.Data.Directory.Management.ADPresentationObject
typename: Deserialized.Microsoft.Exchange.Data.Directory.Adobject
typename: Deserialized.Microsoft.Exchange.Data.Directory.ADRawEntry
typename: Deserialized.Microsoft.Exchange.Data.ConfigurableObject
typename: Deserialized.System.Object

例如,当我执行不同的 powershell 命令时:

            var ps = PowerShell.Create()
             .AddCommand("Get-physicaldisk");
            ps.AddStatement().AddCommand("Get-StorageReliabilityCounter");
            var tempResult = await PowerShellManager.RunPowerShellCommand(ps);

我在调用后设置了一个断点,我得到以下结果:

enter image description here

显示的对象是 MSFT_Physicaldisk。文档在这里

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/msft-physicaldisk

所以我的问题是。我如何投射/到达隐藏在 de PSObjects 中的对象...在我的第一个示例中有没有办法以 MailBoxAcePresentationObject 结束?

由于回答我自己的问题会出现错误,我将解决方案放在这里

我找到了解决方案。而不是在执行 Import-PSSession 后使用 PowerShell.Create(),为了使用自定义命令,我使用 runspace.CreatePipeLine();然后 pipline.Commands.AddScript();

作为回报,我按照 Microsoft 文档中的描述准确地取回了对象。仅作为 PSObjects,但属性等于 MailBoxAcePresentationObject

然后我可以循环每个 PSObject 的属性和值;

                            powershellCreateExchangeSession = PowerShell.Create();
                            command = new PSCommand();
                            command.AddCommand("Import-PSSession");
                            command.AddParameter("Session",tempResultGetSession[0]);
                            powershellCreateExchangeSession.Commands = command;
                            powershellCreateExchangeSession.Runspace = runspace;

                            var tempResultImportSession = powershellCreateExchangeSession.Invoke();
                 
                            Pipeline pipeline = runspace.CreatePipeline();

                            String customScriptText = "Get-mailBox | Get-MailBoxPermission | Select User,Identity,@{Name=\"AccessRights\";Expression={$_.AccessRights}}";

                            pipeline.Commands.AddScript(customScriptText);

                            // execute the script
                            Collection<PSObject> results = pipeline.Invoke();

                            // Now loop through the results 

                            foreach (PSObject obj in results)
                            {
                               //code
                            }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。