Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Powershell to add types from an assembly it just loaded

I loaded an assembly using Add-Type:

 $Typename = '\\crtwfaadvlkv0.d2dbfg.com\PRODUCTION\Vision\Apps\VisionPipeline\Oracle.ManagedDataAccess.dll'
 Add-Type -LiteralPath $TypeName

and confirmed it was loaded

> [appdomain]::CurrentDomain.GetAssemblies() |
>> Sort-Object -Property FullName |
>> Select-Object -Property FullName;

(partial results)

Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342

Next I want to load the classes defined in the assembly so I can use them, but this errors out:

> $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

So, it looks like PowerShell can't find the assembly it just loaded. What am I doing wrong?

like image 781
user1443098 Avatar asked Oct 25 '25 17:10

user1443098


1 Answers

If you don't want to add it twice, then you don't need the second loading.

As mentioned in my comment, just add the "-PassThru" parameter to the first "Add-Type" command.

$assemblyFilePath = '\\crtwfaadvlkv0.d2dbfg.com\PRODUCTION\Vision\Apps\VisionPipeline\Oracle.ManagedDataAccess.dll'
$assembly = Add-Type -LiteralPath $assemblyFilePath -PassThru

$assembly

But if you want to reload it, use the assembly qualified name:

Add-Type -AssemblyName 'Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342' -PassThru

Partial Names are, in general, only allowed for assemblies in the Global Assembly Cache (GAC) and Assemblies in the application folder (recursive). (but there are some special extensions for that like "host assembly store" for SQL Server and others).

But don't confuse the "application folder" with the folder your script is located in. It's the PowerShell Application Folder: %WinDir%\System32\WindowsPowerShell\v1.0\ If you want to test it, just place the DLL in that folder and start a new powershell console.

More information about assembly loading (especially with partial name): https://learn.microsoft.com/en-us/dotnet/framework/deployment/best-practices-for-assembly-loading#avoid-binding-on-partial-assembly-names

like image 84
swbbl Avatar answered Oct 29 '25 19:10

swbbl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!