我使用Add-Type加载了一个程序集:
$Typename = '\\crtwfaadvlkv0.d2dbfg.com\PRODUCTION\Vision\Apps\VisionPipeline\Oracle.ManagedDataAccess.dll'
Add-Type -LiteralPath $TypeName并确认它已装入
> [appdomain]::CurrentDomain.GetAssemblies() |
>> Sort-Object -Property FullName |
>> Select-Object -Property FullName;
(partial results)
Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342接下来,我想加载在程序集中定义的类,以便可以使用它们,但下面的错误如下:
> $oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassThru
Add-Type : Cannot add type. The assembly 'Oracle.ManagedDataAccess' could not be found.
At line:1 char:14
+ ... oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassTh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Oracle.ManagedDataAccess:String) [Add-Type], Exception
+ FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. One or more required assemblies are missing.
At line:1 char:14
+ ... oracletpe = Add-Type -AssemblyName 'Oracle.ManagedDataAccess' -PassTh ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand因此,看起来PowerShell找不到它刚刚加载的程序集。我做错了什么?
发布于 2020-11-25 14:43:18
如果你不想添加两次,那么你就不需要第二次加载。
正如我在评论中提到的,只需在第一个“添加类型”命令中添加"-PassThru“参数即可。
$assemblyFilePath = '\\crtwfaadvlkv0.d2dbfg.com\PRODUCTION\Vision\Apps\VisionPipeline\Oracle.ManagedDataAccess.dll'
$assembly = Add-Type -LiteralPath $assemblyFilePath -PassThru
$assembly但如果要重新加载它,请使用程序集限定名:
Add-Type -AssemblyName 'Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342' -PassThru通常,只允许全局程序集缓存(GAC)中的程序集和应用程序文件夹中的程序集(递归)使用部分名称。(但是有一些特殊的扩展,比如SQL Server和其他服务器的“主机程序集存储”)。
但不要将“应用程序文件夹”与脚本所在的文件夹混淆。这是PowerShell应用程序文件夹:%WinDir%\System32\WindowsPowerShell\v1.0\如果您想测试它,只需将DLL放在该文件夹中并启动一个新的powershell控制台即可。
有关程序集加载的更多信息(尤其是使用部分名称):https://docs.microsoft.com/en-us/dotnet/framework/deployment/best-practices-for-assembly-loading#avoid-binding-on-partial-assembly-names
https://stackoverflow.com/questions/64754177
复制相似问题