Passing an array to a function

Hello,
I’m passing an array to a function. The array passed is an object. I’m making a copy of the array object, via the Invoke function. Then, appending a single item to the array. Then, re-assigning the $stringList = $Collection
In my test example, I intentionally put down a file that I know doesn’t exit.
The problem is, when the array object returns from the function, it should print out only the template name, but it is printing out other items, like the path to the file.
My end goal is pretty simple - pass an array to a function, if it doesn’t exist, remove the first element, “file exists” - replace that “file exists” with the template name. Return the array.
Below is the code/results. Any suggestions/feedback would be appreciated.

#Function prints out an array
 
function letsPassAnArray($yoPathToFile, $yoTemplateName, $yoExtension,  [string[]]$stringList)
{
 
$meDateTest = Get-Date -Format "yyyyMMdd"
 
 
#Begin to formulate the entire path to be based off of something like
$myEntireFilePath = $yoPathToFile + $yoTemplateName + $meDateTest  + $yoExtension
 
 
    #Begin to create the full path to the required file
 
    $yoPathToFile
 
    #$meExists = Test-Path -Path E:\reports\processes.txt
 
    $URL = "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/"
 
import-Module SharePointPnPPowerShellOnline
 
 
Connect-PnPOnline "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/" -useWebLogin -ErrorAction SilentlyContinue
 
$FileExists = Get-PnPFile  $myEntireFilePath
 
 
#Checking to see if the file exists on sharepoint
    If($FileExists)
{
    Write-host -f Green "$myEntireFilePath File Exists yahoo!"   
}
Else #Remove the array element and append the template name see https://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html
{
    #If the first element in the array is file exists, remove that element,
    #and put in the element that doesn't exist
    if($stringList[0] -eq “file exists”)
    {
       $Collection = {$stringList}.Invoke()
       $Collection.Remove("file exists")
          $Collection.Add($yoTemplateName)
        #Re-assign the data from $Collection to the $stringList
        $stringList = $Collection
    }
    else #In the scneario where the file doesn't exist, and the first element 
         #does not equal file exists, then, you simply need to append the first template name in the string
    {
        $stringList.Add($yoTemplateName)
    }
}
 
      Write-Host "Beginning of return on 10/3/23"
   foreach ($arrEle in $stringList)
   {
        Write-Host $arrEle
   }
 
    return $stringList
 
 
}
 
#Function prints out a value passed to it
function letsPrintOutAValueFunction($yoPathToFile)
{
Write-Output $yoPathToFile
 
}
 
 
#1. Create an array with file first element in the array equaling "file exists"
 
#2. Call a function, you'll pass three parameters, the array with the "file exists" as the first parameter, the full file
#path as the second parameter, and the file name as the third parameter.
 
#3. Check in the function if the first element in the array is "file exists" and file does exist as the first element,
#pop out that first element, and insert the file name in the array element[0]
 
#4. Check in the function if the first element in the array is "file name that doesn't exist" and the file doesn't exist,
#then push the file that doesn't exist to the array.
 
#5. If the file does exist, then do nothing.
 
 
[string[]]$stringArr = 'file exists yahoo'
 
$arrayListFileExists = New-Object -TypeName 'System.Collections.ArrayList';
$arrayListFileExists.Add("file exists")
 
$mePathToFile = "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/Documents/Toad_test/Export/tGram/"
$meTemplateName = 'tGramDs'
$meFileExistsStatus = "file exists"
$stringList = letsPassAnArray $mePathToFile $meTemplateName ".xlsm" $meFileExistsStatus
$meTemplateName = 'crcTest'
#When looping via this array, it is showing 
Write-Host "Beginning to write on 10/02/23"
    foreach ($arrEle in $stringList)
    {
        Write-Host $arrEle
    }

Beginning to write on 10/02/23
https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/Documents/Toad_test/Export/tGram/
True
tGramDs

PowerShell returns all unassigned values. So where you have $yoPathToFile on line 15, that’s being returned with your collection.

function Test-Output($path) {

    $path
    $collection = [System.Collections.Generic.List[String]]::new()
    $collection.Add('abc')
    
    $collection # return the collection 
}

$result = Test-Output -path 'E:\Temp'
$result

Thank you - will look into the unassigned values.

Hi,
Commenting out the unassigned values did help. Unfortunately, it didn’t solve the problem completely though.
Below are the updated results. The problem appears to be, is that the “True” seems to have been appended to the array. Do you know why the “True” would have been appended to the array? Where I don’t believe it is appended anywhere, and not seeing that it is an undeclared variable?

