Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ericfran opened this issue Oct 6, 2022 · 12 comments
Open

Comments

@ericfran
Copy link

ericfran commented Oct 6, 2022

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
@ericfran ericfran changed the title Support Using Class Object in command used in the SCriptblocl Support Using Class Object Method in command used in the SCriptblock Oct 6, 2022
@MVKozlov
Copy link
Contributor

MVKozlov commented Oct 6, 2022

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
Copy link
Author

ericfran commented Oct 6, 2022 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Oct 6, 2022

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
Copy link
Author

ericfran commented Oct 6, 2022 via email

@ericfran
Copy link
Author

ericfran commented Oct 6, 2022 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Oct 6, 2022

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
Copy link
Author

ericfran commented Oct 6, 2022 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Oct 7, 2022

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

@ericfran
Copy link
Author

ericfran commented Oct 7, 2022 via email

@MVKozlov
Copy link
Contributor

MVKozlov commented Oct 7, 2022

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
Copy link
Author

ericfran commented Oct 11, 2022 via email

@ericfran
Copy link
Author

ericfran commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants