Не удалось загрузить файл или сборку «Microsoft.PowerShell.Security» или одну из ее зависимостей. Система не может найти указанный файл

Я запускаю PowerShell из консольного приложения С#. В моем ящике разработчика установлен PowerShell 3.0 — на целевых машинах, вероятно, будет версия 2.0. В моем файле проекта есть ссылка на сборку:

<Reference Include="System.Management.Automation" />

который кажется рекомендуемым способом. Я получаю исключение System.IO.FileNotFoundException при вызове runspace.Open():

using (var runspace = RunspaceFactory.CreateRunspace(initialSessionState))
{
    runspace.AvailabilityChanged += runspace_AvailabilityChanged;
    runspace.StateChanged += runspace_StateChanged;
    runspace.Open();
    ...
}

Детали исключения:

Сообщение: не удалось загрузить файл или сборку «Microsoft.PowerShell.Security» или одну из ее зависимостей. Система не может найти указанный файл.

FusionLog:

=== Pre-bind state information ===
LOG: User = MYDOMAIN\MyUser
LOG: DisplayName = Microsoft.PowerShell.Security
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Microsoft.PowerShell.Security | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\MyUser.MYDOMAIN\Projects\PoSh\PoShRunner1\bin\Debug\PoShRunner1.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.DLL.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security.EXE.
LOG: Attempting download of new URL file:///C:/Users/MyUser.MYDOMAIN/Projects/PoSh/PoShRunner1/bin/Debug/Microsoft.PowerShell.Security/Microsoft.PowerShell.Security.EXE.

Если я отключу исключения Common Language Runtime Exceptions в VS 2012, я получу

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.DllNotFoundException' occurred in System.Management.Automation.dll
A first chance exception of type 'System.Management.Automation.Host.HostException' occurred in System.Management.Automation.dll

в выходном журнале.

Я просмотрел документ, на который есть ссылка в журнале слияния, но большая его часть, похоже, не применима к этому делу или не очень помогает моему пониманию. Microsoft.PowerShell.Security находится в GAC, так в чем проблема?


person serialhobbyist    schedule 12.05.2013    source источник


Ответы (1)


Проблема в том, что вы скомпилировали сборку powershell 3.0 system.management.automation (s.m.a.) — 3.0.0.0 — вместо powershell 2.0 s.m.a, которая равна 1.0.0.0.

Откройте свой проект, перейдите к версии 1.0.0.0 и перекомпилируйте. PowerShell 3.0 устанавливает сборки s.m.a. 1.0.0.0 и 3.0.0.0 в GAC. Тогда ваша программа будет работать в системах с версиями powershell 2.0 или 3.0.

person x0n    schedule 12.05.2013