Search code examples
c#powershellpowershell-2.0jobs

Powershell - Calling C# function inside a job


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.


Solution

  • start-job -name Job1 -ScriptBlock {
    
    Add-Type -typedef @"
    
        namespace MyProgram 
        {
            //-----------------------------------------
            public class Program
            //-----------------------------------------
            {
    
                //-------------------------------------
                public static void Main()
                //-------------------------------------
                {
                    // Your c# Code here
                }
    
            }
        }
    "@
    
    
    [MyProgram.Program]::Main()
    
    }