简体   繁体   English

使用C#执行交换管理Shell cmdlet吗?

[英]Execute exchange management shell cmdlets using C#?

I am trying to execute the following exchange management shell cmdlets using C# in order to get total number of mailbox on the server. 我试图使用C#执行以下交换管理Shell cmdlet,以获取服务器上的邮箱总数。

cmdlets:- 的cmdlet: -

Get-mailbox -resultsize unlimited 

My code snippet is as following 我的代码段如下

        PSCredential credential = new PSCredential("Administrator", securePassword); // the password must be of type SecureString
        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(connectTo,schemaURI, credential);
        connectionInfo.MaximumConnectionRedirectionCount = 5;
        connectionInfo.SkipCACheck = true;
        connectionInfo.SkipCNCheck = true;

        try
        {
            Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo);
            remoteRunspace.Open();
            var command = new Command("Get-mailbox");
            command.Parameters.Add("resultsize", "unlimited");
            var pipeline = remoteRunspace.CreatePipeline();
            pipeline.Commands.Add(command);
            // Execute the command
            var results = pipeline.Invoke();
            MessageBox.Show(results.Count.ToString());
            remoteRunspace.Dispose();
        }
        catch (Exception ex)
        {
            //Handle error 
        }    

The above code gives the desired result ie total number of mailboxes.But how can i select some properties of all the mailboxes ie how can i execute the following cmdlets 上面的代码给出了所需的结果,即邮箱总数。但是如何选择所有邮箱的某些属性,即如何执行以下cmdlet

cmdlets: 的cmdlet:

Get-mailbox | select-object DisplayName, PrimarySmtpAddress, ForwardingAddress, alias, identity, legacyexchangeDN | where-object {$_.ForwardingAddress -ne $Null}

Please guid, how can i execute the above given cmdlets... Thank you 请指导,我如何执行上述给定的cmdlet ...谢谢

You need to list some properties that you specify in select-object, you can do this like below 您需要列出在选择对象中指定的一些属性,您可以像下面这样进行操作

var results = pipeline.Invoke();
foreach (PSObject result in results) {
Console.WriteLine(result.Properties["DisplayName"].Value);
Console.WriteLine(result.Properties["PrimarySmtpAddress"].Value);
Console.WriteLine(result.Properties["ForwardingAddress"].Value);
Console.WriteLine(result.Properties["alias"].Value);
Console.WriteLine(result.Properties["identity"].Value);
Console.WriteLine(result.Properties["legacyexchangeDN "].Value);
}

And it is good to use Filter instead of where-object, this can be run as below 而且最好使用Filter代替where-object,这可以如下运行

command.Parameters.Add("Filter", "{forwardingaddress -ne $null}");

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

相关问题 仅使用1个运行空间,使用C#和ASP.NET执行多个在线交换cmdlet - Execute multiple exchange online cmdlets using C# , ASP.NET with only 1 runspace 从C#交换cmdlet - Exchange cmdlets from c# 从C#调用Exchange命令行管理程序 - Invoking Exchange Management Shell from C# 使用 C# 的 Visual Studio Exchange 命令行管理程序 Add-ContentFilterPhrase cmdlet - Visual studio Exchange Management Shell Add-ContentFilterPhrase cmdlet using C# C#已连接到Powershell,但是我需要连接到Exchange命令行管理程序 - C# Connected to Powershell, but I need connect to Exchange Management Shell 从C#调用Exchange命令行管理程序时出现连接错误 - connection error when calling Exchange Management Shell from c# 用C#代码执行Service Fabric cmdlet - Execute Service fabric cmdlets in c# code 为什么相同的脚本在 Exchange Management Shell 2010 中有效,但不能通过 C# 与 Powershell 和 Exchange Connection - Why does the same script work in Exchange Management Shell 2010 but not through C# with Powershell and Exchange Connection 如何从C#在Exchange命令行管理程序中启动脚本? - How can I start a script in Exchange Management Shell from C#? 使用RunspacePool的C#PowerShell - 如何导入像Get-Mailbox这样的Exchange Cmdlet? - C# PowerShell with RunspacePool - How to import Exchange Cmdlets like Get-Mailbox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM