Using -FunctionsToLoad
tyteen4a03 opened this issue · comments
Are there any examples on how to use -FunctionsToLoad?
I have the following script:
Import-Module $PSScriptRoot\Module.psm1 # Exposes FunctionA and Function B
Import-Module $PSscriptRoot\$dynamicallyImportedModule.psm1 # Exposes FunctionC
1..10 | Start-RSJob -ScriptBlock $scriptBlock -FunctionsToLoad "FunctionA", "FunctionB", "FunctionC"
However the script will tell me it cannot find these functions even though they are definitely loaded.
It turns out while I only have 10 jobs, the system spawned 104 jobs while trying to load the correct functions into memory. What is going on here?
you should give more info about your modules.
PS C:\> gc "D:\!\specialmodule.psm1"
function test-func {
'this is a test'
}
export-modulemember -function test-func
PS C:\> import-module "D:\!\specialmodule.psm1"
PS C:\> start-rsjob { test-func } -FunctionsToImport test-func
Id Name State HasMoreData HasErrors Command
-- ---- ----- ----------- --------- -------
1 Job1 Completed True False test-func
PS C:\> Get-RSJob | Receive-RSJob
this is a test
as you can see, there is no problem
but for example with remote exchange there is a problem
PS C:\> $mailsession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://MAILSERVER.domain.com/PowerShell/ -Authentication Kerberos
PS C:\> Import-PSSession $mailsession
PS C:\> start-rsjob { get-mailbox administrator } -FunctionsToImport get-mailbox
PS C:\> start-rsjob { get-mailbox administrator }
PS C:\> Get-RSJob
Id Name State HasMoreData HasErrors Command
-- ---- ----- ----------- --------- -------
1 Job1 Completed False False test-func
2 Job2 Failed False True get-mailbox administrator
3 Job3 Completed False True get-mailbox administrator
PS C:\> Get-RSJob 2 | Receive-RSJob
Exception calling "EndInvoke" with "1" argument(s): "The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name, a script block, or a CommandInfo object."
PS C:\> Get-RSJob 3 | Receive-RSJob
The term 'get-mailbox' 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.
All of my functions in my modules will remote into a server with PS Remoting. Does this mean I cannot use this module then?
I can call my functions after Import-Module
ing them just fine, just not within a RSJob ScriptBlock.
No, remoting also work ok
module:
function test-func($c) {
Invoke-Command -Comp $c { ps }
}
export-modulemember -function test-func
call:
PS C:\> start-rsjob { test-func server } -FunctionsToImport test-func
Id Name State HasMoreData HasErrors Command
-- ---- ----- ----------- --------- -------
2 Job2 Running False False test-func server
PS C:\> Get-RSJob 2 | Receive-RSJob
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName
------- ------ ----- ----- ------ -- -- ----------- --------------
44 6 3556 864 10,11 2272 2 conhost server
39 5 1868 456 4,07 4560 2 conhost server
326 12 2348 688 16,96 364 2 csrss server
...
there may be some specific with functions or scriptblock writing style.
If you can construct short but real code example it can help to find where is a problem
btw, may be there is incompatibility with PS versions or module version ?
which version you use ?
It turns out the issue might actually be me not clearing out old junk by running Get-RSJob -Name * | Remove-RSJob
, therefore I was reading old errors when the script had other problems. It's strange that this is not done automatically when the script finishes executing, however I'm just happy it works.
Now onto unhelpful debug messages...