Dans Exchange 2007, cette ligne de code est utilisé pour charger les commandes Poweshell Echange snapin:Quelle est la commande pour accéder aux outils de gestion Exchange à partir du code C# dans Exchange 2010
PSSnapInInfo info = rsConfig.AddPSSnapIn(
"Microsoft.Exchange.Management.PowerShell.Admin",
out snapInException);
Cependant, cela n'existe pas dans Exchange 2010 et je tire mes cheveux en essayant de trouver comment accéder aux commandes Exchange Powershell à partir du code C#. Microsoft.Exchange.Management.PowerShell.Admin n'existe nulle part sur le serveur Exchange et je ne trouve rien sur Google qui parle d'une ligne de code équivalente.
Comment puis-je accéder aux outils de gestion Exchange à partir du code C# dans Exchange 2010?
Ci-dessous est mon code complet pour référence, tout cela fonctionne jusqu'à ce que j'ajouter la ligne de code:
//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";
rappeler que runspace et pipeline met en œuvre IDisposable et par conséquent doivent être éliminés après utilisation. –