Powershell 处理Com对象终止错误

Powershell 处理Com对象终止错误,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,好的,问题是,LoadFiles选项随机不喜欢某个输入文件,并产生错误。此错误始终是终止错误,我无法找到任何方法使其继续。有什么想法吗 Function ProcessImage { Param( [Parameter(ValueFromPipeline=$true)] [System.IO.FileInfo]$File ) If ($Excluded_Owners -notcontains $(get-acl -path $File.F

好的,问题是,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”时发生异常:“ 段已分配,无法锁定。“位于 L:\MediaFinder.ps1:400字符:83
+If($Images-和$ImageFileTypes-包含“*”+$.Extension){ProcessImage您已捕获catch块中的终止错误,并将其转换为非终止错误。这是第一个重要步骤。顺便说一句,catch块中的
continue
也可能导致过早终止。
continue
用于循环和
Trap
语句。请将其删除nd替换为
返回
语句

您的函数不处理任何其他文件的原因是它写得不太正确。因此,第二步是将您的脚本放入进程块中,以便它可以处理管道中传递的每个
$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

我只是简单地添加了changed continue with return,脚本现在运行得非常完美,谢谢。至于“流程”块,使用进程块有什么好处?我在许多不同的脚本中看到过它,但我不确定我是否看到了使用它的好处。如果您想在管道中使用函数时处理的不仅仅是最后一个管道对象,那么进程块是必需的(如上面的示例用法所示)。如果不指定开始/进程/结束块,PowerShell只会将所有脚本放入
end
块中。当管道完成执行时,该脚本块只执行一次。如果希望为管道中的每个对象调用命令,则需要将相关代码放入
进程{}
block。该块中的$File参数将为每个新对象更新。