从我的Powershell函数中获取无输出

时间:2017-04-24 20:38:00

标签: powershell

很抱歉,如果你看过这段代码,但是在我接受了答案并得到了一些有趣的结果之后我确实做了一些调整,并且无法重新打开以前的线程继续。

我的powershell似乎正在运行但函数调用在函数execute $filestore之后没有产生任何结果。到底是怎么回事?由于缺少全局变量,我的函数是不是在读取输入?

$filestore = Import-Excel 'C:\594 Sample of Filestore.xlsx'
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]$filestore
    )

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

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

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


            $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]
        $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
    }

    Write-Output $fileArray





}

function CopyFiles {

    param(
        [string]$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'
    echo $filestore
    $fileArray = @(FindFiles($fileArray))

    echo "test"
    echo $fileArray
    #CopyFiles($fileArray)
}

1 个答案:

答案 0 :(得分:2)

FindFiles结束时添加以下行:

Write-Output $GuidArray

这会将该变量返回到标准输出流(管道)。

FindFiles2结束时,您需要这样做:

Write-Output $fileArray