Managing sessions cleverly - WinSCP Module

A long time ago I wrote a pretty clunky WinSCP module in a bit of a rush to get it up running quickly (every action built it’s own session, did it’s action then closed the session).

Not so long ago I dusted it off with the view to modularising it properly so you can create a session, do whatever you need to (get, push & move files) then close the session when you’re ready.

I know there is already a GNU’d version, I’m doing this mainly for my own learning.

With this I’m trying to handle the WinSCP sessions in a easy-to-use way.

At the moment my approach has been to include a “WinSCPSession” parameter on all the relevant functions under the module. (Much like the other WinSCP module does, I’ve only just downloaded tonight it to see how this was approached there, before coming here to ask the question.)

I’m wondering if there is something clever I can do to negate needing this when dealing with only one session…although as I type this I’m thinking have the Session parameter is worthwhile for cases where you’ll be connecting to 2 or 3 different FTP/FTPS/SFTP/SCP servers.

Does anyone know how the Connect-MSOLService cmdlet for Azure / Office 365 works? I know it’s a bit different as you can only really connect to one MSOL Service at a time, but I’m still wondering if I can get rid of the Session parameter as a lot of the time it will just be very repetitive.

As I’m doing this for my own learning I’m quite happy to read up on topics and work on my approach. Please don’t feel like I’m asking for a copy & paste answer; I’m more than happy to do some reading, research and testing with this.

Editted: I improved my knowledge of how the Exchange Online / Azure Online module connects and corrected the wording of my question to remove any confusion.

So, it seems as if Invoke-Command has the model you want.

One parameter set has -ComputerName, and when used, that sets up/uses/closes a session for each computer specified.

Another parameter set has -Session, and when used, it accepts one or more open sessions, uses them, and leaves them open.

The key is putting them in different parameter sets, since they’re mutually exclusive.