当相同的 dll 在 Visual Studio 中正常加载时,如何处理在 PowerShell 中尝试添加类型时未找到的引用?

How to deal with reference not found when trying to Add-Type in PowerShell, when the same dll loads fine in Visual Studio?

我正在尝试从 PowerShell 引用 IBM.Data.DB2 和 IBM.Data.Informix 库。

我有一些 Visual Studio 测试代码,我在其中简单地添加了对那些 dll 的引用并开始编码,一切都很顺利。

尝试在 PowerShell 中添加相同的引用时,我尝试了以下操作:

try{
$DataType4 = Add-Type -Path "C:\Program Files\IBM\SQLLIB\BIN\netf40\IBM.Data.DB2.dll"
}
catch
{
$_.Exception | Format-List * -force
}

当尝试 运行 时,我得到一个扩展的错误列表,它抱怨找不到 Microsoft.ReportingServices(我认为它没有安装在这台机器上;我已经打印出来了我的 GAC 并没有出现在那里,尽管有很多对 Microsoft.ReportViewer 的引用;成功的 Visual Studio 项目在其引用中不包含 Microsoft.ReportingServices)。从表面上看,LoaderException 多次包含此信息,所以我只包含了一份:

Types        : {IBM.Data.DB2.CS_FitHighPrecisionType, IBM.Data.DB2.LLIST, IBM.Data.DB2.LISTLOCK, IBM.Data.DB2.ADP...}
LoaderExceptions : {System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ReportingServices.Interfaces, Version=10.0.0.0, Culture=neutral, 
               PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.
               File name: 'Microsoft.ReportingServices.Interfaces, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91'

               WRN: Assembly binding logging is turned OFF.
               To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
               Note: There is some performance penalty associated with assembly bind failure logging.
StackTrace   :    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
                  at System.Reflection.Assembly.GetTypes()
                  at Microsoft.PowerShell.Commands.AddTypeCommand.LoadAssemblyFromPathOrName(List`1 generatedTypes)
                  at Microsoft.PowerShell.Commands.AddTypeCommand.EndProcessing()
                  at System.Management.Automation.CommandProcessorBase.Complete()

谁能解释为什么这似乎 运行 在 Visual Studio 中很好,但在 PowerShell 中却不行?我是否正确检索异常?有什么方法可以解决缺少的 Microsoft 组件,因为 Visual Studio 没有它也能很好地应对?

我觉得我做的事情显然是错误的,而且正朝着风车倾斜,但我似乎找不到任何其他提及此问题的地方。

编辑: 这是我的小测试项目及其参考的截图。它只是获取 table 数据并将其转储到 DataGridView 中,但它显示了使用这些驱动程序连接连接所需的最少编码量。

周末重启后,我修改了我的答案:

事实证明,在加载过程中抛出的异常不是致命的,因此捕获它们并忽略它们对于测试代码来说效果很好。 如果 Add-Type 以更严重的方式失败,我在整个执行块周围有另一个 try-catch,实际上试图在 "real" 加载失败时访问 Informix 对象会抛出一个被捕获的异常那里。

包含测试代码以供未来搜索者使用:

try{
$DataType7 = Add-Type -Path "C:\Program Files\IBM\SQLLIB\BIN\netf40\IBM.Data.Informix.dll"
}
catch
{
    #Throw a horrendous mass of exceptions, which we ignore, but output.
    $_.Exception | Format-List * -force
}


$ifxCStr = New-Object -TypeName IBM.Data.Informix.IfxConnectionStringBuilder
$ifxCStr.Server = "x"
$ifxCStr.Database = "x"
$ifxCStr.UserID = "x"
$ifxCStr.Password = "x"

$ifxConn = New-Object -TypeName IBM.Data.Informix.IfxConnection($ifxCStr.ConnectionString)
$ifxConn.Open()


$ifxCommand = $ifxConn.CreateCommand()
$ifxCommand.CommandText = "SELECT FIRST 100 * FROM tbl_x"

$ifxReader = $ifxCommand.ExecuteReader()

$ifxTable = New-Object System.Data.DataTable
$ifxTable.Load($ifxReader)
$ifxTable | Out-GridView

$ifxConn.Close()