VMware Cloud Community
arvindathere
Enthusiast
Enthusiast

Unable to log Powershell output in the c# code.

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.

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

Hi LucD

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

Regards,

Arvind S,

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

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.

Reply
0 Kudos
LucD
Leadership
Leadership

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();

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

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

Reply
0 Kudos
LucD
Leadership
Leadership

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

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

Reply
0 Kudos
arvindathere
Enthusiast
Enthusiast

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

Reply
0 Kudos
LucD
Leadership
Leadership

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}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos