Reputation: 81
I have this code, I send remotely a powershell command "date" to my exchange server (server01) and it works, I'm receiving a result in a messagebox.
but, if I send the command "Get-Mailbox" the debbugers stops with this error: The term 'Get-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program.
If I go to the server01 and runs powershell and execute "Get-Mailbox danielr" is the same error that I get. But Exchange Management Shell execute the Get-Mailbox command fine.
So, I guess I'm connected to the Window Powershell cmd..but, to execute the "Get-Mailbox danielr" and others Exchange Management commands I have to connect to the Exchange Management Shell.
What do I need to change to make it works? (to connect to the exchange management shell, not to powershell. THANKS A LOT!!
public void CreateMailBoxExchange()
{
string loginName = "administrator";
string loginPassword = "123456";
SecureString ssLoginPassword = new SecureString();
foreach (char x in loginPassword)
ssLoginPassword.AppendChar(x);
PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);
// Set the connection Info
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", remoteMachineCredentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
//connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("date");
//command.AddCommand("Get-Mailbox");
//command.AddParameter("Identity", "danielr");
powershell.Commands = command;
try
{
// open the remote runspace
runspace.Open();
// associate the runspace with powershell
powershell.Runspace = runspace;
// invoke the powershell to obtain the results
powershell.Invoke();
var results = powershell.Invoke();
runspace.Close();
foreach (PSObject obj in results)
{
StringBuilder stringBuilder = new StringBuilder();
//stringBuilder.AppendLine(obj.ToString());
MessageBox.Show(obj.ToString());
}
}
finally
{
// dispose the runspace and enable garbage collection
runspace.Dispose();
runspace = null;
// Finally dispose the powershell and set all variables to null to free
// up any resources.
powershell.Dispose();
powershell = null;
}
Upvotes: 2
Views: 2508
Reputation: 47613
If you are remote from the Exchange Server which your code suggests then you'd have to use something like:
string loginName = "administrator";
string loginPassword = "123456";
SecureString ssLoginPassword = new SecureString();
foreach (char x in loginPassword)
ssLoginPassword.AppendChar(x);
PSCredential remoteMachineCredentials = new PSCredential(loginName, ssLoginPassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://server01:5985/Powershell"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", remoteMachineCredentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
Upvotes: 0
Reputation: 2123
Depending on which version of Exchange you are running, you may need to execute the following code:
For Exch 2007
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin
For Exch 2010
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010
For Exch 2013 (try this)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Upvotes: 1
Reputation: 905
You should be able to just include the Exchange Management snapin before trying the Get-Mailbox command. Here's how I do that in PowerShell:
Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"
Upvotes: 0