0

I have a PowerShell program with C# code inside. I want that the C# function will run in a new job. I tried this-

start-job -name Job1 -ScriptBlock {[MyProgram.Program]::Main()}

It looks like the job was executed but nothing happened.

6
  • What does "it doesn't work" mean ? Do you have an error message ? Commented Dec 17, 2019 at 12:59
  • Cannot work, because scriptblock don't know declaration of c# class. Commented Dec 17, 2019 at 13:01
  • @f6a4 so is there a way to execute C# function using start-job? Commented Dec 17, 2019 at 13:01
  • @shon Jobs execute in a separate process, so you'll need to compile/execute your C# code in the {} scriptblock before calling Main() Commented Dec 17, 2019 at 13:04
  • 2
    @shon Show us your whole script (including the C# code) Commented Dec 17, 2019 at 13:18

1 Answer 1

1
start-job -name Job1 -ScriptBlock {

Add-Type -typedef @"

    namespace MyProgram 
    {
        //-----------------------------------------
        public class Program
        //-----------------------------------------
        {

            //-------------------------------------
            public static void Main()
            //-------------------------------------
            {
                // Your c# Code here
            }

        }
    }
"@


[MyProgram.Program]::Main()

}
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.