I'm extremely new to PowerShell, but I have a lot of C# experience. I have written an API wrapper in C# and I would like to expose it as PowerShell Cmdlets. The typical flow in C# is a pretty standard:
var client = new MyClient("username", "password");
var endpointData = client.GetSomeEndpointData();
The question is, what is the proper flow for something like this in PowerShell and how to persist it only for that session. I think it should look something like the following.
Import-Module MyClient.dll
New-MyClient "username" "password"
Get-SomeEndpointData
So my questions are
- Is this the "correct" flow for something like this
- What does this look like in C# code for creating the Cmdlets, specifically
- how should the
MyClientinstance be persisted for the current PowerShell session - How should the
MyClientinstance be accessed in the other Cmdlets that require that instance?
- how should the
To be clear, I don't need actual code (examples would be nice), but my google-fu might just be lacking today. MSDN documentation (or equivalent) would be sufficient.
MyClientis similar enough to your background inC#that I don't foresee you running into any issues there within the existing session. If you wanted the same instance ofMyClientto carry-over into other scripts and such, then check out Dot Sourcing.MyClient(or suitable wrapper) which the user would call methods on. Otherwise you typically see these sort of "ambient" objects being stored in static fields in some class of the module. The cmdlets in the module just all reference that static field to get whatever they need.