问题描述
我正在使用 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/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);
它显示的对象是 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 (将#修改为@)