Manage Exchange Management Powershell with .Net

Why am I getting this exception:

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

Relevant Code:

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();
+3
source share
1 answer

Use pipeline.Commands.AddScript(strScript)instead. An object Commandexpects only a cmdlet, for example. "New-StorageGroup" alone. Then you should use the returned collection of Commandobject Parametersto add parameters.

+2
source

Source: https://habr.com/ru/post/1755376/


All Articles