[英]PowerShell cmdlet Install-WindowsFeature called from C# errors and is not listed in the collections of commands
[英]Powershell commands from C# 'the term is not recognizes as cmdlet'
我在从 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
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.