代码之家  ›  专栏  ›  技术社区  ›  Serge van den Oever

在运行空间中从c#调用PowerShell脚本并获取发生错误的行号

  •  4
  • Serge van den Oever  · 技术社区  · 16 年前

    我的托管代码如下所示:

            public Exception Execute(string scriptcode, Hashtable variables)
        {
            Runspace runspace = null;
            Pipeline pipeline = null;
            PipelineStateInfo info = null;
    
            try
            {
                // Make our output window active
                Logger.ShowOutputWindow();
    
                // Create the runspace and stuff.
                runspace = RunspaceFactory.CreateRunspace(host);
                pipeline = runspace.CreatePipeline();
    
                runspace.Open();
    
                // Add our scriptcode to the pipeline
                pipeline.Commands.Add(new Command(scriptcode, true));
    
                // We don't want to get PSObjects out of the pipeline, output result as string in default way
                pipeline.Commands.Add(new Command("Out-Default", true));
    
                // Set up global variables
                FillVariables(runspace, variables);
    
                SetUpOutputStreams(pipeline);
    
    
                // Run the scriptcode
                Collection<PSObject> psOutput = pipeline.Invoke();
    
                // Did it complete ok?
                info = pipeline.PipelineStateInfo;
                if (info.State != PipelineState.Completed)
                {
                    return info.Reason;
                }
                else
                {
                    return null; // succesful!
                }
            }
            catch (Exception ex)
            {
                return ex;
            }
        }
    

    首先,我将脚本放在scriptcode变量中,现在我首先将代码写入一个临时的.ps1文件,以便报告该文件中的行号。但是我找不到如何在文件中执行代码,所以在出现错误时可以检索文件名和行号。

    有什么想法吗?

    1 回复  |  直到 16 年前
        1
  •  7
  •   James Pogran    16 年前

    这会让你在正确的地方:

    //invoke pipeline
    collection = pipeline.Invoke();
    
    // check for errors (non-terminating)
    if (pipeline.Error.Count > 0)
    {
      //iterate over Error PipeLine until end
      while (!pipeline.Error.EndOfPipeline)
      {
        //read one PSObject off the pipeline
        var value = pipeline.Error.Read() as PSObject;
        if (value != null)
        {
          //get the ErrorRecord
          var r = value.BaseObject as ErrorRecord;
          if (r != null)
          {
            //build whatever kind of message your want
            builder.AppendLine(r.InvocationInfo.MyCommand.Name + " : " + r.Exception.Message);
            builder.AppendLine(r.InvocationInfo.PositionMessage);
            builder.AppendLine(string.Format("+ CategoryInfo: {0}", r.CategoryInfo));
            builder.AppendLine(
            string.Format("+ FullyQualifiedErrorId: {0}", r.FullyQualifiedErrorId));
          }
        }
      }
      return builder.ToString();
    }
    

    更新:

    除了我在评论中写的信息,还请看这本书: Professional PowerShell Programming

    当我第一次开始为PowerShell运行时编程主机时,我发现这本书非常有价值。它是由一些PowerShell开发人员编写的。