To hide this message set the environment variable PNPLEGACYMESSAGE to "false"
In PowerShell add $env:PNPLEGACYMESSAGE='false' to your profile. Alternatively use 'Connect-PnPOnline -Url [yo
ururl] -WarningAction Ignore'
Get-PnPFile : File Not Found.
At C:\Work\Programming\sharepointDrive\powerShell\checkFileExistsNewNewest.ps1:28 char:15
+ $FileExists = Get-PnPFile  $myEntireFilePath
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-PnPFile], ServerException
    + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFile
Beginning of return on 10/3/23
file exists
Beginning to write on 10/02/23
True
tGramDs

Please post your updated code.

#Function prints out an array
 
function letsPassAnArray($yoPathToFile, $yoTemplateName, $yoExtension,  [string[]]$stringList)
{
 
$meDateTest = Get-Date -Format "yyyyMMdd"
 
 
#Begin to formulate the entire path to be based off of something like
$myEntireFilePath = $yoPathToFile + $yoTemplateName + $meDateTest  + $yoExtension
 
 
    #Begin to create the full path to the required file
 
    #COMMENTED OUT ON 10/17/23 as it is unassigned, and don't want it returned. $yoPathToFile
 
    #$meExists = Test-Path -Path E:\reports\processes.txt
 
    $URL = "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/"
 
import-Module SharePointPnPPowerShellOnline
 
 
Connect-PnPOnline "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/" -useWebLogin -ErrorAction SilentlyContinue
 
$FileExists = Get-PnPFile  $myEntireFilePath
 
 
#Checking to see if the file exists on sharepoint
    If($FileExists)
{
    Write-host -f Green "$myEntireFilePath File Exists yahoo!"   
}
Else #Remove the array element and append the template name see https://www.jonathanmedd.net/2014/01/adding-and-removing-items-from-a-powershell-array.html
{
    #If the first element in the array is file exists, remove that element,
    #and put in the element that doesn't exist
    if($stringList[0] -eq “file exists”)
    {
       $Collection = {$stringList}.Invoke()
       $Collection.Remove("file exists")
          $Collection.Add($yoTemplateName)
        #Re-assign the data from $Collection to the $stringList
        #COMMENTED OUT ON 10/16/23 $stringList = $Collection
    }
    else #In the scneario where the file doesn't exist, and the first element 
         #does not equal file exists, then, you simply need to append the first template name in the string
    {
        $stringList.Add($yoTemplateName)
    }
}
 
      Write-Host "Beginning of return on 10/3/23"
   foreach ($arrEle in $stringList)
   {
        Write-Host $arrEle
   }
 
   #COMMENTED OUT ON 10/16/23 return $stringList
   return  $Collection
 
 
}
 
#Function prints out a value passed to it
function letsPrintOutAValueFunction($yoPathToFile)
{
Write-Output $yoPathToFile
 
}
 
 
#1. Create an array with file first element in the array equaling "file exists"
 
#2. Call a function, you'll pass three parameters, the array with the "file exists" as the first parameter, the full file
#path as the second parameter, and the file name as the third parameter.
 
#3. Check in the function if the first element in the array is "file exists" and file does exist as the first element,
#pop out that first element, and insert the file name in the array element[0]
 
#4. Check in the function if the first element in the array is "file name that doesn't exist" and the file doesn't exist,
#then push the file that doesn't exist to the array.
 
#5. If the file does exist, then do nothing.
 
 
[string[]]$stringArr = 'file exists yahoo'
 
$arrayListFileExists = New-Object -TypeName 'System.Collections.ArrayList';
$arrayListFileExists.Add("file exists")
 
$mePathToFile = "https://comcastcorp.sharepoint.com/sites/NEDIV-FIN-COLLECTIONS-COE/Documents/Toad_test/Export/tGram/"
$meTemplateName = 'tGramDs'
$meFileExistsStatus = "file exists"
$stringList = letsPassAnArray $mePathToFile $meTemplateName ".xlsm" $meFileExistsStatus
$meTemplateName = 'crcTest'
#When looping via this array, it is showing 
Write-Host "Beginning to write on 10/02/23"
    foreach ($arrEle in $stringList)
    {
        Write-Host $arrEle
    }

Hi - I reposted the code.

Some methods will return output, you can supress this by piping to Out-Null.

This line is causing the problem:

$Collection = {$stringList}.Invoke()

Change it to:

$Collection = {$stringList}.Invoke() | Out-Null