Scope of functions defined in modules when used in psake tasks
I created a script module test-module.psm1:
function Invoke-Test { Import-Module ".\somefunctions.psm1" Import-Module ".\morefunctions.psm1" Set-Something #This is a function defined in morefunctions.psm1}
and a couple of dummy modules, somefunctions.psm1:
function Get-Something { 'Get-Something'}
and morefunctions.psm1:
function Set-Something { Get-Something 'Set-Something'}
If I call
Import-Module .\test-module.psm1Invoke-Test
then I get the error "Get-Something : The term 'Get-Something' is notrecognized as the name of a cmdlet, function, script file, or operableprogram.". So it looks like a generic PowerShell issue dealing with scriptmodules. I tried PowerShell v2.0, v3.0, and v4.0.
Perhaps this cannot be resolved in psake without workarounds because it is a scriptmodule. You can use the similar toolInvoke-Build. It is implementedas a script and avoids issues like these. It works finewith this build script:
Task Invoke-Deploy { Import-Module ".\somefunctions.psm1" Import-Module ".\morefunctions.psm1" Set-Something #This is a function defined in morefunctions.psm1}
It outputs, as expected:
Build Invoke-Deploy ...\.build.ps1Task /Invoke-DeployGet-SomethingSet-SomethingDone /Invoke-Deploy 00:00:00.0150008Build succeeded. 1 tasks, 0 errors, 0 warnings 00:00:00.1450083
I ran into this today and got it to work.
In your module "morefunctions.psm1" you need to export the method you want like this:
Export-ModuleMember -Function Set-Something
In your psake task, you need to prepend the module name in front of the method so PowerShell can find it:
Import-Module "morefunctions.psm1"morefunctions\Set-Something