1

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

  1. Is this the "correct" flow for something like this
  2. What does this look like in C# code for creating the Cmdlets, specifically
    1. how should the MyClient instance be persisted for the current PowerShell session
    2. How should the MyClient instance be accessed in the other Cmdlets that require that instance?

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.

2
  • The scope of MyClient is similar enough to your background in C# that I don't foresee you running into any issues there within the existing session. If you wanted the same instance of MyClient to carry-over into other scripts and such, then check out Dot Sourcing. Commented Jul 28, 2016 at 18:42
  • Does it make sense to want to connect to different endpoints or use different credentials in the same session? If so I would think the cmdlet New-MyClient would return an instance of 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. Commented Jul 29, 2016 at 8:05

1 Answer 1

1

It would looks like this:

Add-Type -Path 'c:\fullpath\to\myclient.dll'
$client = New-Object MyClient("username", "password")
$endpointData = $client.GetSomeEndpointData()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.