处理Com对象终止错误

时间:2014-09-14 00:14:23

标签: powershell powershell-v2.0 powershell-v3.0

好的,这是问题,随机的LoadFiles选项不喜欢某个输入文件并产生错误。此错误始终是终止错误,我无法找到任何方法让它继续。任何想法?

Function ProcessImage {
    Param(
        [Parameter(ValueFromPipeline=$true)]
        [System.IO.FileInfo]$File
    )

    If ($Excluded_Owners -notcontains $(get-acl -path $File.FullName).owner) {                                         #Check owner of the file and compare it to list of blacklisted file owners.
        Try{
            $Image = New-Object -ComObject Wia.ImageFile
            $Image.LoadFile($File.fullname)
        } Catch{
            LogWriter -OutPut "File Failed Process File in WIA - `"$($File.fullname)`""
            Write-Error $_.Exception.Message
            continue
        }

        If($Image.width -gt $PictureMinWidth -or $Image.height -gt $PictureMinHeight) {                                #Check image dimensions.
            IF ($Script:Copy) {
                $CopyTryCount = 0
                While ((Test-Path -Path "$CopyDir\$($Script:MF_ImagesCopied + 1)$($File.extension)" -PathType Leaf) -EQ $False -AND $CopyTryCount -le 3) {           #After the script says the picture was copied without error, verify it indeed was.
                    $CopyTryCount++
                    Try {
                        Copy-Item -Path $File.FullName -Destination "$CopyDir\$($Script:MF_ImagesCopied + 1)$($File.extension)" -Force                                    #If the picture meets all requirements, attempt to copy the image.
                    } Catch {
                        LogWriter -Status "Failure" -Output "File Failed to Copy (Attempt $CopyTryCount) - `"$($File.fullname)`""
                    }
                }
                IF (Test-Path -Path "$CopyDir\$($Script:MF_ImagesCopied + 1)$($File.extension)" -PathType Leaf) {                                         #Check the CopyDir directory for the image.
                    LogWriter -Status "Success" -Output "File Successfully Copied - `"$($File.fullname)`""             #If the image was copied successfully, log that.
                    [Int]$Script:MF_ImagesCopied += 1
                    $Temp_ProcessImage_Success=$True
                } Else {
                    LogWriter -Status "Failure" -Output "File Failed to Copy after 3 tries - `"$($File.fullname)`""    #If the image was not copied successfully, log that.
                    [Int]$Script:MF_ImagesFailed+= 1
                }
            }
        } Else {
            LogWriter -Status "Skipped" -Output "Incorrect Dimensions - `"$($File.fullname)`""
            [Int]$Script:MF_ImagesSkipped += 1
        }
    } Else {
        LogWriter -Status "Skipped" -Output "Excluded Owner - `"$($File.fullname)`""
        [Int]$Script:MF_ImagesSkipped += 1
    }
}#End ProcessImage

这是一个麻烦的错误。

  

ProcessImage:使用“1”参数调用“LoadFile”的异常:“The   段已经丢弃且无法锁定。 “ 在   L:\ MediaFinder.ps1:400 char:83   + If($ Images -AND $ ImageFileTypes -contains“*”+ $ .Extension){ProcessImage<<<< $ }       + CategoryInfo:NotSpecified:(:) [Write-Error],WriteErrorException       + FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,ProcessImage

1 个答案:

答案 0 :(得分:1)

您已捕获catch块中的终止错误并将其转换为非终止错误。这是第一个重要的步骤。 BTW你的catch区块中的continue也可能导致提前终止。 Continue旨在与循环和Trap语句一起使用。删除它并替换为return语句。

您的函数不处理任何其他文件的原因是它写得不是很正确。所以第二步是将你的脚本放在一个进程块中,这样它就可以处理传递给管道的每个$File对象,例如:

function ProcessImage {
    param(
        [Parameter(ValueFromPipeline=$true)]
        $File
    )

    process {
        try {
            if ($file -eq 'foo') {
                throw 'kaboom'
            } 
            else {
                "Processing $file"
            }
        } 
        catch {
            Write-Error $_
            return # can't continue - don't have valid file obj
        }
        "Still processing $file" 
    }
}

如果我使用这些参数运行上述内容,您可以看到它在抛出终止错误的对象之后处理对象:

C:\PS> 'bar','foo','baz' | ProcessImage
Processing bar
Still processing bar
ProcessImage : kaboom
At line:1 char:21
+ 'bar','foo','baz' | ProcessImage
+                     ~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ProcessImage

Processing baz
Still processing baz