1

サーバー上のメールボックスの総数を取得するために、C# を使用して次の交換管理シェル コマンドレットを実行しようとしています。

コマンドレット:-

Get-mailbox -resultsize unlimited 

私のコードスニペットは次のとおりです

        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 
        }    

上記のコードは、目的の結果、つまりメールボックスの総数を提供します。しかし、すべてのメールボックスのいくつかのプロパティを選択するにはどうすればよいですか?つまり、次のコマンドレットを実行するにはどうすればよいですか?

コマンドレット:

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

上記のコマンドレットを実行する方法を教えてください...ありがとう

4

1 に答える 1

1

select-object で指定するいくつかのプロパティをリストする必要があります。これは以下のように行うことができます

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);
}

また、where-object の代わりに Filter を使用することをお勧めします。これは以下のように実行できます。

command.Parameters.Add("Filter", "{forwardingaddress -ne $null}");
于 2014-09-03T06:35:03.647 に答える