4

I'm trying to run a PowerShell script, and I have to load several assemblies to accomplish my task. Most assemblies are loading fine, but there's one I'm having trouble getting to work:

Microsoft.VisualStudio.Services.Common.dll

I've tried a few different approaches.

I've tried the LoadFrom, ReflectionOnlyLoadFrom, and Add-Type -Path Methods, but they all return an error saying

Could not load file or assembly 'path\Microsoft.WITDataStore64.dll' or one of its dependencies. The module was expected to contain an assembly manifest...

FulllyQualifiedErrorID : BadImageFormatException

I've tried ReflectionOnlyLoad, but that just returned an error saying

Exception calling "ReflectionOnlyLoad" with "1" argument(s): "Could not load file or assembly '$pathToDlls\\Microsoft.WITDataStore64.dll' or one of its dependencies. The given assembly name or codebase was invalid.

(Exception from HRESULT: 0x80131047)"

The path is correct - I have visually confirmed that the file is there - and the name of the dll is correct as well (copy-pasted the filename just to be sure). Other dlls are loading just fine; it's just this one that isn't loading.

Microsoft has a documents page about this exception, and it suggests that I should "[a]ccess the methods defined in the DLL by using the features provided by your development language." I think it assumes that I'm using something more than just PowerShell. If there is a way to access the DLL's methods through powershell, I am unaware of it.

What is making this dll in particular so much trickier to load, and how can I get it to load?

Community
  • 1
  • 1
MrSpudtastic
  • 205
  • 2
  • 18
  • Please check `$Error[0].Exception |fl * -Force` and `$Error[0].Exception.InnerException |fl * -Force` for more details about the underlying cause (post complete exception details rather than just the error message) – Mathias R. Jessen Jul 16 '19 at 21:03
  • Done, but the inner exception just repeats what the error message says. "Could not load file or assembly or one of its dependencies. The given assembly name or codebase was invalid." – MrSpudtastic Jul 16 '19 at 21:37
  • No error codes, HRESULT values or similar? – Mathias R. Jessen Jul 16 '19 at 21:39
  • Just re-read the question, you should probably be using `ReflectionOnlyLoadFrom()` rather than `ReflectionOnlyLoad()` – Mathias R. Jessen Jul 16 '19 at 21:41
  • Are you using 32bit PowerShell ? – Prasoon Karunan V Jul 22 '19 at 17:05
  • @PrasoonKarunanV I think I am. – MrSpudtastic Jul 22 '19 at 17:07
  • Try using 64bit PowerShell ! – Prasoon Karunan V Jul 22 '19 at 17:10
  • You might give this MS forum a read. The issue seems similar to yours and there is a work around mentioned. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2d047112-ad50-4515-811d-ba1ab53465f0/tfs-2015-sdk-missing-microsoftwitdatastore64dll?forum=tfsgeneral – phiz Jul 22 '19 at 21:51
  • Microsoft.WITDataStore64.dll will not load in a 32 or 64 bit powershell session as it is not a .net assembly. You should share your exact assembly loading code as you mention that you "load several assemblies" – Avner Jul 25 '19 at 02:29

1 Answers1

1

The error you are getting is that some code is trying to load a native dll (which lacks the magic bits that make it a CLR assembly) as an assembly.

The assembly Microsoft.VisualStudio.Services.Common.dll can be obtained at different versions via nuget, and might be installed on the local computer at any version.

I also see some related questions regarding problems and bugs loading the same assembly.

I was able to load an old version of the package from NuGet to a local folder and access it in powershell:

  • Install .net core SDK (2.0 or above)
  • Install .net 4.5.2 Developer Pack
  • Create a blank folder
  • Open a powershell prompt in the folder

Then run:

dotnet new console --target-framework-override net452
dotnet add package Microsoft.TeamFoundationServer.ExtendedClient -v 14.89.0
dotnet build .
add-type -Path .\bin\Debug\net452\Microsoft.VisualStudio.Services.Common.dll
[microsoft.visualstudio.services.common.VssEnvironment]::GetTfsSharedFilesPath()

If this works, you'll probably want to switch it to use nuget.exe - this doesn't require the SDKs to be installed but I don't know how to use it.

Peter Wishart
  • 11,600
  • 1
  • 26
  • 45