简体   繁体   中英

Executing cmdlet Get-ClusterGroup from C#

Hi I'm trying to execute the Get-ClusterGroup cmdlet from C# 4.0. I've used the following code

InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "failoverclusters"});

Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss);
myRunSpace.Open();

Pipeline pipeLine = myRunSpace.CreatePipeline();

Command myCommand = new Command("Get-ClusterGroup");
pipeLine.Commands.Add(myCommand);

Console.WriteLine("Invoking Command");
Collection commandResult = pipeLine.Invoke();

foreach (PSObject resultObject in commandResult)
{
Console.WriteLine(resultObject.ToString());
}
myRunSpace.Close();

But getting the following error

The term 'Get-ClusterGroup' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

It will be great if someone can show me the where I'm missing the logic or where is the problem in my code

Get-ClusterGroup is a Powershell Commandlet, not an .exe file. You can invoke Powershell commands from .NET using the System.Management.Automation.PowerShell class, as described on MSDN here: http://msdn.microsoft.com/en-us/library/system.management.automation.powershell(v=vs.85).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM