Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.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,如何从以下命令获取返回代码: RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); PSSnapInException snapInException = null; PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException); Runspace myRu

如何从以下命令获取返回代码:

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";

    //Add the command to the Commands collection of the pipeline.
    pipeline.Commands.AddScript(strScript)

    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”;
//将命令添加到管道的“命令”集合中。
pipeline.Commands.AddScript(strScript)
收集结果=pipeline.Invoke();

您可以通过获取$的值来查询最后一个命令(布尔)的执行状态?变量,例如:

bool succeeded = myRunspace.SessionStateProxy.GetVariable("?");

为什么它总是返回true,即使它无法创建已存在的恢复组?PowerShell中的执行状态有点奇怪,IIRC表示发生了终止错误,例如,
gci xyzy;$返回True,即使文件不存在。您可以通过多种方式捕获非终止错误,但最直接的方法是使用普遍存在的参数-ErrorVariable(-ev)<代码>$status=$null;gci xyzzy-电动汽车状态$状态
-在这种情况下,$status将保存非终止错误信息。