[英]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.