12

At the moment I am trying to pass in two array items by reference to a function I have written which loads data into those arrays. However, once this function loses scope the arrays appear as blank.

If I use the ref keyword to pass them into the function the first array loads up correctly. However, the second array gives an error saying that I cannot use the add operator on it.

$logConfigPath = "C:\Testing\Configuration\config.xml"

#### VARIABLES RELATING TO THE LOG FILE

# Contains the log path and log file mask
$logPaths = @()
$logFileMasks = @()

#### FUNCTION CALLS
LoadLogTailerConfig($logConfigPath, $logPaths, $logFileMasks)

"$logPaths"
"$logFileMasks"


function LoadLogTailerConfig($logConfigPath, $logPath, $logFileMasks)
{
    Write-Debug "Loading config file data from $logConfigPath"

    [xml]$configData = Get-Content "C:\Testing\Configuration\config.xml"

    foreach ($log in $configData.Logs.Log) {

        $logPaths += $log.FilePath
        $logFileMasks += $log.FileMask
    }
}

Why is this not working for me?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew Pigram
  • 1,400
  • 3
  • 25
  • 65
  • possible duplicate of [Powershell passing argument values to parameters and back](http://stackoverflow.com/questions/9222285/powershell-passing-argument-values-to-parameters-and-back) – Andrew Savinykh Feb 03 '15 at 03:36

2 Answers2

14

I modified your example to work:

$logConfigPath = "C:\Testing\Configuration\config.xml"

#### VARIABLES RELATING TO THE LOG FILE

# Contains the log path and log file mask
$logPaths = @()
$logFileMasks = @()


function LoadLogTailerConfig($logConfigPath, [ref]$logPaths, [ref]$logFileMasks)
{
    Write-Debug "Loading config file data from $logConfigPath"

    #[xml]$configData = Get-Content "C:\Testing\Configuration\config.xml"

    foreach ($log in 1..10) {

        $logPaths.value += $log
        $logFileMasks.value += $log
    }
}

#### FUNCTION CALLS
LoadLogTailerConfig $logConfigPath ([ref]$logPaths) ([ref]$logFileMasks)

"$logPaths"
"$logFileMasks"

Notes:

  • Different syntax for calling functions in PowerShell
  • You need to define your function first then call not the other way around
  • Make sure that you use correct parameter names. Your functions accept $logPath, but then it tries to modify $logPaths - of course that's not going to work as expected because of the extra s at the end
  • You need to use [ref] both in the function definition and function call
  • You need to access reference value by adding .value to the reference variable

Also refer to the almost identical prior question here: Powershell passing argument values to parameters and back

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • Peter Mortensen, I prefer no full-stops at the end of bullet points. This is a stylistic choice, there is not need to edit that. Thanks! – Andrew Savinykh Dec 28 '18 at 19:38
0

You are complaining about a problem that's not the first problem in your code. You are using the function LoadLogTrailerConfig without defining it before its usage. So, you must correct this gaffe first. After the correction, your code will run without syntax error, but logically it will still present an undesired output, but the explanation will then, be simple: array is an imutable reference object.

  • that code is actually contained in a main function, I just removed it to make it more clear and easy to ready, so it actually does run through fine, the error is to do with the output I get from the `keywords` variable and `maxCounts`. The code provided by zespri works – Matthew Pigram Feb 03 '15 at 06:27