Forum Discussion
AndreiPossamai
Jan 24, 2022Copper Contributor
The 'using module' statement into a ThreadJob's ScriptBlock
Hi everyone! I have a question about the 'using module' statement in Powershell. I have many class in different .psm1 files, wich works perfectly with 'using module' statement. But I need to ...
ManuelBerfelde
Apr 07, 2024Copper Contributor
Did you ever find a solution for this?
Struggeling myslef here.
Struggeling myslef here.
LainRobertson
Apr 08, 2024Silver Contributor
It is doable, but not necessarily advisable.
If the module's simple, such as only defining classes and functions then you'll be fine. But if you're doing more complex work like using database connections and other limited resource operations then you can run into issues if you're not careful - and overhead may become an issue, too, if initialisation operations take a long time.
Still, here's some example code you can use within your job's [scriptblock] to get around PowerShell's "using must be the first line" error.
$Script = [scriptblock]::Create("using module 'D:\Data\Temp\Forum\Forum.psm1'");
. $Script *> $null;
Cheers,
Lain
- ManuelBerfeldeApr 11, 2024Copper Contributor
My usecase is classes - so... thanks a lot 🙂