简体   繁体   English

来自 C# 的 Powershell 命令“该术语不被识别为 cmdlet”

[英]Powershell commands from C# 'the term is not recognizes as cmdlet'

I'm having a problem to execute powershell commands from the C# application.我在从 C# 应用程序执行 powershell 命令时遇到问题。 I've found many things related to this issue, but none of them helped me to figured it out what might is going on.我发现了很多与这个问题相关的事情,但没有一个能帮助我弄清楚可能发生了什么。

So I have this little test function:所以我有这个小测试功能:

    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();
    }

The problem is that in some computers it works just fine, but in others I receive the message that "the term 'msg' is not recognized as a cmdlet, function,etc."问题是在某些计算机中它工作得很好,但在其他计算机中我收到消息“术语'msg'未被识别为cmdlet、函数等。” This is happening with every executable file that exists in c:\\windows\\system32. c:\\windows\\system32 中存在的每个可执行文件都会发生这种情况。 When I use a cmdlet like 'Get-Process', it works fine...当我使用像“Get-Process”这样的 cmdlet 时,它工作正常......

I'm testing in two computers right now, both of them have the ExecutionPolicy seted to unrestricted and they have the same Powershell version.我现在正在两台计算机上进行测试,它们都将 ExecutionPolicy 设置为不受限制,并且它们具有相同的 Powershell 版本。 The 'Path' on the Environment Variables are the same too.环境变量上的“路径”也相同。

How are you compiling your C# application?你是如何编译你的 C# 应用程序的? If it is compiled as x86 platform then it will be using the virtualized System32 dir C:\\windows\\syswow64 and there is no msg.exe in that dir.如果它被编译为 x86 平台,那么它将使用虚拟化的System32目录C:\\windows\\syswow64并且该目录中没有 msg.exe。 You can either A) compile as x64 or B) use the path C:\\windows\\sysnative\\msg.exe .您可以 A) 编译为 x64 或 B) 使用路径C:\\windows\\sysnative\\msg.exe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM