Powershell 无法加载文件或程序集

Powershell 无法加载文件或程序集,powershell,powershell-2.0,Powershell,Powershell 2.0,在我的powershell脚本中,我正在加载一个自定义程序集,然后通过新对象实例化该程序集的一个类 Assembly.LoadFile()成功执行,但New Object语句给出以下异常 New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=

在我的powershell脚本中,我正在加载一个自定义程序集,然后通过
新对象
实例化该程序集的一个类

Assembly.LoadFile()
成功执行,但
New Object
语句给出以下异常

New-Object : Exception calling ".ctor" with "1" argument(s): "Could not load file or assembly 'MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of i
ts dependencies. The system cannot find the file specified."
脚本:

[System.Reflection.Assembly]::LoadFile("MyAssembly.dll")
$a=New-Object MyAssembly.MyClass -ArgumentList "arg1"
此自定义程序集仅引用以下程序集

System
System.Core
System.Runtime.Serialization
System.Xml.Linq
System.Data
System.Xml
我尝试显式加载System.Runtime.Serialization dll,如下所示。但同样的例外

[System.Reflection.Assembly]::Load("System.Runtime.Serialization, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

有什么想法吗?

不要使用
LoadFile
。由于您使用的是Powershell V2,因此首选方法是添加类型

Add-Type -AssemblyName "MyLibrary.dll" 
提供了将库导入当前shell运行空间的各种方法的良好列表


是添加类型上的technet Documentation,其中有一些示例,包括如何在加载的库中使用静态方法

我在尝试从64位Windows Powershell ISE调用32位Oracle.DataAccess.dll时遇到了这个问题。后来,我在Windows Powershell ISE(x86)中使用了相同的代码,并且能够调用Oracle.DataAccess.dll文件。干杯

问:您的程序集是否在gac中注册?不,它没有在gac中注册。请尝试LoadFrom,看看它是否有效。我也会在这里抛出这个问题:Add Type对程序集非常挑剔