Hi,
I want to create a function to write a message to multiple variables. I'm trying to do this via [ref]:
function My-Write-Output () {
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[array]$message,
[Parameter()]
[ref]$var
)
foreach ($line in $message) {
foreach ($ref in $var) {
if ($append) {
$ref.value += "$line"
} else {
$ref.value = "$line"
}
}
}
}
This works with 1 variable:
PS C:\Scripts> . '.\Common Helpers.ps1'; $variable1 = @(10); $variable2 = 20; My-Write-Output -message "Blub" -var ([ref]$variable1) -append;
$variable1; $variable2
10
Blub
20
PS C:\Scripts>
How can I do this with multiple variables?
PS C:\Scripts> . '.\Common Helpers.ps1'; $variable1 = @(10); $variable2 = @(20); My-Write-Output -message "Blub" -var ([ref]$variable1), ([ref
]$variable2) -append; $variable1; $variable2
My-Write-Output : Cannot process argument transformation on parameter 'var'. Reference type is expected in argument.
At line:1 char:104
+ ... put -message "Blub" -var ([ref]$variable1), ([ref]$variable2) -append ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [My-Write-Output], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,My-Write-Output
10
20
PS C:\Scripts>
Thank you!