2010-09-15 8 views
5

Używam tego C#:Dlaczego wywoływanie PowerShell z C# rzucić wyjątek System.Management.Automation.CommandNotFoundException?

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; 
    } 

Aby uruchomić ten skrypt:

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

To działa ok na Windows 2003 32bit OS, ale na 64-bitowych systemu Windows 2008R2, otrzymuję ten błąd:

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) 

Mam zainstalowany Microsoft.SystemCenter.VirtualMachineManager. Skrypt działa również wtedy, gdy ręcznie wpisuję go w konsoli Power-Shell na komputerze 2008R2.

Czy możesz pomóc w sprawie wszelkich pomysłów na to, czego może mi brakować?

Dziękuję bardzo.

Odpowiedz

7

Dzieje się tak, ponieważ metadane snap-in w programie Power Shell są rejestrowane w rejestrze. W twoim przypadku oznacza to, że informacja o przystawce jest dostępna tylko w 32-bitowej gałęzi oprogramowania w rejestrze. Zwykle, aby udostępnić go, należy użyć 64-bitowej wersji pliku installutil.exe platformy .NET (w katalogu framework64), aby go zarejestrować, ale czasami jest to 32-bitowy tylko z jakiegoś powodu. Może to zależeć od 32-bitowych obiektów COM, które nie są dostępne w 64-bitowym środowisku.

Więc masz dwa podejścia:

1) zarejestrować przystawkę do 64 bitów za pomocą installutil.exe/I (raczej nie działa)

lub

2) kierowanie. NET exe dla 32 bit tylko poprzez właściwości projektu VS (anycpu -> x86)

lub

3) owinąć swoją pracę w skrypcie tak: http://www.nivot.org/blog/post/2012/12/18/Ensuring-a-PowerShell-script-will-always-run-in-a-64-bit-shell

-Osin

+0

Tak, myślę, że masz rację. Chociaż w moim przypadku, myślę, że mogłem skończyć z tym na odwrót. Mój program C# jest uruchamiany z procesu WOW64 (budowanie TFS), więc kończy się jako proces 32-bitowy. Ale snapin jest dostępny tylko wtedy, gdy powershell jest uruchamiany jako natywny 64-bitowy. Dziękuję bardzo. –

+0

Myślę, że mógłbym użyć czegoś takiego: http://stackoverflow.com/questions/2003573/how-to-start-a-64-bit-process-from-a-32-bit-process aby wrócić do macierzystej 64-bitowej ziemi. –

+2

Czy byłeś w stanie to zrobić? Utknąłem z podobnym problemem i chciałbym wiedzieć, jak rozwiązać tę – satyajit

Powiązane problemy