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.
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. –
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. –
Czy byłeś w stanie to zrobić? Utknąłem z podobnym problemem i chciałbym wiedzieć, jak rozwiązać tę – satyajit