代码之家  ›  专栏  ›  技术社区  ›  Scott Langham

为什么从C调用PowerShell会引发System.Management.Automation.CommandNotFoundException?

  •  5
  • Scott Langham  · 技术社区  · 15 年前

    我用这个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计算机上的PowerShell控制台中,该脚本也可以工作。

    你能为我可能遗漏的东西提供帮助吗?

    非常感谢。

    2 回复  |  直到 7 年前
        2
  •  0
  •   GrayDwarf    7 年前

    param([string[]]$listOfVMNames, [string]$userName, [string]$resourceGroupName, [int]$waitForJob)
    if ($pshome -like "*syswow64*") {
        & (join-path ($pshome -replace "syswow64", "sysnative") powershell.exe) -file `
            (join-path $psscriptroot $myinvocation.mycommand) -listOfVMNames (,$listOfVMNames) -userName $userName -resourceGroupName $resourceGroupName -waitForJob $waitForJob
        exit
    }
    
    推荐文章