Powershell脚本过早退出ForEach-Object循环

时间:2017-09-03 20:42:25

标签: powershell foreach

所以我一直在编写一个脚本,它将获取存储在238个电子表格中的所有数据并将其复制到主表格中,以及9个高级报表单。我真的不确定原因,但在特定文档之后,脚本过早结束而没有发布任何错误。这很奇怪。我将在下面发布一些匿名代码,所以也许有人可以帮我找到我的方式错误。

据我所知,它之后退出的文件很好。我没有看到任何数据错误,并且在powershell完全调用它完全退出脚本之前,信息被成功复制到主文档。

我尝试通过仅限制包含问题文件的文件夹来更改数据集的大小。它仍然在没有错误输出的同一文件之后结束。由于公司政策,我无法上传文件,但与其他任何相同性质的文件相比,我真的没有看到该文件的数据有任何不同。

此外,请为蹩脚的代码提前道歉。我不是开发人员,并且已经重新学习PowerShell,因为它是我现在唯一可用的工具。

$StartTime = Get-Date -Format g
Write-Host $StartTime
pushd "Z:\Shared Documents\IO"
$TrackTemplate = "C:\Users\USERNAME\Desktop\IODATA\MasterTemplate.xlsx"

# Initialize the Master Spreadsheet
$xlMaster = New-Object -ComObject Excel.Application
$xlMaster.Visible = $False
$xlMaster.DisplayAlerts = $False
$MasterFilePath = "C:\Users\USERNAME\Desktop\IODATA\Master.xlsx"
Copy-Item $TrackTemplate $MasterFilePath
$wbMaster = $xlMaster.Workbooks.Open($MasterFilePath)
$wsMaster = $wbMaster.Worksheets.Item(2)
$wsMaster.Unprotect("PASSWORD")
$wsMasterRow = 3

# Initialize L4 Document Object
$xlL4 = New-Object -ComObject Excel.Application
$xlL4.Visible = $False
$xlL4.DisplayAlerts = $False

# Initialize object for input documents
$xlInput = New-Object -ComObject Excel.Application
$xlInput.Visible = $False
$xlInput.DisplayAlerts = $False

# Arrays used to create folder path names
$ArrayRoot = @("FOLDER1","FOLDER2","FOLDER3","FOLDER4","FOLDER5","FOLDER6","FOLDER7","FOLDER8","FOLDER9")
$ArrayShort = @("SUB1","SUB2","SUB3","SUB4","SUB5","SUB6","SUB7","SUB8","SUB9")
# $counter is used to iterate inside the loop over the short name array.
$counter = 0
$FileNumber = 0
$TotalFiles = 238
$ArrayRoot | ForEach-Object {
    $FilePathL4 = "C:\Users\USERNAME\Desktop\IODATA\ROLLUP\" + $ArrayShort[$counter] + "_DOC_ROLLUP.xlsx"
    Copy-Item $TrackTemplate $FilePathL4
    $wbL4 = $xlL4.Workbooks.Open($FilePathL4)
    $wsL4 = $wbL4.Worksheets.Item(2)
    $wsL4.Unprotect("PASSWORD")
    $wsL4Row = 3
    If ($ArrayShort[$counter] -eq "SUB7") {$FilePath = "Z:\Shared Documents\IO\" + $_ + "\" + $ArrayShort[$counter] + " - DOC v2\"}
    Else {$FilePath = "Z:\Shared Documents\IO\" + $_ + "\!" + $ArrayShort[$counter] + " - DOC v2\"}
    Get-ChildItem -Path $FilePath | ForEach-Object {
        If ($_.Name -eq "SPECIFIC_DOC.xlsx") {Continue}
        $FileNumber += 1
        Write-Host "$FileNumber / $TotalFiles $_"
        $wbInput = $xlInput.Workbooks.Open($_.FullName)
        $wsInput = $wbInput.Worksheets.Item(2)
        $wsInputLastRow = 0

        #Find the last row in the Input document
        For ($i = 3; $i -le 10000; $i++) {
            If ([string]::IsNullOrEmpty($wsInput.Cells.Item($i,1).Value2)) {
                $wsInputLastRow = $i - 1
                Break
            }
            Else { Continue }
        }

        [void]$wsInput.Range("A3:AC$wsInputLastRow").Copy()
        Start-Sleep -Seconds 1
        [void]$wsMaster.Range("A$wsMasterRow").PasteSpecial(-4163)
        Start-Sleep -Seconds 1
        $wsMasterRow += $wsInputLastRow - 2
        [void]$wsL4.Range("A$wsL4Row").PasteSpecial(-4163)
        Start-Sleep -Seconds 1
        $wsL4Row += $wsInputLastRow - 2

        $wbInput.Close()
        $wbMaster.Save()
    }
    $counter += 1
    $wsL4.Protect("PASSWORD")
    $wbL4.Save()
    $wbL4.Close()
}
$wsMaster.Protect("PASSWORD")
$wbMaster.Save()
$wbMaster.Close()
$xlMaster.Quit()

$EndTime = Get-Date -Format g
$TimeTotal = New-Timespan -Start $StartTime -End $EndTime
Write-Host $TimeTotal

1 个答案:

答案 0 :(得分:0)

要继续使用下一个输入对象进行管道处理,请在传递给return cmdlet的脚本块中使用continue - 而不是ForEach-Object

以下简单示例跳过Get-ChildItem输出的第一个对象,并将剩余的对象通过:

$i = 0; Get-ChildItem | ForEach-Object{ if ($i++ -eq 0) { return }; $_ }

目前(PSv5.1)没有直接的方式停止处理更多输入对象 - 对于变通方法,请参阅我的this answer

相比之下,正如您所发现的那样,breakcontinue仅在for / foreach 声明的脚本块中按预期工作,而不是直接在传递给ForeEach-Object cmdlet 的脚本块中:

例如,以下产生 no 输出(使用break会产生相同的效果):

$i = 0; Get-ChildItem | ForEach-Object{ if ($i++ -eq 0) { continue }; $_ }

原因是continuebreak寻找一个封闭的for / foreach 声明来继续/突破,以及没有,整个命令都退出了;在脚本中,如果调用堆栈中没有包含for / foreach / switch语句,则退出整个脚本。