I have to list all databases in an exchange 2010 server with their mailboxes.
In Powershell I can run this,
Get-Mailbox -Server Exc2010 | Group-Object Database | Select-Object name,count
Name Count
---- -----
DB01 16
DB04 3
DB02 2
DB03 5
However if I try the same in C#,
//Running cmdlets remotely
WSManConnectionInfo wsConnectionInfo = new WSManConnectionInfo(new Uri("https://" + ExchangeSite + "/powershell"),
"http://schemas.microsoft.com/powershell/Microsoft.Exchange", getCredential());
wsConnectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
wsConnectionInfo.SkipCACheck = true;
wsConnectionInfo.SkipCNCheck = true;
wsConnectionInfo.SkipRevocationCheck = true;
rsRemoteRunspace = RunspaceFactory.CreateRunspace(wsConnectionInfo);
rsRemoteRunspace.Open();
Collection<PSObject> Result = null;
Pipeline pipeLine = rsRemoteRunspace.CreatePipeline();
Command myCommand = new Command("Get-MailboxDatabase");
myCommand.Parameters.Add("Server", "Exc2010");
Command myCommand2 = new Command("Group-Object");
myCommand2.Parameters.Add("Property", "Database");
pipeLine.Commands.Add(myCommand);
pipeLine.Commands.Add(myCommand2);
Result = pipeLine.Invoke();
I get,
The term 'Group-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
If I use Select-Object it works fine so I wonder why I’m getting that error using Group-Object.
Any help would be appreciated.
EDIT
If I run it using Select-Object, it works fine:
Command myCommand = new Command("Get-MailboxDatabase");
myCommand.Parameters.Add("Server", "Exc2010");
Command myCommand2 = new Command("Select-Object");
myCommand2.Parameters.Add("Property", "Name");
pipeLine.Commands.Add(myCommand);
pipeLine.Commands.Add(myCommand2);
Result = pipeLine.Invoke();
PSObject PSResult = Result.FirstOrDefault();
Console.WriteLine("DB Name: " + PSResult.Properties["Name"].Value.ToString());
I get,
DB Name: DB01
Are you running this in the remote management session provided by Exchange? If that’s the case, you can only run cmdlets provided by Exchange and Group-Object is not one of them.