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

淺唱寂寞╮ 提交于 2019-12-02 03:19:12

问题


I'm having a problem to execute powershell commands from the C# application. 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." This is happening with every executable file that exists in c:\windows\system32. When I use a cmdlet like 'Get-Process', it works fine...

I'm testing in two computers right now, both of them have the ExecutionPolicy seted to unrestricted and they have the same Powershell version. The 'Path' on the Environment Variables are the same too.

Appreciate any help.

Thanks in advance.

Paulo


回答1:


How are you compiling your C# application? 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. You can either A) compile as x64 or B) use the path C:\windows\sysnative\msg.exe.



来源:https://stackoverflow.com/questions/20272931/powershell-commands-from-c-sharp-the-term-is-not-recognizes-as-cmdlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!