Automation

 View Only
Expand all | Collapse all

Unable to log Powershell output in the c# code.

  • 1.  Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 11:54 AM

    Hi all,

    Am using a powershell script to get the network configuration from the VMs.

    Am logging this output in log file as well as connecting this to file stream (to be used/logged in the c# code).

    C#code --> invoke cmds in powershell

    Logs               Logs

    (not seen)      (seen)

    Now the output from the script is received and logging works fine in powershell code but not in the C# code.

    Any clues ? Is there some formatting i need to watch out for ??

    Regards,

    Arvind S.



  • 2.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 12:00 PM

    Powershell uses different 'streams' for different types of output.

    Are you redirecting and capturing all the streams?

    For more details on these streams and redirection have a look at About Redirection



  • 3.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 12:03 PM

    Hi LucD

    Yes .All levels of log streams are redirected and mapped.

    Regards,

    Arvind S,



  • 4.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 12:09 PM

    Let me dive deeper.

    For instance

    e.g . $ret = Invoke-VMScript <>

    Using this $ret is ineffective in C# here . The output  of $ret when printed  has no use.

    If I run the command though, the script output is seen without any issues.

    However while using that script output of Invoke-VMScript command in variable and using it has no impact.

    Here even the logging of the variable in C# code is ineffective (not logged)

    Regards,

    Arvind S.



  • 5.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 12:13 PM

    Isn't that because the cmdlet is not actually producing output, but placing an object on the pipeline.

    What happens when you 'pipe' that to an Out-String for example?



  • 6.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 04:11 PM

    It is actually returning some value. The issue is intermittent.

    We were able to redirect to a file form script but not able to return it to the caller of the script which is c# code.

    We could see the output very well in the redirected file.



  • 7.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 01, 2018 05:24 PM

    I'm not a regula c# user, but isn't capturing PowerShell output basically something like this?

    PowerShell ps = PowerShell.Create().AddScript("c:\test.ps1", true);

    ps.Runspace = remoteRunspace;

    ICollection<PSObject> results = ps.Invoke();

    using (StringWriter sw = new StringWriter())

    {

      foreach (PSObject invoke in results)

       sw.WriteLine(invoke.ToString());

      outp = sw.ToString();

    }



  • 8.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 02, 2018 11:33 AM

    Thanks LucD.

    We too are tried similar to what you had described above.

    The output of the script being stored in a variable:

    Example:

    $output_str = invoke-vmscript <  ifconfig >

    If we log the output_str or access for parsing, the script proceeds further.

    However the stream which is being used by C# code is not getting anything from then on.

    Only If we comment out the logging and parsing, the stream is properly going back to C# code.

    Then, we modified the above line like:

    $output = invoke-vmscript <  ifconfig >

    $output_str = $output.ScriptOutput

    $f1 =  parse ($output_str)

    #Log $f1

    Then, the parsing is working and c# is able to read the stream properly.

    The problem is  if I uncomment the “Log $f1”, again we don’t see anything in the stream at C# end.

    Not sure what is happening here.

    Regards,

    Arvind S



  • 9.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 02, 2018 11:42 AM

    Like I said I'm not really that deep into c#.

    This looks more like a PowerShell - c# subject, it shouldn't make a lot of difference when you run a PowerCLI cmdlet in the PowerShell session.
    I assume you see the same issue when you run a PowerShell cmdlet this way?

    On a side-note, is this helping?

    c# - Get Powershell command's output when invoked through code - Stack Overflow

    Otherwise, I think your question would get a better answer in a Developer forum.

    Perhaps try raising your question in one of those.



  • 10.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 02, 2018 05:43 PM

    Oh ok. The link may not help us much .. Anyways thank you.



  • 11.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 05, 2018 11:37 AM

    I am bit uncertain which forum should I post. Do you have any suggestion on this? A reference to the forum would help me.



  • 12.  RE: Unable to log Powershell output in the c# code.

    Posted Mar 05, 2018 11:55 AM

    Looks like the VMware{code} forums merged with the VMTN forums.
    This is the overview of the Developer related forums on VMTN: Forums - VMware {code}

    Not sure which one on there would be the best fit for your question, perhaps vSphere Automation SDK for .Net Forum - VMware {code}