2017-04-24 68 views
0

对不起,如果你看过这段代码,但我接受了答案后做了一些调整,并得到了一些有趣的结果,并无法重新打开之前的线程继续。从我的Powershell函数获取无输出

我的powershell似乎正在运行,但功能执行后$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) 
} 
+0

我得到的输出测试,但之前所有的呼叫,没有任何生产出 – goldenwest

+4

后从函数调用一个相当邪恶的东西。顺便说一下,throw也会停止函数执行,并且不会杀死整个shell。 – Joey

+0

退出只在import-excel方面调用,以获得作为对象输入的excel,但发布它不起作用 – goldenwest

回答

2

在的FindFiles末尾添加这一行:

Write-Output $GuidArray 

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

在相若方式的FindFiles2结束时,您需要做的:

Write-Output $fileArray 
+0

现在尝试它会报告返回结果 – goldenwest

+0

powershell ISE是非常buggy – goldenwest

+0

hmm仍然得到与写输出插入 – goldenwest