在Exchange 2007中的命令命令管理單元:什麼是這行代碼是用來加載Exchange Poweshell從C#代碼中訪問Exchange管理工具在Exchange 2010中
PSSnapInInfo info = rsConfig.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.Admin",
out snapInException);
然而,這並不在Exchange存在2010年,我正試圖找出如何從C#代碼訪問Exchange Powershell命令。 Microsoft.Exchange.Management.PowerShell.Admin在Exchange Server的任何位置都不存在,我無法在Google上找到有關等效代碼行的任何內容。
如何從Exchange 2010中的C#代碼訪問Exchange管理工具?
下面是我參考完整的代碼,所有的工作,直到我添加的代碼行:
//Creating and Opening a Runspace
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.Admin",
out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
//How Do I Run a Cmdlet?
//create a new instance of the Pipeline class
Pipeline pipeLine = myRunSpace.CreatePipeline();
//create an instance of the Command class
// by using the name of the cmdlet that you want to run
Command myCommand = new Command(txtCommand.Text);
//add the command to the Commands collection of the pipeline
pipeLine.Commands.Add(myCommand);
Collection<PSObject> commandResults = pipeLine.Invoke();
// iterate through the commandResults collection
// and get the name of each cmdlet
txtResult.Text = "start ....";
foreach (PSObject cmdlet in commandResults)
{
string cmdletName = cmdlet.Properties["Name"].Value.ToString();
System.Diagnostics.Debug.Print(cmdletName);
txtResult.Text += "cmdletName: " + cmdletName;
}
txtResult.Text += ".... end";
請記住Runspace和Pipeline實現了IDisposable,因此應該在使用後丟棄。 – 2010-12-03 09:55:26