Return object from PowerShell using a parameter ("By Reference" parameter)? Return object from PowerShell using a parameter ("By Reference" parameter)? powershell powershell

Return object from PowerShell using a parameter ("By Reference" parameter)?


Ref should work, you don't say what happened when you tried it. Here is an example:

Test.ps1:

Param ([ref]$OptionalOutput)"Standard output"$OptionalOutput.Value = "Optional Output"

Run it:

$x = "".\Test.ps1 ([ref]$x)$x

Here is an alternative that you might like better.

Test.ps1:

Param ($OptionalOutput)"Standard output"if ($OptionalOutput) {    $OptionalOutput | Add-Member NoteProperty Summary "Optional Output"}

Run it:

$x = New-Object PSObject.\Test.ps1 $x$x.Summary


Is this closer to what you want to do?

Test2.ps1

 $Issues = "Potentially long list of issues" $SummaryLine = "37 issues found" $Issues $SummaryLine

Test1.ps1

 $MainOutput,$SummaryOutput = & ".\Test2.ps1"  $MainOutput  $SummaryOutput

This:

 param([String]$SummaryLine) $Issues = "Potentially long list of issues" $SummaryLine = "37 issues found" $Issues

Is irrational. You're passing a parameter for $SummaryLine, and then immediatly replacing it with "37 issues found". That variable only exists in the scope the called script is running in. As soon as that script finishes, it's gone. If you want to use it later, you need to output it and save it to a variable in your calling script.