Reputation: 70
In powershell, I have a function where I want to return multiple values and use said values as positional arguments to a second function. Unfortunately, it is returning the group of values as an array. How do I avoid this?
Additionally, can I avoid this behavior without introducing new variables? I understand I could pass the returned array to a variable and splat it as arguments to the function, but I would like to avoid that if possible.
Code that demonstrates the issue I'm having is as follows:
function Return-Values{
return "One", "Two", "Three"
}
function Print-Args{
param($One,$Two,$Three)
Write-Host "1" $One
Write-Host "2" $Two
Write-Host "3" $Three
}
Print-Args (Return-Values)
The output is:
1 One Two Three
2
3
I expect the output to be:
1 One
2 Two
3 Three
Upvotes: 0
Views: 378
Reputation: 2208
You could use About Splatting. I was not able to get the function work as you wished. The following example does what you wish with one line more code. Maybe someone else knows another way.
function Return-Values{
return "One", "Two", "Three"
}
[System.Array]$InputArray = Return-Values #Get the input values as an array
function Print-Args{
param($One,$Two,$Three)
Write-Host "1" $One
Write-Host "2" $Two
Write-Host "3" $Three
}
Print-Args @InputArray #Use splatting for the input parameters
Upvotes: 1