简体   繁体   English

C#中的Powershell返回命令输出

[英]Powershell in C# Return Command Output

I'm new to bringing C# and Powershell together, but am hoping to create a web page that leverages Powershell in the back end. 我是将C#和Powershell集成在一起的新手,但是希望创建一个在后端利用Powershell的网页。 I realize that what I am doing can be done solely with C#, but would like to figure this out for other applications. 我意识到我在做什么只能用C#来完成,但是想在其他应用程序中解决。

Essentially, I am taking the name of a new web application from a web form and getting the authenticated user's username for physical path mapping. 本质上,我是从Web表单中获取新Web应用程序的名称,并获取经过身份验证的用户的用户名来进行物理路径映射。

My Powershell code works correctly (even when copying it from Pipeline.Commands[0] directly), but it does not appear to do anything when I run it. 我的Powershell代码正常工作(即使直接从Pipeline.Commands [0]复制时也是如此),但是运行它时似乎没有任何作用。 I get parameter errors in the result variable if I force one (ex: make -physicalpath a non-existent path), but with all parameters correct, the variable result only contains one blank item. 如果我强制执行一个结果(例如,使-physicalpath为不存在的路径),则会在结果变量中出现参数错误,但是在所有参数正确的情况下,变量结果仅包含一个空白项。

I see many similar questions to this one, but do not see s definitive answer. 我看到许多与此问题类似的问题,但没有看到明确的答案。

Does this sound like a C# or IIS Powershell module issue? 这听起来像C#或IIS Powershell模块问题吗? Any ideas how I get more information returned from my command? 有什么想法可以让我从命令中返回更多信息吗?

protected void Button1_Click(object sender, System.EventArgs e)
{
  String username = getUser();
  String physicalPath = "S:\\WebSites\\" + username + "\\public_html\\" + TextBox1.Text; 

  // Create Powershell Runspace
  Runspace runspace = RunspaceFactory.CreateRunspace();

  runspace.Open();

  // Create pipeline and add commands
  Pipeline pipeline = runspace.CreatePipeline();
  pipeline.Commands.AddScript(
    "Import-Module WebAdministration; set-psdebug -trace 1; " +

    "New-WebApplication -Site MySite" +
    " -Name " + TextBox1.Text +
    " -PhysicalPath " + physicalPath +
    " -ApplicationPool WebSites -Verbose -force");

  pipeline.Commands.Add("Out-String");

  // Execute Script
  Collection<PSObject> results = new Collection<PSObject>();
  try
  {
    results = pipeline.Invoke();
  }
  catch (Exception ex)
  {
    results.Add(new PSObject((object)ex.Message));
  }

  // Close runspace
  runspace.Close();

  //Script results to string
  StringBuilder stringBuilder = new StringBuilder();
  foreach (PSObject obj in results)
  {
    stringBuilder.AppendLine(obj.ToString());
  }

}

Thanks! 谢谢!

That looks like it should work. 看起来应该可以。 You should check the error stream and see if there are messages there (ie: "Destination element already exists"). 您应该检查错误流,看看那里是否有消息(即:“目标元素已存在”)。

I'd also suggest you consider using the PowerShell 2 APIs as in this blog post: 我也建议您考虑使用PowerShell 2 API,如本博文所述:

http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/ http://huddledmasses.org/how-to-invoke-powershell-and-use-the-results-from-csharp/

If you're using that, you can check the ps.Streams.Error to make sure it's empty... 如果您正在使用它,则可以检查ps.Streams.Error以确保其为空...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM