proxb/PoshRSJob

Support Using Class Object Method in command used in the SCriptblock

Opened this issue · 12 comments

Do you want to request a feature or report a bug?

What is the current behavior?
Is it possible to use a object class method to be used in the Scripblock ?

example:
tart-ThreadJob -ScriptBlock {
# Get Ticket Properties from database
$lv_Tems.GetItempProperties([oracleDb]$DBConnection)

Unable to find type [oracleDb].
InvalidOperation:

How is it possible to make it work ?
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

What is the expected behavior?

Which versions of Powershell and which OS are affected by this issue? Did this work in previous versions of our scripts?

Please provide a code example showing the issue, if applicable:

#Code goes here

there is no type accelerator [oracleDb] in the powershell by default
so you got it somewhere.
Add it in the same way to your script :)
This can be import-module or add-type
and if your $lv_Tems from outside world, add it too.

The Start-RSJob have -ModulesToImport parameter, use it for your module.

The Start-RSJobs's scriptblock itself has no method call restrictions, but it runs in a new runspace and has no imported modules or variables other than those you passed to it via -xxxToImport parameters

you can skip import-module inside scriptblock but add HrXTicketClass to -Import parameter.
it is equivalent to import-module inside

and your variant doesn't work?
what error you get?

you can use either one or the other
but recommended way -ModulesToImport parameter

do you mean this?
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_using?view=powershell-7.2

yes, there is restriction

The using statements must come before any other statements in a script or module. No uncommented statement can precede it, including parameters.

Afaik it just syntax sugar
instead of

using namespace System.IO
[Stream]$memorystream = [MemoryStream]::new($stringbytes)

you can just write

[System.IO.Stream]$memorystream = [System.IO.MemoryStream]::new($stringbytes)

So, your scriptblock can be rewritten without using using :)
and what happens inside the module is the problem of the module