Powershell工作流程在内存和崩溃中肆虐

时间:2015-01-27 22:44:15

标签: powershell memory-management workflow-foundation

我在PowerShell中涉及工作流程,而且我注意到一些奇怪的行为。当目录不包含大量文件时,以下脚本将起作用。在某一点之后,它将保持在第6行(当您在ise中运行时,您将看到工作流程状态栏),咀嚼内存,然后最终崩溃(至少半小时后)。当文件目录至少为1.25GB时发生此崩溃,但是当$Path只有50mb的文件时则不会发生此崩溃。这是一个简单的测试:

Workflow Test-Me {
    Param
    (
        $Path = "c:\temp",
        $Days = 0
    )
    $Files = InlineScript{
                Get-ChildItem -Path $using:Path -File -Recurse -Force | Where-Object {$_.LastWriteTime -lt ((get-date).AddDays(-$using:Days))}
             }
    $Files
}

现在奇怪的是,当Get-ChildItem -Path $using:Path -File -Recurse -Force | Where-Object {$_.LastWriteTime -lt ((get-date).AddDays(-$using:Days))}从工作流程外部运行时(在常规函数中或仅在shell的一行上),它在不到一分钟的时间内完成,即使是1.25GB的文件

工作流程是什么导致它吃掉内存,花费很长时间,然后崩溃?它显然做了一些意想不到的事情。同样,如果目录中只有少数文件,它就可以工作。

此外,解决方案/解决方案会很棒。

研究:

Activity to invoke the Microsoft.PowerShell.Management\Get-ChildItem command in a workflow

Running Windows PowerShell Commands in a Workflow

1 个答案:

答案 0 :(得分:1)

此处的问题似乎与保留对象数据有关。添加select会大大减少返回的对象数据的大小,以便搜索100GB +不会导致崩溃。解决方案如下:

Workflow Test-Me {
    Param
    (
        $Path = "c:\temp",
        $Days = 0
    )
    $Files = InlineScript{
                Get-ChildItem -Path $using:Path -File -Recurse -Force | Where-Object {$_.LastWriteTime -lt ((get-date).AddDays(-$using:Days))} | select filename
             }
    $Files
}