gpt4 book ai didi

c# - 为什么从 C# 调用 PowerShell 会抛出 System.Management.Automation.CommandNotFoundException?

转载 作者:太空狗 更新时间:2023-10-29 23:03:52 26 4
gpt4 key购买 nike

我正在使用这个 C#:

    public bool RunPowershell(string script)
{
RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();

using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfig))
{
runspace.Open();

using (RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace))
{
scriptInvoker.Invoke(script);
}
}

return true;
}

要运行这个脚本:

      Add-PSSnapin -name Microsoft.SystemCenter.VirtualMachineManager
$vmm = Get-VMMServer -ComputerName "VmmComputerName"

它在 Windows 2003 32 位操作系统上工作正常,但在 Windows 2008R2 64 位操作系统上,我收到此错误:

System.Management.Automation.CommandNotFoundException: The term 'Get-VMMServer' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandOrigin commandOrigin)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.CommandFactory._CreateCommand(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command)
at System.Management.Automation.CommandNode.CreateCommandProcessor(Int32& index, ExecutionContext context)
at System.Management.Automation.CommandNode.AddToPipeline(PipelineProcessor pipeline, ExecutionContext context)
at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)
at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.AssignmentStatementNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

而且,我已经安装了 Microsoft.SystemCenter.VirtualMachineManager。如果我将脚本手动输入到 2008R2 机器上的 power-shell 控制台,该脚本也能正常工作。

您能否就我可能遗漏的任何想法提供帮助?

非常感谢。

最佳答案

出现这种情况是因为 powershell 管理单元元数据记录在注册表中。在您的情况下,这意味着管理单元信息仅在注册表的 32 位软件配置单元中可用。通常使它可用的技巧是使用 64 位版本的 .NET 框架的 installutil.exe(在 framework64 目录中)来注册它,但有时它是 32 位的只是出于某种原因。这可能取决于在 64 位环境中不可用的 32 位 COM 对象。

所以你有两种方法:

1) 使用 installutil.exe/i 为 64 位注册管理单元(不太可能工作)

2) 仅通过 VS 项目属性将 .NET exe 定位为 32 位 (anycpu -> x86)

3) 将您的工作总结在如下脚本中:http://www.nivot.org/blog/post/2012/12/18/Ensuring-a-PowerShell-script-will-always-run-in-a-64-bit-shell

-奥伊辛

关于c# - 为什么从 C# 调用 PowerShell 会抛出 System.Management.Automation.CommandNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3719440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com