2013-05-12 4 views
2

Я запускаю PowerShell из консольного приложения C#. Мой блок разработчика имеет PowerShell 3.0 - у целевых компьютеров, вероятно, будет v2.0. В файле моего проекта имеется ссылка на сборку:Не удалось загрузить файл или сборку «Microsoft.PowerShell.Security» или одну из его зависимостей. Система не может найти указанный файл

<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 исключений в 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, в чем проблема?

ответ

2

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

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

Смежные вопросы

 Смежные вопросы