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.

Community
  • 1
  • 1
Rob
  • 4,327
  • 6
  • 29
  • 55
  • 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. – mjolinor Apr 24 '15 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" – Rob Apr 24 '15 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. – mjolinor Apr 24 '15 at 14:49

1 Answers1

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
user4003407
  • 21,204
  • 4
  • 50
  • 60
  • Perfect! Thanks! And piping it to Import-Module lets me remove it later if I should wish – Rob Apr 24 '15 at 16:19