与.Net交换管理Powershell

时间:2010-07-19 20:09:19

标签: .net powershell

为什么我收到此例外:

System.Management.Automation.CommandNotFoundException: The term new-storagegroup....

相关代码:

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();

//Create pipeline and feed it the script text
Pipeline pipeline = myRunSpace.CreatePipeline();

string strScript = "new-storagegroup -Server KINGKONG"
    + " -LogFolderPath c:\\rsg\\logs -Name RecoveryGroup -SystemFolderPath c:\\rsg\\data -Recovery";

//Create an instance of the Command class by using the name of the cmdlet that you want to run
Command myCommand = new Command(strScript);

//Add the command to the Commands collection of the pipeline.
pipeline.Commands.Add(myCommand);

Collection<PSObject> results = pipeline.Invoke();

1 个答案:

答案 0 :(得分:2)

请改用pipeline.Commands.AddScript(strScript)Command对象只需要Cmdlet,例如仅“New-StorageGroup”。然后,您将使用返回的Command对象的Parameters集合来添加参数。