c# - 来自 C# 'the term is not recognizes as cmdlet' 的 Powershell 命令

标签 c# powershell powershell-cmdlet

我在从 C# 应用程序执行 powershell 命令时遇到问题。 我发现了很多与这个问题相关的事情,但没有一个能帮助我弄清楚可能发生了什么。

所以我有这个小测试函数:

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
            Runspace runspace = RunspaceFactory.CreateRunspace();

            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript("msg * test");
    
            pipeline.Invoke();

            runspace.Close();
    }

问题是在某些计算机上它工作得很好,但在其他计算机上我收到消息“术语‘msg’未被识别为 cmdlet、函数等”。 c:\windows\system32 中存在的每个可执行文件都会发生这种情况。 当我使用像“Get-Process”这样的 cmdlet 时,它工作正常......

我现在正在两台计算机上进行测试,它们都将 ExecutionPolicy 设置为不受限制,并且它们具有相同的 Powershell 版本。 环境变量上的“路径”也是一样的。

最佳答案

您如何编译 C# 应用程序?如果它被编译为 x86 平台,那么它将使用虚拟化的 System32 目录 C:\windows\syswow64 并且该目录中没有 msg.exe。您可以 A) 编译为 x64 或 B) 使用路径 C:\windows\sysnative\msg.exe

关于c# - 来自 C# 'the term is not recognizes as cmdlet' 的 Powershell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272931/

相关文章:

powershell - 如何查找用户使用了哪个 Cmdlet 别名?

c# - FFMPEG:用多帧解码 h264

C# 泛型类型导致歧义

c# - 从 Controller (表示层)内部映射域实体是否正确?

c# - 通过从组合框中选择不同的表,在 sql db 中插入新值

powershell - 为什么可以遍历空数组

c# - 脚本通过 PowerShell 控制台正确运行,通过 C# 运行时失败

powershell - PowerShell 脚本中的变量清理

powershell - 在公开 PowerShell 模块成员时,我应该更喜欢 Export-ModuleMember 还是全局范围?