❔ C# Powershell, pipe one script to another?

Is it possible to run a script and pipe that result to another? The closest I find is something like this:
System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject> res = null;
foreach (var item in this.tasks)
{
Console.WriteLine($"Executing script: {scripts[item.script].path}");
// add current script and parameters
ps.AddScript(scripts[item.script].script, true).AddParameters(item.parameters);
// check if we got a previous result
if (res is not null)
{
// check if the result has anything in it
if (res.Count > 0)
{
// add the previous result as an argument for the current script
// this seems to add the result to the first parameter of the same type
ps.AddArgument(res[0]);
}
}

// Execute script
res = await ps.InvokeAsync();
}
System.Management.Automation.PSDataCollection<System.Management.Automation.PSObject> res = null;
foreach (var item in this.tasks)
{
Console.WriteLine($"Executing script: {scripts[item.script].path}");
// add current script and parameters
ps.AddScript(scripts[item.script].script, true).AddParameters(item.parameters);
// check if we got a previous result
if (res is not null)
{
// check if the result has anything in it
if (res.Count > 0)
{
// add the previous result as an argument for the current script
// this seems to add the result to the first parameter of the same type
ps.AddArgument(res[0]);
}
}

// Execute script
res = await ps.InvokeAsync();
}
6 Replies
Omnissiah
Omnissiah2y ago
couldn't you pipe them by executing them in cmd? like "script1.ps1 | script2.ps1 | ..." ah no, you are the one of the powershell scripts job manager
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Fjärilsmannen
FjärilsmannenOP2y ago
yeah, otherwise that suggestion would work, thanks though:)
Omnissiah
Omnissiah2y ago
is ps.AddArgument not working for you in some way?
Fjärilsmannen
FjärilsmannenOP2y ago
it doesn't seem to follow the position rules that strictly, so I don't know when i can actually trust it. It says it will use the parameter at position 0, but it seems to only care about that if it's the same type (i tested with int and string). If i know what rules it's working on then i would feel safer using it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server