无法加载文件或程序集“Microsoft.PowerShell.Security”或其依赖项之一。该系统找不到指定的文件

时间:2013-05-12 10:22:50

标签: powershell powershell-v3.0 fusion

我正在从C#控制台应用程序运行PowerShell。我的开发盒有PowerShell 3.0 - 目标机器可能有v2.0。我的项目文件引用了程序集:

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

这似乎是推荐的方式。当我调用runspace.Open()时,我得到一个System.IO.FileNotFoundException:

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.

如果我在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中,那么问题是什么?

1 个答案:

答案 0 :(得分: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。然后,您的程序将适用于具有2.0或3.0版本PowerShell的系统。