user103633
user103633

Reputation: 213

Powershell script running more slowly in a runspace than in the shell

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?

Upvotes: 3

Views: 2304

Answers (4)

Jaykul
Jaykul

Reputation: 15824

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.

Upvotes: 1

Damian Powell
Damian Powell

Reputation: 8775

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?

Upvotes: 0

Bill Wert - MSFT
Bill Wert - MSFT

Reputation: 181

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.

Upvotes: 0

Shay Levy
Shay Levy

Reputation: 126842

You can run the script as a scheduled task.

Upvotes: 1

Related Questions