How do I call Start-Job which depends on a function in the same powershell module as the function calling Start-Job?
move you module awsutils.psm1
in the canonical path for powershell modules:
$env:userprofile\documents\WindowsPowerShell\Modules\awsutils"
then initialize start-job like this
-InitializationScript { Import-Module awsutils }
Tested with my custom modules and start-job works.
try also, if you don't want move your psm1 this:
-InizializationScript { import-module -name c:\yourpath\yourmodulefolder\ }
where yourmoduleforder
contain only one psm1 file.
Background jobs are autonomous things. They aren't a separate thread sharing resources, they are actually run in a whole new PowerShell.exe process. So I think you will need to use Import-Module
inside your script block to have you module members available there.
What I ended up doing was setting $env:WhereAmI = Get-Location
before the call to Start-Job
, and then changing to -InitializationScript { Import-Module "$env:WhereAmI\awsutils.psm1 }
. After the Start-Job
call, I called Remove-Item env:\WhereAmI
to clean-up.
(I wanted a solution that didn't require me to be developing the module within the $PSModulePath, because then source-control is a little more painful to set up.)
Thanks for the responses.