I have created the following to get understanding of how a variable array is passed to a function:
# Define array
[array]$Global:P_sourceHostName = @()
[array]$Global:P_destinationHostName = @()
# Add string values to source array
$global:P_sourceHostName = "ABC"
$global:P_sourceHostName += "DEF"
$global:P_sourceHostName += "GHI"
# add string values to destination array
$global:P_destinationHostName = "zzz"
$global:P_destinationHostName += "yyy"
function test {
Param(
[string]$paramA="",
[string]$paramB=""
)
Write-Host "test function > paramA: $paramA"
Write-Host "test function > paramB: $paramB"
}
$i = 0
# Pass the individual value to a function
test ($Global:P_sourceHostName[$i],$Global:P_destinationHostName[$i])
#Pass the individual value to a function with an additional text
test ("AAA $Global:P_sourceHostName[$i]", "BBB $Global:P_destinationHostName[$i]")
What resulted is:
test function > paramA: ABC zzz test function > paramB: test function > paramA: AAA ABC DEF GHI[0] BBB zzz yyy[0] test function > paramB:
Question:
- Why the first call of
testfunction, it resulted with a blank "paramB"? - Why the second call of
testfunction, it combines the text but does not resulted in the correct array value?