当文件不再存在时,工作流会抛出错误'GetVersionInfo'

时间:2016-07-20 13:19:56

标签: powershell workflow

考虑这种情况:

Subject<Student> applicationStream

所有执行正常且没有错误。现在删除文件后,工作流不再执行而没有错误:

$Error.Clear()

Workflow Test-Workflow {
    Param (
        [Parameter(Mandatory)]
        [Array]$File
    )
    'Workflow executed'
}

$File = New-Item $env:TEMP\test.txt -ItemType File -Force

$Object = [PSCustomObject]@{
    File = $File
    Test = 'Stuff'
}

Test-Workflow -File $Object 
$Error

报告以下错误两次:

$Error.Clear()
Remove-Item $File
Test-Workflow -File $Object 
$Error

这是正常行为吗?如何避免这种情况?因为我们想在执行工作流之前归档文件。或者我是否遗漏了一些关于工作流细节的超级明显的东西?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我认为发生的是$object存储对您创建的文件的引用。然后,您删除了引用的文件。这就是你有FileNotFoundException

的原因
#Delete your file
Remove-Item $File
#Try to pass reference to deleted file to Workflow, when it looks it finds no file and fails 
Test-Workflow -File $Object

因为这种失败是正常的行为。您可以做的是在归档之前创建文件的临时副本,并在处理后删除临时文件。否则我认为您必须将文件的内容加载到内存中,并且只对内存中的对象进行操作,并确保没有查找已删除的文件。