1

I have a function that I use in my main script, and I also need to create a Job, which uses the same function. Right now I just have the code written twice, once in the main script, and once in the script block handed to the Job. I know I can add code to a variable, but not sure how to then unpack that variable in the code block, so the same code is effectively used in both places. If it makes a difference, I am limited to v2 unfortunately.

1 Answer 1

1

Use a Module

Define your function(s) in the module.

In your main script, you import the module.

Import-Module MyModule

In your job, you import the module:

Start-Job -ScriptBlock {
    Import-Module MyModule

    # Invoke-MyFunction
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.