I’m writing a module to use the Force.com API. I have two core functions:
- New-ForceSession - returns a session ID and servicing server URL
- Invoke-ForceRest - this is what actually is calling (Invoke-RESTMethod) to Force.com. This is the function that needs the session information
The remaining functions are Get and Set functions that basically build a URL and JSON parameters and call the Invoke-ForceRest. The current way I have written it is you create a session and then pass the session to each respective Get and Set with a session parameter.
$session = New-ForceSession -User email@example.com -Password "blu3b!rd" Get-Something -Session $session
Everything works great. I just don’t like having to pass the session information from my Get-Something that calls Invoke-ForceRest which actually needs the session information. If I call New-ForceSession in Invoke-ForceREST, I need to get it the username, password, etc. and I’ll be calling it over and over again for each respective Get\Set call I make.
Is there a better way to store the session information globally in the module versus passing it every time? If I set global variables in New-ForceSession (e.g. $script:SessionID = $myVar.SessionID), is that the best way to do it? I’m just trying to figure out the cleanest way to do something like this:
New-ForceSession -User firstname.lastname@example.org -Password "blu3b!rd" Get-Something | Set-Something
Just looking for best practice recommendations. As I said, the current code works great, it just drives me a little crazy that I have to pass the same thing over and over.