4

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.

Vega
  • 27,856
  • 27
  • 95
  • 103
Paulo Rocha
  • 87
  • 2
  • 9
  • Does the executable msg.exe exist on both system? This replaced "net send" starting with Vista, and I believe this is what your command going to invoke. – mjolinor Nov 28 '13 at 18:59
  • Yes, and if I type the command directly in powershell, it works fine. As I said, it happens to every file in ths system32 (msg, psexec, tscmd, etc..) – Paulo Rocha Nov 28 '13 at 19:11
  • Are you creating a Custom Cmdlet ???? – Aravind Nov 29 '13 at 04:45

1 Answers1

4

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.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • This seems to be the problem. I've tried to use the comand as 'c:\windows\system32\msg.exe' but didn't work either. But them I've copied some of the files as msg, psexec, tscmd, etc to the 'C:\windows\syswow64' and it starts to work. Thanks a lot Keith, great help. – Paulo Rocha Nov 29 '13 at 09:59