proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

Support Using Class Object Method in command used in the SCriptblock #215

Open ericfran opened 1 year ago

ericfran commented 1 year ago

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
MVKozlov commented 1 year ago

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.

ericfran commented 1 year ago

HiCan you explain ?Give an example? This oracledb is a class define in module file psm1I import in my script with : using module oracle class.psm1Then I wan to execute multiple sql call (method) for an oracle object of type [oracledb] How can I make the class [oracledb] in the blockscript ?ThanksRegardsOn 6 Oct 2022, at 15:36, Max Kozlov @.***> wrote: 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

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

MVKozlov commented 1 year ago

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

ericfran commented 1 year ago

Ok thanksDoes it take into account the class defined in the module ?On 6 Oct 2022, at 19:56, Max Kozlov @.***> wrote: 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

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

ericfran commented 1 year ago

Hi,

I have a class file : class\ORacleDBClass.psm1 containing the class [oracleDB]

Then execute the following $OracleConnection = [Oracledb]::new(); $lv_result = CreateHRWDBConnection -DBConnection $OracleConnection -Open $true -Policy $RetryPolicy; $TicketITems = [HrxTicket]::new()

Start-RSJob -Throttle $MaxJobs -Batch "Demo" -argumentlist $OracleConnection, $TicketITems -Verbose -ModulesToImport 'C:\TicketPDFGenerator\Class\OracleDBClass.psm1' -ScriptBlock { param([psobject]$lv_connection, [psobject]$lv_TicketITems) import-module 'C:\TicketPDFGenerator\Class\OracleDBClass.psm1' -PassThru -Verbose import-module 'C:\TicketPDFGenerator\Class\Class\HrXTicketClass.psm1' -PassThru -Verbose $lv_TicketITems.GetTicketAttributes([oracleDb]$lv_connection) } Get-RSJob

@.***

@.***

Regards,

Eric

From: Eric FRANCKX @.> Sent: Thursday, 6 October 2022 20:54 To: Eric Franckx @.> Subject: Fwd: [proxb/PoshRSJob] Support Using Class Object Method in command used in the SCriptblock (Issue #215)

[External]

Sent from my iPhone

Begin forwarded message: From: Eric FRANCKX @.**@.>> Date: 6 October 2022 at 20:47:02 CEST To: proxb/PoshRSJob @.**@.>> Cc: proxb/PoshRSJob @.**@.>>, Author @.**@.>> Subject: Re: [proxb/PoshRSJob] Support Using Class Object Method in command used in the SCriptblock (Issue #215) Ok thanks Does it take into account the class defined in the module ?

On 6 Oct 2022, at 19:56, Max Kozlov @.**@.>> wrote: 

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

— Reply to this email directly, view it on GitHubhttps://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fproxb%2FPoshRSJob%2Fissues%2F215%23issuecomment-1270473821&data=05%7C01%7Ceric.franckx%40alight.com%7Cfecab227ea3941e5033f08daa7cc24d7%7C232a81501ff64e00ad51c491fb66cb74%7C1%7C0%7C638006792524250151%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=QLb6vtmOOuPDLvnbslw%2BtVjzL8fWgu5lFqheBIps2zg%3D&reserved=0, or unsubscribehttps://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FARDNWIGVHWUZRGGPQFZTXN3WB4HFHANCNFSM6AAAAAAQ6PKJZI&data=05%7C01%7Ceric.franckx%40alight.com%7Cfecab227ea3941e5033f08daa7cc24d7%7C232a81501ff64e00ad51c491fb66cb74%7C1%7C0%7C638006792524250151%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=VY3JPJqGVBuRzcAe4wd3QQY4SLAYi%2FlsMK52VZMUN6g%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.**@.>>

MVKozlov commented 1 year ago

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?

ericfran commented 1 year ago

I need to use import parameter : import module ?

On 6 Oct 2022, at 23:37, Max Kozlov @.***> wrote:

 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?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.

MVKozlov commented 1 year ago

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

ericfran commented 1 year ago

HiHere it is well explain function and module are uploaded by not classesUsing Statement: Import PowerShell Classes from Modules - SAPIEN Information Center | SAPIEN Information Centerinfo.sapien.comI’m i wrong ?But with:  using module …. That works but it needs to be the first line of a script and it is not supported in scriptblockRegardsEricOn 7 Oct 2022, at 09:07, Eric FRANCKX @.> wrote:HiI try the importmodule, and during debugging but i would like to check if the import works fine and also base on some doc/info sels that natively powershell doesn’t know class define in file when using import module command but work well with command : using module at the start of the script (as first command)RegardsEricSent from my iPhoneOn 7 Oct 2022, at 08:21, Max Kozlov @.> wrote: you can use either one or the other but recommended way -ModulesToImport paramete—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

MVKozlov commented 1 year ago

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

ericfran commented 1 year ago

HiI try the importmodule, and during debugging but i would like to check if the import works fine and also base on some doc/info sels that natively powershell doesn’t know class define in file when using import module command but work well with command : using module at the start of the script (as first command)RegardsEricSent from my iPhoneOn 7 Oct 2022, at 08:21, Max Kozlov @.> wrote: you can use either one or the other but recommended way -ModulesToImport paramete—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.>

ericfran commented 1 year ago

But also restriction for bested class or . Fienke class , seems they need to be in the root module to be usedSent from my iPhoneOn 7 Oct 2022, at 15:40, Max Kozlov @.***> wrote: 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

you can just write

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

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>