Problems trying to register Elasticsearch.NET/NEST in PowerShell

I've been looking into using elasticsearch.net and nest via powershell instead of re-inventing the wheel with all the different type of web-requests. As dotnet DLLs, it should be possible to use them with PowerShell via Add-Type and/or [System.Reflection.Assembly]Load methods.

I've loaded the dependencies of system.buffers & System.Diagnostics.DiagnosticSource into memory, but still receive an error message:

Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.3.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

Has anyone been able to successfully implement use of these clients with PowerShell?

Never tried to do this, but there may be some pointers in the elastico PowerShell module codebase, which provides cmdlets for operations using the .NET client.

You may also be interested in checking out Elastic.Console which provides a few cmdlets for working with Elasticsearch from PowerShell 3.0+ and Core.

Thanks Russ,

I'd taken a look at those previously, but unfortunately one of them is just wrapping the web requests, and the other actually using C# files in the module. .

Timing is mad actually, as I've just found something that i still can't understand, which is that I'm able to successfully load the Elasticsearch.NET and NEST types if I launch them from PowerShell 7 as opposed to Windows PowerShell. That's despite the fact that I specifically use the full framework dlls.

It might be that somehow the Elasticsearch.NET full framework DLL is targeting the net core diagnosticsource one somehow, but really not sure, PowerShell is more my thing than C#.

I'll see if the methods and properties work. If I need to introduce PowerShell 7 LTS to my work to make it happen, then I'll see what I can do.

Will update with findings. :slight_smile:

Windows PowerShell would need to use the full framework TFM of the client, which is .NET Framework 4.6.1 for later releases such as NEST and Elasticsearch.Net 7.6.2. Under dependencies for .NETFramework 4.6.1, it's using System.Diagnostics.DiagnosticSource >= 4.5.1, which will be .NETFramework 4.6 version of System.Diagnostics.DiagnosticSource nuget package, version 4.5.1 or greater. From there, you'll need to go through its dependencies, etc.

I'm curious to understand why something like Elastic.Console doesn't fit your needs? You'd be able to transform any json responses with jq, add Newtonsoft.Json and use Json.NET's linq types, or use PowerShell's ConvertFrom-Json (though it's a little clunky).

Yeah, I'd had a look at the DLLs with Dotpeek and downloaded versions for the dependencies that matched the requirements, but didn't have any joy.

I've had a look at elastic.console, but for the initial stage I've already written functions which target the main api functions via invoke-restmethod, do stuff like coverting pscustomobjects to json before attaching it to the body. It'd be handy no doubt if I hadn't already got to that stage.

But for next stage, the ElasticSeach.NET & NEST would mainly offer me a way with a client to not have to re-invent the wheel by being able to use their methods, functions en all in my own PS classes and only requiring to register a few dlls.