Closed malthe closed 2 years ago
Just very briefly thinking about this I think an easier solution would be to allow Client(...)
to accept a RunspacePool
object. That way the function can then re-utilise this pool instead of creating a new one.
@jborean93 yes I suppose it could have a from_runspace_pool
class method / constructor which then initiates the client with a pool (and as a consequence, can use this directly in its methods).
I think just doing something like this would work
from pypsrp.client import Client
from pypsrp.powershell import RunspacePool
from pypsrp.wsman import WSMan
with WSMan(...) as wsman, RunspacePool(wsman) as rp:
client = Client(rp)
client.copy(..., ...)
We could also just expose copy
(and other functions) as a standalone function that takes in a pool so both the old Client(wsman).copy
works and this newer scenario.
The new psrp
namespace that is coming out in the upcoming 1.0.0
release includes this functionality. You can specify an already opened Runspace Pool with it's calls and it will reutilise the pool rather than creating a new connection.
import psrp
def main():
wsman = psrp.WSManInfo('server', ...)
with psrp.SyncRunspacePool(wsman) as rp:
psrp.copy_file(rp, 'src', 'dst')
psrp.invoke_ps(rp, 'script')
...
This hasn't been implemented in the old pypsrp
area as I'm stopping work on that in favour of the new stuff in psrp
.
Currently, the functionality to copy/fetch files is tied to
pypsrp.client.Client
.If you want to do multiple such operations on the same
RunspacePool
, have an existing pool or an existingwsman
connection then there is no direct way to access this functionality.An example of a possible API:
For a more generic interface, we might want to pass an open binary stream instead as the source:
It's a little awkward to implement because of the computation of the maximum buffer size which is based on the internal
WSMan
object – perhaps the connection provided toRunspacePool
should actually be aWSMan
rather than mostly that and possibly another object.For example: