我有這段代碼,我遠程發送PowerShell命令「日期」到我的交換服務器(server01),它的工作原理,我收到一個消息框的結果。C#連接到Powershell,但我需要連接到Exchange管理外殼
但是,如果我發送命令「Get-Mailbox」,則debbuger將停止並顯示以下錯誤:術語'Get-Mailbox'不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。
如果我去server01並運行powershell並執行「Get-Mailbox danielr」與我得到的錯誤相同。但是,Exchange命令行管理程序可以執行Get-Mailbox命令。
所以,我想我已經連接到Window Powershell cmd ..但是,要執行「Get-Mailbox danielr」和其他Exchange管理命令,我必須連接到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;
}
嗨。非常感謝你的幫助。 – 2014-09-02 16:57:33
你能幫我告訴我,我應該把代碼放在哪裏(在我之間)請! – 2014-09-02 17:15:32
好的,讓我試試。感謝邁克爾 – 2014-09-02 17:48:31