I am attempting to write a cmdlet based PowerShell module for an application we have. After the initial connection cmdlet, I want to store values somewhere while the user has their current PowerShell window open. One of the values is an auth key that I create from the user's credentials and then is required for future calls, without providing credentials for every action.
I thought it might be easy to get and set these in a custom object from C#, but I am struggling to get it to apply to the current runspace. I will admit, that I am pretty new to C# too.
Here is an example of my most successful attempt in a minimal form i.e. this will evaluate as true, however, it's not the same runspace. If I call $item in PowerShell, it obviously doesn't know what I am going on about.
using System.Management.Automation.Runspaces;
namespace Test
{
public class VariableTest
{
public bool CreateVariable()
{
var runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
runSpace.SessionStateProxy.SetVariable("item", "FooBar");
var a = runSpace.SessionStateProxy.PSVariable.GetValue("item");
if (a.ToString() == "FooBar")
{
return true;
}
else
{
return false;
}
}
}
}
If I was writing this module in PowerShell, I'd probably just save them in a global variable. Can anyone help with getting and setting these values so I can call item in the PowerShell console? Or is this terribly wrong and something exists to already account for persistent module wide variables within C# that I have missed.