I am trying to use PowerShell v4.0 (x86/64) against one of our internal API's to do some fairly basic stuff, but I cannot seem to get past the dependency loading.
So far I have:
[Reflection.Assembly]::LoadFrom("C:\Users\David Shaw\Desktop\API\API.dll")
as per Dat Bui's blog post.
This works fine, I then try to use a type inside this DLL:
$a = New-Object API.API("", 1234)
This gives me the following error:
New-Object : Exception calling ".ctor" with "2" argument(s): "Unable to find assembly API.Dependency,
Version=1.2.5.0, Culture=neutral, PublicKeyToken=null'."
At line:1 char:6
+ $a = New-Object API.API("", 1234)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Looking in FusionLog, the only places it looks for the dependency is:
C:\Windows\System32\WindowsPowerShell\v1.0
Things I've tried so far:
- Setting the powershell current dir.
- Writing it as a script instead of from the console.
- I have the dependency in the same location as API.dll
- Using
LoadFileinstead ofLoadFrom - Using
Add-Type -Path API.dll - Setting the .net
CurrentDirectory - Calling
LoadFromon the dependency. - Doing an AppDomain.AssemblyResolve event in Powershell see below, but this stack overflows powershell:
From my Script:
$OnAssemblyResolve = [System.ResolveEventHandler] {
param($sender, $e)
$n = New-Object System.Reflection.AssemblyName($e.Name).Name
$fn = "C:\Users\David Shaw\Desktop\API\$n.dll"
return [Reflection.Assembly]::LoadFile($fn)
}
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($OnAssemblyResolve)
Based on the comment, the API.dll is .Net 4.0 (AnyCPU) and the API.Dependency.dll is .Net 2.0 (AnyCPU). If this could be an issue, any idea's how to resolve it?
Add-Type -Path C:\Users\David Shaw\Desktop\API\API.dll; Add-Type -Path C:\Users\David Shaw\Desktop\API\API.Dependency.dlldid not work. Or at the very least point to yet another dependent assembly the loader can't find.C:\Windows\System32\WindowsPowerShell\v1.0. If that still fails, then there is something else going on (missing an assembly dependency, etc).