如何返回要在另一个函数powershell中使用的值

如何返回要在另一个函数powershell中使用的值,powershell,Powershell,在下面的代码中,我很难理解为什么当我返回FileArray时,为什么我的另一个函数返回为空 我正在寻找与FileArray一起使用copyfiles。我应该把所有的步骤都放在一个巨大的功能中吗 function Import-Excel { param ( [string]$FileName, [string]$WorksheetName, [bool]$DisplayProgress = $true ) if ($FileName -eq "") {

在下面的代码中,我很难理解为什么当我返回
FileArray
时,为什么我的另一个函数返回为空

我正在寻找与FileArray一起使用copyfiles。我应该把所有的步骤都放在一个巨大的功能中吗

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)
}

$fileArray
不能通过执行
Return
而在函数外部可用,但您可以通过定义全局范围使其在函数外部可访问(尽管这不是最佳做法):
Return$Global:fileArray

相反,它成为函数调用本身的值,因此在Execute中,您可以执行以下操作:

$filestore = Import-Excel 'C:\594 Sample of Filestore.xlsx'
$fileArray = @(FindFiles($filestore))
echo $fileArray
CopyFiles($fileArray)
但是,我认为您还需要从
FindFiles
函数中删除任何
echo
语句,否则它们也可能会被返回


注意:这是未经测试的代码。

ahh ok,因此只需在变量内调用函数,变量值将成为函数结果。奇怪的基本上,任何输出到标准输出的命令(例如
write output
或只是写入一个字符串)都将成为函数的输出。我尝试了这个方法,但没有得到输出。如果你有兴趣,这里有一个链接到我的另一个帖子。