If you have a snap-in with many cmdlets that connect and talk to a resource,
what is the recommended way to pass the connection along?
There is a cmdlet connect-myresource that takes a machine name and
credentials and connects to the remote application. Then there are many
cmdlets to act against that connection.
so some possibilities are:
1. have a connection named parameter and/or positional 0 and require user to
supply for every cmdlet
$connection = connect-myresource -server xxxx
get-myobject $connection -id xxxx | delete-myobject2 $connection -id
$_.object2id
the problem is i'd like to not require any connection and if the cmdlet does
not have a connection sent to it then it should automatically try to connect
to the local machine
I could still do it this way but the user might forget to send the
connection and have undesirable results...
$connection = connect-myresource -server xxxx
get-myobject $connection -id xxxx | delete-myobject2 -id $_.object2id
(deleted off local machine - might not be what user expected)
2. have every cmdlet pass the connection object to the pipeline along with
the normal object it writes
is it a good idea to do this? it would allow...
get-myobject -id xxxx | delete-myobject2 -id $_.object2id
(get-myobject connected to the local and sent it along)
connect-myresource -server xxxx | get-myobject -id xxxx | delete-myobject2
-id $_.object2id
(all cmdlets acted on the remote connection)
or
$connection = connect-myresource -server xxxx
get-myobject $connection -id xxxx | delete-myobject2 -id $_.object2id
(all cmdlets used $connection)
3. Only other way I could think of is setting a var
every cmdlet checks for a global connection var
if available it uses it
otherwize creates a local connection and sets the global var
connect-myresource just sets the global var
only problem i see with this is i don't really like to use a global var
since once a connection is made then everything acts on that even after the
current command
any opinions on what would be the best way?


