0

currently I'm developing a c# 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? e.g. 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!)

Danny.
  • 363
  • 1
  • 4
  • 23

2 Answers2

0

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.

Alex
  • 391
  • 2
  • 9
  • I don't care on security yet. Important is functionality. As far as I see at my first look, I will get an Collection of PSObject when I work with WinRM. There is again my question? How I handle the response? How I create the same output as it's in powershell console!??? Sry - doesn't answer my question! – Danny. Sep 09 '13 at 09:26
  • 1
    I'm trying to help you here. Exposing an endpoint where callers can execute any script is highly dangerous. If you still want to do this (I strongly recommend you *not* to) and to get the printed output, you should get the StdOut (StandardOut) stream and return its contents to the caller of your service. You'll want to use Pipeline.Output for that, see http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.pipeline.output(v=vs.85).aspx – Alex Sep 09 '13 at 15:17
0

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

Skrymsli
  • 5,173
  • 7
  • 34
  • 36