简体   繁体   English

C#Powershell管道输出/响应

[英]C# Powershell Pipeline output/response

currently I'm developing ac# web service which is a "man in the middle". 目前,我正在开发ac#Web服务,它是“中间人”。 The Client can call a method "executeScript(String script)" and my web services invokes the script. 客户端可以调用方法“ executeScript(String script)”,而我的Web服务将调用该脚本。

Currently the web service doesn't know which scripts the client will execute (normally exchange scripts). 当前,Web服务不知道客户端将执行哪些脚本(通常交换脚本)。 How I read the output correctly? 如何正确读取输出? eg Get Process or something else. 例如获取流程或其他东西。

My Error Reading works allready. 我的错误阅读已经可以使用了。 Example Script: "Get-Mailbox xxxxx" 示例脚本:“ Get-Mailbox xxxxx”

if (pl.Error.Count > 0)
 {
 //error in pipeline
 var errorList = pl.Error.Read() as Collection<ErrorRecord>;
 if (errorList != null)
 {
     foreach (ErrorRecord er in errorList)
     {
     logger.Error(er.Exception.Message);
     ExceptionObject eo = new ExceptionObject();
     eo.message = er.Exception.Message;
     eo.source = er.Exception.Source;
     eo.stackTrace = er.Exception.StackTrace;
     errors.Add(eo);
     }
  }
  if (pso != null)
  {
      foreach (PSObject o in pso)
      {
      logger.Info("size: " + o.Properties.Count());
      logger.Info(o.ToString());
      foreach (PSPropertyInfo psprop in o.Properties)
       {
           sb.AppendLine("Name: " + psprop.Name + ", Value: " + psprop.Value + ", MemberType: " + psprop.MemberType);
        }
   }
   logger.Info(pso.ToString());
   pro.result = sb.ToString();
}

logger.Info(o.ToString()); logger.Info(o.ToString()); -> Get-Mailbox xxxxx ->获取邮箱xxxxx

The propeties: "Name: Length, Value: 19, MemberType: Property" 属性:“名称:长度,值:19,成员类型:属性”

Thats not the correct output. 那不是正确的输出。 Is there a way, to get the response as string as it is in the orginal powershell console? 有没有办法像原始Powershell控制台一样以字符串形式获取响应? (Dynamically on every script!) (动态地在每个脚本上!)

Please take a look at Windows Remote Management (WinRM) , this allows you to remotely execute PowerShell scripts. 请查看Windows远程管理(WinRM) ,它使您可以远程执行PowerShell脚本。

Using WinRM will most likely be a better solution than building a web service, as you can limit what a person can do remotely. 与构建Web服务相比,使用WinRM最有可能是更好的解决方案,因为您可以限制一个人可以远程执行的操作。 Your web service will probably contain security holes, allowing unwanted code execution with limited logging of who performed what. 您的Web服务可能会包含安全漏洞,从而允许在执行人员限制登录的情况下执行有害代码。

PSObject is a wrapper around the actual object that was returned. PSObject是返回的实际对象的包装。 You want to use PSObject.BaseObject . 您要使用PSObject.BaseObject

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

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