2017-04-24 165 views
0

在下面的代碼中,我無法理解爲什麼當我返回FileArray爲什麼我的其他函數將它返回爲空?如何返回值在另一個函數中使用powershell

我正在尋找與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) 
} 

回答

1

$fileArray不會成爲功能外可用做Return,但你可以通過全球範圍內定義它讓訪問的功能外(雖然這不是最好的做法):Return $Global:fileArray

相反,它變成了函數的值調用本身,所以在執行你可以這樣做:

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

不過,我想你也需要從FindFiles函數中刪除任何echo陳述或者他們可能會也返回。

注意:這是未經測試的代碼。

+0

ahh ok所以只需調用變量內部的函數,變量值將成爲函數結果。奇怪的! – goldenwest

+0

非常感謝 – goldenwest

+1

基本上任何輸出到標準輸出的命令(例如'write-output'或者只是寫一個字符串)都將成爲函數的輸出。 –

相關問題