2010-07-19 23 views
3

为什么我收到此异常:Exchange管理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(); 

回答

2

使用pipeline.Commands.AddScript(strScript)代替。 A Command对象仅期望Cmdlet,例如“新建StorageGroup”。然后,您将使用返回的Command对象的Parameters集合来添加参数。

+0

我试着使用AddScript来代替'New-Mailbox'命令,这是行不通的。我猜测我将不得不一一浏览所有参数。有什么建议吗? – BRogers 2013-06-14 00:50:01

+0

你是什么意思“不起作用?”你可以说得更详细点吗? – x0n 2013-06-14 14:53:04

+0

我正在使用带有参数('New-Mailbox')的命令,而且我正在接近与他相同的错误(只是使用'New-Mailbox'而不是他正在运行的命令)。我使用'pipeline.Commands.AddScript(脚本)',我仍然得到这个错误。我想我必须使用'var cmd = new Command(script,true);'use'cmd.Parameters.Add(“key”,“value”);' - 对此有任何建议。我还没有解决,但那将是我的下一次尝试。 – BRogers 2013-06-14 16:11:19

相关问题