В зависимости от правильного пакета PowerShell Nuget в нашем. NET Core WPF Project, ориентированном на PowerShell 5.1 Desktop Edition - PullRequest
0 голосов
/ 14 июля 2020

TL; DR;

Я пытаюсь создать приложение WPF Windows (. net core 3.1) для создания виртуальных машин для тестирования и воспроизведения ошибок в целях обслуживания windows для клиентов. Мне нужно иметь возможность запускать кучу сценариев PowerShell на пользовательском компьютере, чтобы создать виртуальные машины, простые команды, такие как:

Get-VmHost
Get-VMNetworkAdapter

et c.

Какой PowerShell nuget пакеты, которые я должен использовать, если я хочу настроить таргетинг на настольную версию powershell 5.1 через c#?

Подробная версия:

Что я пробовал :

Microsoft.Powershell.5.1.ReferenceAssemblies:  
Microsoft.Powershell.5.0.ReferenceAssemblies:

выбросить следующее исключение:

System.InvalidProgramException: Common Language Runtime detected an invalid program.
    at System.Management.Automation.PowerShell.Create(RunspaceMode runspace)
System.Management.Automation (7.0.2):

вызывает следующее исключение:

System.Management.Automation.Runspaces.PSSnapInException: Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: Could not load file or assembly 'D:\VMReady.Offline\VMReady.Offline.WPF\bin\x64\Debug\netcoreapp3.1\runtimes\win\lib\netcoreapp3.1\Microsoft.PowerShell.Commands.Diagnostics.dll'. The system cannot find the file specified.
    at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
    at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke()
Microsoft.PowerShell.SDK (7.0.2):  

Хотя это работает, если я запускаю приложение в режиме отладки в Visual Studio он выдает исключение, когда я запускаю тот же код после установки приложения:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
    File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
    at System.Reflection.RuntimeAssembly.GetExportedTypes()
    at System.Reflection.Assembly.get_ExportedTypes()
    at System.Management.Automation.Runspaces.PSSnapInHelpers.GetAssemblyTypes(Assembly assembly, String name)
    at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzeModuleAssemblyWithReflection(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, String helpFile, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers)
    at System.Management.Automation.Runspaces.PSSnapInHelpers.AnalyzePSSnapInAssembly(Assembly assembly, String name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, Dictionary`2& cmdlets, Dictionary`2& aliases, Dictionary`2& providers, String& helpFile)
    at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
    at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault()
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
    at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
    at System.Management.Automation.PowerShell.Invoke()

Кто-нибудь запускает сценарии PowerShell из автономного приложения WPF (встроенного . net ядро)? Любая помощь будет принята с благодарностью.

Спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...