c# - 无法调用此函数,因为当前主机未实现它 c#

标签 c# powershell

我知道这是在这个板上,但我是新手,没有什么能真正阻止我,所以我希望有人能提供一些见解。我收到以下错误:

无法调用此函数,因为当前主机未实现它

这是我的代码

    public static void runPowershellScript(string scriptName)
    {
        RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

        Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
        runspace.Open();

        RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
        scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

        Pipeline pipeline = runspace.CreatePipeline();
        pipeline.Commands.Add(scriptName);
        pipeline.Invoke();

    }

我传递的变量是我的脚本名称,它没有问题。

最佳答案

您可能正在使用一些特定于主机的函数,这意味着任何托管 PowerShell 引擎的进程(例如 powershell.exe、powershell_ise.exe、您的 c# 应用程序)都必须为它们提供实现。

一般来说,这些函数包括 *-host 函数(write-host、out-host、read-host)、转录函数(start-transcript、stop-transcript)和其他一些函数。

关于c# - 无法调用此函数,因为当前主机未实现它 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367561/

相关文章:

xml - Powershell - 将 XML 转换为 CSV

c# - Xamarin iOS 向其他应用程序发送命令/事件

c# - WPF 列表和上下文菜单

c# - Windows 窗体用户控件 + 面板 : WPF Equivalent

powershell - 如何使用 PowerShell 为 AD 用户对象上的属性获取有效权限

powershell - 我可以在Powershell中自定义 'not recognized as the name of a cmdlet'错误吗?

azure - 运行命令扩展正在执行。请等待完成后再调用运行命令

xml - 使用 Powershell 进行外科 XML 编辑

c# - DbContextOptionsBuilder 找不到 ASP.NET Core 1.1 Web API 项目

c# - 如何使用 Task.Run(Action<T>)