Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top