简体   繁体   中英

C# Powershell Pipeline output/response

currently I'm developing ac# web service which is a "man in the middle". The Client can call a method "executeScript(String script)" and my web services invokes the script.

Currently the web service doesn't know which scripts the client will execute (normally exchange scripts). How I read the output correctly? eg Get Process or something else.

My Error Reading works allready. Example Script: "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()); -> Get-Mailbox xxxxx

The propeties: "Name: Length, Value: 19, MemberType: Property"

Thats not the correct output. Is there a way, to get the response as string as it is in the orginal powershell console? (Dynamically on every script!)

Please take a look at Windows Remote Management (WinRM) , this allows you to remotely execute PowerShell scripts.

Using WinRM will most likely be a better solution than building a web service, as you can limit what a person can do remotely. Your web service will probably contain security holes, allowing unwanted code execution with limited logging of who performed what.

PSObject is a wrapper around the actual object that was returned. You want to use PSObject.BaseObject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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