1

For example, one (bad) idea I had would be to import a module from a string defined in the file (I don't know if I can do this without first writing this string to the file system)

I'm writing a script that I want to be able to execute remotely, and following this answer I'm using the "Scripts Approach". However, I want to hide some of the functions somehow - I'd like a sort of module within a script.

Or is there a completely different approach to what I'm trying to do? All my scripts/modules are client side, and I want to run them in a remote PSSession. I don't particularly want to handle copying of scripts to the remote server or shared drive.

I am using the following to dot source my script into a remote PSSession:

invoke-command -Session $s -FilePath $p

and then invoking a scriptblock making use of the functions defined in the script file $p. This works, but I don't think this works with modules, and if I want to reuse code in other scripts, I either have to manually import each one into the remote session, or duplicate code in a single monolithic script. Neither is appealing, and neither seems to allow hiding of "private" methods.

3
  • 3
    I think you've painted yourself into a corner. If all of the script components are going to reside on the local machine, then the user has to be able to read it in order to invoke it. Commented Apr 24, 2015 at 14:12
  • @mjolinor - I don't see why that is necessarily the case. E.g. if I have a module file locally that I can read, the non-exported methods in that module file are still "hidden" when I import the module. That is what I mean by "hidden" Commented Apr 24, 2015 at 14:37
  • From Get-Help Import-Module: Description The Import-Module cmdlet adds one or more modules to the current session. The modules that you import must be installed on the local computer or a remote computer. Commented Apr 24, 2015 at 14:49

1 Answer 1

3

You can use New-Module cmdlet:

New-Module {
    function a {
        b
    }
    function b {
        'function b called'
    }
    Export-ModuleMember a
}|Out-Null
a #OK
b #Error
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thanks! And piping it to Import-Module lets me remove it later if I should wish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.