Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When executing a particular script from Powershell command window, the execution takes about 2 minutes. Executing the same script from C# (Windows Service) using System.Management.Automation.Runspaces takes 5 to 6 minutes! Spawning new powershell.exe process from the service makes time drop to 2 minutes again.

I also noticed the CPU usage is higher when using Runspace.

Unsurprisingly, nearly all the time is spent in Pipeline.Invoke(), which doesn't make debugging the problem any easier.

The script I use is complex and does whole bunch of stuff, thus it's not easy to pin down the issue. How can I narrow down the problem?

Is there something obvious I may be missing?

share|improve this question
1  
I use System.Management.Automation.Runspaces a lot and never see such a performance loss. From the top of my head: is this the case that your script actually produces huge number of errors when it is run in the default or custom host associated with a runspace? Default/custom host may have something not implemented which may cause errors. Errors not necessarily stop processing but may affect performance. – Roman Kuzmin Apr 25 at 11:23
1  
runspaces are common to both methods - the difference I see is the security context. powershell.exe obviously runs as you, and the service will run as the service account (assuming it's not you.) Is this an AD or other issue? – x0n Apr 25 at 14:19
@RomanKuzmin Perfmon shows there is initially few peaks of exceptions (about 900 total!), however after few seconds there are no more. Don't think that is the cause of my problem. Still, how can I find when those exceptions reported by perfmon are thrown? 900 looks like an awful lot! – ya23 Apr 26 at 15:10
@x0n I have tried changing service user to the same (administrative) account as for the command window, or running powershell.exe with -noprofile. That didn't help... – ya23 Apr 26 at 15:11
@ya23, I was talking about PS errors, they are not always correspond to classic exceptions. Inspect the content of the system variable $Error in the end of your script (e.g. dump it to a file). If you do not see anything, OK, this possible cause is eliminated. But if you see something it may give you further ideas. – Roman Kuzmin Apr 26 at 16:17

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.