0

In the below code I am having trouble understanding why when I return FileArray why my other function is returning it as being empty?

I am looking to use copyfiles with FileArray. Am I just supposed to put all the steps into one huge function?

function Import-Excel
{
  param (
    [string]$FileName,
    [string]$WorksheetName,
    [bool]$DisplayProgress = $true
  )

  if ($FileName -eq "") {
    throw "Please provide path to the Excel file"
    Exit
  }

  if (-not (Test-Path $FileName)) {
    throw "Path '$FileName' does not exist."
    exit
  }

  $FileName = Resolve-Path $FileName
  $excel = New-Object -com "Excel.Application"
  $excel.Visible = $false
  $workbook = $excel.workbooks.open($FileName)

  if (-not $WorksheetName) {
    Write-Warning "Defaulting to the first worksheet in workbook."
    $sheet = $workbook.ActiveSheet
  } else {
    $sheet = $workbook.Sheets.Item($WorksheetName)
  }

  if (-not $sheet)
  {
    throw "Unable to open worksheet $WorksheetName"
    exit
  }

  $sheetName = $sheet.Name
  $columns = $sheet.UsedRange.Columns.Count
  $lines = $sheet.UsedRange.Rows.Count

  Write-Warning "Worksheet $sheetName contains $columns columns and $lines lines of data"

  $fields = @()

  for ($column = 1; $column -le $columns; $column ++) {
    $fieldName = $sheet.Cells.Item.Invoke(1, $column).Value2
    if ($fieldName -eq $null) {
      $fieldName = "Column" + $column.ToString()
    }
    $fields += $fieldName
  }

  $line = 2


  for ($line = 2; $line -le $lines; $line ++) {
    $values = New-Object object[] $columns
    for ($column = 1; $column -le $columns; $column++) {
      $values[$column - 1] = $sheet.Cells.Item.Invoke($line, $column).Value2
    }  

    $row = New-Object psobject
    $fields | foreach-object -begin {$i = 0} -process {
      $row | Add-Member -MemberType noteproperty -Name $fields[$i] -Value $values[$i]; $i++
    }
    $row
    $percents = [math]::round((($line/$lines) * 100), 0)
    if ($DisplayProgress) {
      Write-Progress -Activity:"Importing from Excel file $FileName" -Status:"Imported $line of total $lines lines ($percents%)" -PercentComplete:$percents
    }
  }
  $workbook.Close()
  $excel.Quit()
}

function FindFiles {

    param(
        [string]$fiestore
    )

    $length = $filestore.Length
    $GuidArray = @()

    for($line=0;$line -le $filestore.Count;$line++){

            $check = $filestore[$line]
            $length2 = $check.Length
            echo $check

            $fileGuid = $check | ForEach-Object{$_.FileGuid}





            $GuidArray = $GuidArray + $fileGuid    
    }

    write-host "-------------------------------------------------------------" -ForegroundColor Yellow

    $filepath = Read-Host " Please Enter File Path to Search"

    for ($counter=0;$counter -lt $GuidArray.Count;$counter++){
        $fileArray = @()
        $guidcheck = $GuidArray[$counter]
        echo $guidcheck
        $file = Get-ChildItem -Recurse -Force $filePath -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $false) -and  ( $_.Name -like "*$guidcheck*") } | Select-Object Directory,Name| Format-Table -AutoSize 
        $fileArray += $file
    }
    echo $fileArray

    return $fileArray


}

function CopyFiles {

    param(
        [string]$filearray
    )
    echo $fileArray
    for($counter = 0;$counter -lt $filearrray.Count;$counter++){
        echo $filearray[$counter]
        #Copy-Item 
    }

}

function execute {
    $filestore = Import-Excel 'C:\594 Sample of Filestore.xlsx'
    $fileArray = @()
    FindFiles($filestore)
    echo $fileArray
    CopyFiles($fileArray)
}

1 Answer 1

1

$fileArray doesn't become available outside of the Function by doing Return, but you could make it accessible outside of the function by defining it with a Global scope (although this is not best practice): Return $Global:fileArray.

Instead, it becomes the value of the Function call itself, so in Execute you could do:

$filestore = Import-Excel 'C:\594 Sample of Filestore.xlsx'
$fileArray = @(FindFiles($filestore))
echo $fileArray
CopyFiles($fileArray)

However I think you also need to remove any echo statements from within the FindFiles function or they may be returned as well.

Note: This is untested code.

Sign up to request clarification or add additional context in comments.

3 Comments

ahh ok so just call the function inside a variable and the variable value will become the function result. Weird!
Basically any command that outputs to standard out (e.g write-output or just writing a string) will become the output of the Function.
I tried this but havent been able to get an output. here is a link to my other thread if you are interested .stackoverflow.com/questions/43597297/…

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.