How can I call Start-Job, which depends on a function in the same powershell module as the function that calls Start-Job?

I am writing several powershell to talk with AWS API, in one module. I wrote one function, Get-CloudFormation , which returns the status of CloudFormation. I wrote another Delete-CloudFormation , which, after disabling the API request, delete-CF tries to run a task that checks the status of CloudFormation using Get-CloudFormation .

I call Export-ModuleMember on Get-CloudFormation (but not Delete-CloudFormation ; this is a private function). Get-CloudFormation defined earlier in the module file than Delete-CloudFormation .

My Start-Job call (inside Delete-CloudFormation ) looks like this:

 $job = Start-Job -Name "CloudFormationWaitForDeleteSuccess" -ScriptBlock { $status = "" $time = 0 while($status -ne "DELETE_COMPLETE") { Write-Verbose ("Checking CloudFormation status") $stack = Get-CloudFormation -accessKey $accessKey -secretKey $secretKey -stackName $stackName $status = $stack.Status Start-Sleep -seconds 10 $time += 10 } Write-Host "CloudFormation delete-complete after $time seconds $stackName" } 

When Delete-CloudFormation , I get an exception:

 The term 'Get-CloudFormation' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. + CategoryInfo : ObjectNotFound: (Get-CloudFormation:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException 

Why? And how to fix it?

I found 7152090 , which, it seems to me, is similar, but calling Start-Job with -InitializationScript { Get-CloudFormation } gives about the same error.

If I call Start-Job with -InitializationScript { Import-Module ".\awsutils.psm1" } , then . is my document directory. Even if I bind the variable to Get-Location outside of Start-Job and name it as -InitializationScript { Import-Module "$location\awsutils.psm1" } .

+8
powershell jobs
source share
4 answers

move the awsutils.psm1 module in the canonical path for powershell modules:

 $env:userprofile\documents\WindowsPowerShell\Modules\awsutils" 

then initialize a start-job like this

 -InitializationScript { Import-Module awsutils } 

Tested by my custom modules and startup work.

try also if you don't want to move your psm1:

 -InizializationScript { import-module -name c:\yourpath\yourmodulefolder\ } 

where yourmoduleforder contains only one psm1 file.

+5
source share

Background jobs are autonomous things. They are not separate thread-sharing resources; they actually run in a completely new PowerShell.exe process. So I think you will need to use Import-Module inside your script block so that the members of the module are there.

+2
source share

What I ended up with was setting $env:WhereAmI = Get-Location before calling Start-Job , and then switching to -InitializationScript { Import-Module "$env:WhereAmI\awsutils.psm1 } . After calling Start-Job I called Remove-Item env:\WhereAmI to clean.

(I need a solution that did not require me to develop the module in $ PSModulePath, because then source control is a little more painful to configure.)

Thanks for answers.

0
source share
 $root = $PSScriptRoot $initScript = [scriptblock]::Create("Import-Module -Name '$root\Modules\Publish-Assigned_CB_Reports.psm1'") $job1 = Start-Job -InitializationScript $initScript -ScriptBlock {} -ArgumentList 
0
source share

All Articles