【问题标题】:Powershell - Calling C# function inside a jobPowershell - 在作业中调用 C# 函数
【发布时间】:2020-04-09 23:39:06
【问题描述】:

我有一个 PowerShell 程序,里面有 C# 代码。 我希望 C# 函数将在新工作中运行。 我试过这个-

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

看起来作业已执行,但什么也没发生。

【问题讨论】:

  • “它不起作用”是什么意思?你有错误信息吗?
  • 无法工作,因为 scriptblock 不知道 c# 类的声明。
  • @f6a4 那么有没有办法使用 start-job 执行 C# 函数?
  • @shon 作业在单独的进程中执行,因此您需要在调用 Main() 之前编译/执行 {} 脚本块中的 C# 代码
  • @shon 向我们展示您的整个脚本(包括 C# 代码)

标签: c# powershell powershell-2.0 jobs


【解决方案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()

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-17
    • 2011-05-09
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多