views:

229

answers:

4

I've a PS script which I use to keep track of my VMWare ESX servers. I need to run it as a service so that I'm not permanently logged on. Unfortunately, the script runs more slowly if I use a runspace inside a service rather than just running the script through the powershell console. It's taking 2-5 minutes to make calls to the VMWare web service instead of a second or so.

Is there some sort of magic I should be using when I invoke the runspace?

+1  A: 

You can run the script as a scheduled task.

Shay Levy
I could - but since it needs to run continously (it's a performance monitoring tool), I will put that in my back pocket as a last-resort evil hack.
A: 

Does the service authenticate as a user with the same rights you do? I suspect something is timing out when it runs as the service, which you don't see when you run the script yourself.

Bill Wert
A: 

The script in question runs equally slowly whether it runs in a service using its usual service account, or if I run as myself in a very basic console app (create runspace, add script, execute).

Apart from the usual suspects (account rights, Set-ExecutionPolicy RemoteSigned, and firewall configuration) I can't think of a good reason why PowerShell shouldn't work exactly the same in a Windows Services as it does in a console application, which makes me think it might be something environmental. It's not something silly like poor DNS resolution, is it? Assuming you're running this on a server, does it also perform slowly on your development machine too?

PS: I wonder if this question might be more appropriate for ServerFault?

Damian Powell
It's a programming question, really -- since he's asking about the way he calls the API...
Jaykul
@Jaykul It's a programming question if he is correct in his assumption that his code is wrong. Given that he has similar behaviour in a service and console app, I'm guessing it's something environmental and therefore worth a shot on SF. TBH though, it's difficult to tell either way without a bit more experimentation and a few more details.
Damian Powell
A: 

Without seeing what you're doing, I can't think of many things that would make that big of a difference:

  • Do you have profile customization that might be helping it out when it runs in PowerShell (test with PowerShell -noprofile)
  • Are you counting the time it takes to initially import a module or snapin in your service, but not in PowerShell.
  • Are you re-creating the whole runspace each time in your service?

Have you tried using the 2.0 API of PowerShell with PowerShell.Create() to run it?

Finally, you could also take a look at some of the open source PowerShell hosts like PoshConsole or bgHost on CodePlex ... if they don't run it slowly, then you could follow the process they use for creating their runspace.

Jaykul