Powershell 工作流在内存和崩溃时发出嘎嘎声

Powershell Workflow Chugging at Memory and Crashing

我正在尝试使用 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 的文件。

工作流在做什么导致它吃内存、花很长时间并崩溃?它显然在做一些意想不到的事情。同样,如果目录中只有几个文件,它就可以工作。

此外,solution/workaround 会很棒。

研究:

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

Running Windows PowerShell Commands in a Workflow

这里的问题似乎与对象数据的保留有关。添加 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
}