我在从 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/