proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

Add ImportPSModulesFromPath support #177

Open beargle opened 6 years ago

beargle commented 6 years ago

Do you want to request a feature or report a bug? Feature

What is the current behavior? Importing modules in background runspace jobs requires that the module be:

What is the proposed behavior? Add support for passing a custom module path, from which modules will be imported into background runspace jobs.

This should require no modification of $env:PSModulePath, and shouldn't require the module to be pre-installed on the client. Expecting something similar to how modules are specified for import in psake-config.ps1. Specific use case is a portable CI/CD pipeline that manages PowerShell module dependencies, without relying on Install-Module or a specific build server configuration.

It looks like InitialSessionState.ImportPSModulesFromPath() could work for this, but would make a hard dependency on PowerShell V3. Wasn't sure if PowerShell V2 support was still important based on #162.

Please provide a code example showing the potential feature, if applicable: Today we pass in one or more module names to import, which have to be accessible with $env:PSModulePath

Start-RSJob -ModulesToImport @("Pester")...

Would like to be able to pass in a custom module path, from which one or more modules are imported. Assuming a directory structure of:

Dependencies/
    PowerShell/
        Modules/

We should be able to:

Start-RSJob -ModulesFromPathToImport ".\Dependencies\PowerShell\Modules"...
beargle commented 6 years ago

If this looks like something that would be useful, I'm happy to put together a PR for review. Thanks!

proxb commented 6 years ago

@beargle This sounds cool! I am definitely done with adding new features for V2 and have already moved the V2 supporting module onto its own branch. I just need to work on removing the V2 code as well as adding a warning when this module is imported from a V2 host. I should have that completed by the end of the week if you can wait until then, otherwise go ahead and do the PR and I will merge it and then begin work on removing the V2 stuff.

MVKozlov commented 6 years ago

btw this can be done thru -ModulesToImport parameter like Import-Module -Name. If supplied path exists, that it is path to module, if not - just module name and path can support wildcards thru Resolve-Path

beargle commented 6 years ago

@MVKozlov Good point about similar functionality with -ModulesToImport; I do see that it could be used if the collection of module paths is pre-built with Resolve-Path and wildcards.

I haven't been able to get -ModulesToImport to work with a single path that contains module subdirectories (ex. like what Windows users have at ~\Documents\WindowsPowerShell\Modules). Am I missing something on that front?

Regardless, PR has been raised - would appreciate any feedback you all have. Thanks!

MVKozlov commented 6 years ago

I this this way close to your needs

$list = 'PoshWSUS', 'D:\Work\ps\test\', 'D:\Work\ps\test\PSAlphaFS\2.0.0.1\*.psm1'

$modulenames = New-Object System.Collections.ArrayList
$modulepaths = $list | ForEach-Object {
  $m = $_
  try {
    Resolve-Path $_ -ErrorAction Stop | Select-Object -ExpandProperty Path
  }
  catch {
    $null = $modulenames.Add($m)
  }
}
$modulenames
$modulepaths

here I'm split common list to names and paths