There are several examples in the common parameters, like ErrorVariable, InformationVariable;
get-item foad: -ev foo
$foo
"get-item" will create and set the value for $foo to an instance of ErrorRecord. How can I create my own parameters that do something similar?
Basically, I'm creating a cmdlet that uses WriteObject() to write data to the pipeline, but then I also have some additional information that I want to allow users to access - essentially out of band data - that isn't part of the pipeline.
An example of an out parameter in C#:
public class ExampleCode
{
public int GetMyStuff(int param1, out string someVar)
{
someVar = "My out-of-band result";
return param1 + 1;
}
public static void RunMe()
{
ExampleCode ex = new ExampleCode();
string value;
int result = ex.GetMyStuff(41, out value);
Console.WriteLine($"result is {result}, OOB Data is {value}");
}
}
I'm looking for how to convert "GetMyStuff()" into a powershell cmdlet.
[Cmdlet(VerbsCommon.Get, "MyStuff")]
public class ExampleCmdLet : PSCmdlet
{
[Parameter(Mandatory = false)] int param1;
[Parameter(Mandatory = false)] string someVar; // How to make this [out] ?
protected override void ProcessRecord()
{
someVar = "My out-of-band result";
WriteObject(param1 + 1);
}
}