У меня есть этот код, я отправляю удаленно команду powershell "date" на мой сервер обмена (server01), и он работает, я получаю результат в виде сообщения.
но, если я отправлю команду "Get-Mailbox", debbugers перестанут с этой ошибкой: термин "Get-Mailbox" не распознается как имя командлета, функции, файла сценария или операционной программы.
Если я перейду на сервер01 и запускаю powershell и выполняю "Get-Mailbox danielr" - это та же самая ошибка, которую я получаю. Но Exchange Management Shell отлично выполняет команду Get-Mailbox.
Итак, я думаю, что я подключен к Window Powershell cmd..but, чтобы выполнить "Get-Mailbox danielr" и другие команды управления Exchange, которые мне нужно подключиться к командной консоли Exchange.
Что мне нужно изменить, чтобы оно работало? (чтобы подключиться к оболочке управления обменом, а не к powershell. СПАСИБО МНОГО!
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;
}
В зависимости от используемой версии Exchange вам может потребоваться выполнить следующий код:
Для Exch 2007
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.Admin
Для Exch 2010
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010
Для Exch 2013 (попробуйте это)
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
Вы должны просто включить оснастку управления Exchange, прежде чем пытаться выполнить команду Get-Mailbox. Вот как я это делаю в PowerShell:
Add-PSSnapin "Microsoft.Exchange.Management.PowerShell.Admin"
Если вы удалены от Exchange Server, который предлагает ваш код, вам нужно будет использовать что-то вроде:
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);