Running custom functions with -AsJob

Hello everyone,

I have a stack of functions combined into a module, both private and public functions.

I want to utilize this in the Azure environment, what would be the proper way to source both private and public functions and send the via Start-Job ?

I was doing something like this(below), but this only applies to the one function, which is not enough in my case.

PS C:\> $myfunction = (Get-Command Get-AzWebAppSize).ScriptBlock ; $myfunction = "function get-azwebappsize {"+$myfunction+"}"
PS C:\> $scriptblock = [scriptblock]::Create($myfunction)

PS C:\> Start-Job -InitializationScript $scriptblock -ScriptBlock {Get-AzWebAppSize -WebApp whatever}

I would import the entire module into the job. You can retrieve an in-memory module with $module = Get-Module Mymodule. Then, pass that as an argument to the job script. Then import it in the job script itself. :slight_smile:

 

Awesome, after some digging I got the solution.

But this is only for the local environment, I will need to find out how to work this out in Azure :slight_smile:

$root = "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MyModule\MyModule.psm1"

$initscript = [scriptblock]::Create("import-module -name $root")

Start-Job -InitializationScript $initscript -ScriptBlock {Get-AzWebAppSize -WebApp nemanjajovicwebapp}

Perhaps something more along these lines?

Import-Module "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MyModule\MyModule.psm1"
$Module = Get-Module MyModule
Start-Job -ArgumentList $Module -ScriptBlock {
    param($Module)
    Import-Module $Module
    Get-AzWebAppSize -WebApp nemanjajovicwebapp
}