2010-01-05 54 views
2

当我直接从Powershell控制台运行脚本时,它可以正常工作。当我在PowerGUI中运行脚本并尝试实例化一个对象时,出现错误:无法让powershell脚本在PowerGUI内部运行

调用带“3”参数的“.ctor”异常:“无法加载文件或程序集”MyLib,Version = 1.0 .0.0,Culture = neutral,PublicKeyToken = 77f676cc8f85d94e'或其依赖项之一,系统找不到指定的文件。“

如果我将所有需要的DLL放在$ PSHOME中,脚本将成功从控制台运行,但不是PowerGUI。如果我将DLL移动到本地目录并使用反射来加载DLL,该脚本将不会在PowerGUI和PowerShell控制台中运行。

[reflection.assembly]::loadfile('c:\mylibs\mylib.dll')

我需要做什么才能让脚本在PowerGUI中运行?理想情况下,我想将这些DLL放在与$ PSHOME不同的目录中。

回答

4

您应该使用[Assembly]::LoadFrom而不是LoadFile。 LoadFile用于加载不能在常规程序集加载上下文中加载的程序集,例如,您尝试加载相同程序集的两个版本的情况。它不使用正常的探测规则,因此它不会自动加载依赖关系。以下是LoadFile的文档摘录。

Use the LoadFile method to load and examine assemblies that have the same identity, but are located in different paths. LoadFile does not load files into the LoadFrom context, and does not resolve dependencies using the load path, as the LoadFrom method does. LoadFile is useful in this limited scenario because LoadFrom cannot be used to load assemblies that have the same identities but different paths; it will load only the first such assembly.

如果你正在使用PowerShell 2.0中,你可能希望使用而不是添加 - 键入:

Add-Type -Path c:\mylibs\mylib.dll 

如果一切都失败了,run Fuslogvw.exe找出为什么绑定失败。

0

使用set-psdebug -trace 2查看它正准备调用的内容。

0

这可能是因为PowerGUI是一个不同的PowerShell主机,所以它的“本地文件夹”是Program Files中的PowerGUI文件夹,而不是$ pshome - 放置DLL的位置。