Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guidelines for creating remote-enabled functions #112

Open
mlanderos opened this issue Jul 28, 2018 · 2 comments
Open

Guidelines for creating remote-enabled functions #112

mlanderos opened this issue Jul 28, 2018 · 2 comments

Comments

@mlanderos
Copy link

Hi,

I have been searching the web for any guidelines, examples or best practices for when you want to create functions within or not within a module that allows for local and remote execution, but I haven't found anything.

A good example is the Get-Service cmdlet. If you specify the ComputerName parameter, the cmdlet knows how to handle executing this the specified host. If the ComputerName parameter is not used, then the cmdlet executes on the local computer.

Any help or guidance is really appreciated.

Thanks,
Michael

@Jaykul
Copy link
Member

Jaykul commented Aug 2, 2018

This is a great question! I hope some others will pitch in thoughts ...

From my point of view, it seems that very few of these types of command will ever be written by third parties -- they only truly make sense when the thing you're working against has a built-in method of remote execution that is not WSMan, or is inherently remote anyway (e.g. REST commands like the Azure modules, or commands based on CIM or DCOM APIs).

Obviously if the command needs to execute PowerShell on a remote computer in order to work there, it's not a good idea to build remote execution into the command because you're just adding complication (e.g. many different ways to create sessions) and it's ultimately inefficient: you want the end user to create a single session and run multiple commands within it, rather than each command creating it's own session.

@mlanderos
Copy link
Author

Thanks for the comment @Jaykul !

One thing I learned is that Invoke-Command cmdlet works on your local machine. I originally thought it only worked when running a command to a remote machine. I have one large function that houses a few other functions which work locally or remotely. If the ComputerName passed in is not the local host, I first verify if a PSSession is present and not in a broken state, and then create a new PSSession or utilize the existing session to run these functions persistently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants