1

I have a problem when I'm trying to return an array from a PowerShell function.

My function:

function filter-SWCluster {
   param($Path, $FolderList)
   $OldSWCluster = New-Object System.Collections.ArrayList
   ForEach ($y in $FolderList) {
      Get-ChildItem -Path $Path -Filter $y* | sort { [version]($_.Name -replace '^.*_(\d+(\.\d+){1,3})$', '$1') } -Descending | Select-Object -skip 3 | ForEach-Object { $OldSWCluster.Add($Path + "\" + $_) }
   }
   Write-Output $OldSWCluster
}

The function call:

$FilerSWCluster = filter-SWCluster $NetworkPath $SWCluster

The output I get:

0 1 \\server.domain\C$\TEMP\Folders\Testcluster_1.0.2 \\server.domain\C$\TEMP\Folders\Testcluster_1.0.1

The output I want and need would be:

\\server.domain\C$\TEMP\Folders\Testcluster_1.0.2 \\server.domain\C$\TEMP\Folders\Testcluster_1.0.1

I need just the paths. I know there will already be some solutions, but please excuse me. I didn't find one which was working for me or I were to dumb to use it right (could be).

3
  • 1
    ArrayList's Add method returns the new index, you need to hide it e.g. $OldSWCluster.Add($Path + "\" + $_) >$null Commented Nov 10, 2016 at 9:15
  • Man you are great, it worked perfectly. Did I understood right, the add method is not just adding the value, its also adding an index. And when I'm passing back the variable, the index get passed too? Commented Nov 10, 2016 at 10:20
  • 1) it adds just the value into the array and 2) emits the index into the default output stream by using implicit Write-Output, which can be suppressed by redirecting to $null or casting to [void] as the other answer shows. Commented Nov 10, 2016 at 10:25

2 Answers 2

5

You could also wrap the Add method with the [void] class accelerator. It will hide any output, like this:

function filter-SWCluster {
   param($Path, $FolderList)
   $OldSWCluster = New-Object System.Collections.ArrayList
   ForEach ($y in $FolderList) {
      Get-ChildItem -Path $Path -Filter $y* | sort { [version]($_.Name -replace '^.*_(\d+(\.\d+){1,3})$', '$1') } -Descending | Select-Object -skip 3 | ForEach-Object { [void]$OldSWCluster.Add($Path + "\" + $_) }
   }
   Write-Output $OldSWCluster
}
Sign up to request clarification or add additional context in comments.

Comments

2

wOxxOm already provided you a solution to your problem. However, consider to just return the path using a simple select. You also don't have to join the path since the FullName property already contains the full path:

function filter-SWCluster {
   param($Path, $FolderList)
   ForEach ($y in $FolderList) {
      Get-ChildItem -Path $Path -Filter $y* |
        sort { [version]($_.Name -replace '^.*_(\d+(\.\d+){1,3})$', '$1') } -Descending |
        Select-Object -skip 3 |
        select -ExpandProperty FullName
   }
}

1 Comment

Thanks would also be an opportunity, but I cant implement it in my script like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.