Reputation: 14700
I'm writing a C# server application that, among other things, calls several remote Powershell cmdlets on an Exchange server. It's not the main activity of my application, but these functions are occasionally called. When they do, I do the following:
RunspaceFactory.CreateRunspace
.PowerShell
object.PowerShell
and Runspace
objects.Now, is there really a need to go through all these steps for every call? Assuming that a call might be made every minute, hour, or even once a day, I'm not worried about performance. But I am looking for a neat and tidy solution, and wonder if creating a Runspace object once and calling it again and again might be more elegant.
Is there a problem with reusing a Runspace object and keeping it open? Should I keep the object alive, but call Close()
and Open()
on it whenever I need to? Or should I not depend on it remaining stable over time (and "over time" here is the server app's lifetime, which could (and should) be months.
Upvotes: 2
Views: 1030
Reputation: 13887
I've noticed that remote sessions, if you leave them open and don't intend on using them again, can cause problems. There's usually a limit to the number of remote shells that can be opened on a server at once. If you don't bother to clean up after yourself, you can hit that limit quite easily and you won't be able to open another remote shell.
I'd also mention that depending on the maximum size of your remote shell (default is 50MB but can be increased, when I run installers I typically need to increase this significantly) you could be eating up memory unnecessarily. You mentioned you aren't worried about performance though so this may not be an issue for you.
That being said, I'd try to be as neat and tidy as possible. There's no reason not to clean up after yourself properly.
Upvotes: 1