Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有.Net的Exchange管理Powershell_.net_Powershell - Fatal编程技术网

带有.Net的Exchange管理Powershell

带有.Net的Exchange管理Powershell,.net,powershell,.net,Powershell,为什么我会收到这个例外: System.Management.Automation.CommandNotFoundException: The term new-storagegroup.... 相关代码: RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; PSSnapInInfo info = rsConfig.AddPSSnap

为什么我会收到这个例外:

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();
RunspaceConfiguration rsConfig=RunspaceConfiguration.Create();
PSSnapInException snapInException=null;
PSSnapInInfo info=rsConfig.AddPSSnapIn(“Microsoft.Exchange.Management.PowerShell.Admin”,out snapInException);
运行空间myRunSpace=RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
//创建管道并向其提供脚本文本
Pipeline Pipeline=myRunSpace.CreatePipeline();
string strScript=“新建存储组-服务器KINGKONG”
+“-LogFolderPath c:\\rsg\\logs-Name RecoveryGroup-SystemFolderPath c:\\rsg\\data-Recovery”;
//使用要运行的cmdlet的名称创建命令类的实例
命令myCommand=新命令(strScript);
//将命令添加到管道的“命令”集合中。
pipeline.Commands.Add(myCommand);
收集结果=pipeline.Invoke();

使用
pipeline.Commands.AddScript(strScript)
命令
对象只需要Cmdlet,例如单独使用“New StorageGroup”。然后,您将使用返回的
命令
对象的
参数
集合来添加参数。

我尝试使用AddScript代替
新邮箱
命令,但这不起作用。我猜我必须一个接一个地检查所有的参数。有什么建议吗?我正在使用带有参数的命令(
New Mailbox
),并且我很接近于与他相同的错误(只是使用
New Mailbox
,而不是他正在运行的命令)。我使用了
pipeline.Commands.AddScript(script)
,但仍然出现了这个错误。我想我必须使用类似于
var cmd=new的命令(script,true)使用
cmd.Parameters.Add(“键”、“值”)-有什么建议吗。我还没有解决它,但这将是我的下一次尝试。您是否确保先加载exchange模块?您使用的是powershell 2还是3?我使用的是powershell 2,我首先使用的是以下命令
pipeline.Commands.AddScript(“add pssnapin Microsoft.Exchange.Management.powershell.E2010;”
然后我在做
newmailbox
像这样
var cmd=New命令(“newmailbox”,true)之后,我将所有内容都添加为参数(例如“-UserPrincipalName”之类的内容。看起来它没有注册管理单元。有什么想法吗?还有,有没有办法验证它正在运行哪个版本的powershell?