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?