简体   繁体   中英

Exchange Management Powershell with .Net

Why am I receiving 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();

Use pipeline.Commands.AddScript(strScript) instead. A Command object expects only the Cmdlet, eg "New-StorageGroup" alone. You would then use the returned Command object's Parameters collection to add the parameters.

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