I'm developing a custom PowerShell module, which I'd like to use in context of a remote session to a different computer. The following code (which obviously doesn't work) explains what I'm trying to achieve:
import-module .\MyCustomModule.psm1 $session = new-pssession -computerName server01 invoke-command -session $session -scriptblock { <# use function defined in MyCustomModule here #> }
The first question is whether it is at all possible to achieve this scenario? I mean I would only like my custom module to be physically present on my machine, not on remote server.
I have found this thread, but I didn't manage it to work - it doesn't allow creating a session from remote machine back to the local one. Probably, I faced with the configuration limitations mentioned somewhere in the comments to that thread... Besides, the author mentioned the performance implications which is critical for my solution...
If that's possible, then how?
The version of PowerShell is currently not a constraint - if the solution is only available in PS 3.0 - I can live with this.
In PowerShell 2.0, you can import a newly-installed PowerShell module with a call to Import-Module cmdlet. In PowerShell 3.0, PowerShell is able to implicitly import a module when one of the functions or cmdlets in the module is called by a user.
The Import-PSSession cmdlet imports commands , such as cmdlets, functions, and aliases, from a PSSession on a local or remote computer into the current session.
Open an elevated Windows PowerShell command prompt (run Windows PowerShell as an administrator). Run the Install-Module MSOnline command. If you're prompted to install the NuGet provider, type Y and press Enter. If you're prompted to install the module from PSGallery, type Y and press Enter.
There were some great comments to the question, and I've spent some time investigating various ways to approach the problem.
To begin with, what I've initially asked for is not possible. I mean, if you go the module way, then the module should be physically present on a target machine to be able to Import-Module
into remote session.
To abstract my question further, I'm trying to create a reusable PowerShell-based framework for the product deployments. It's going to be a push-manner deployments, meaning that we encourage people to run some scripts on a local machine to deploy to some remote server. As far as I investigated the area, there are two possible ways which are friendly to the common sense.
The process to follow:
*.psm1
)PSModulePath
variable to include the new modules locationInvoke-Command -Session $s -ScriptBlock {...}
Import-Module CustomModule
- it will search the CustomModule
on a remote machine and obviously will find itThe following are the reasons to love this approach for:
The following is important to take into consideration:
xcopy
from the shared folder. Besides, the delivery mechanism should support upgrade / downgrade and (preferably) multi-instance installations, but that's more related to my task than to the problem in generalThe process to follow:
Invoke-Command -Session $s -FilePath .\myscript.ps1
to load the functions defined in a script to the remote sessionInvoke-Command -Session $s -ScriptBlock {...}
and refer to your custom functions - they will be there in a sessionThe following are good points of this approach:
Sure, it's not ideal:
Finally, I should say that remote machine still needs to be prepared for the remoting. This is what I mean:
Set-ExecutionPolicy Unrestricted
Enable-PSRemoting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With