清除内存/ powershell缓冲区每3秒运行一次无限循环

问题描述 投票:0回答:1

我创建了一个PowerShell脚本,它只是解析日志文件,如果最后一行包含关键字“error”,它会发送一封电子邮件并运行一个微小的鼠标宏 - 循环每3秒重复一次 - 无限循环

if ($lastSent -ne $currentLine -and $currentLine -match "Error") {
            if ($currentLine -match "Error23") {
                [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(225, 305)
                sleep -Seconds 01
                $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
                $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
                sleep -Seconds 01
                [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(60, 305)
                sleep -Seconds 01
                $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
                $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
                sleep -Seconds 01

}

我已经测试了过去,离开了运行应用程序好几天,它很好,现在运行应用程序和这个powershell脚本。

然而,在12h到18h之后,有时甚至一天左右,计算机冻结,我无法RDP或ping或任何东西,完全冻结 - 所以它是powershell脚本导致它。

有人可以告诉我任何powershell cmd或者我可以将它添加到循环中,清除内存或缓冲区

谢谢

powershell powershell-v3.0 powershell-v4.0
1个回答
0
投票

如果您没有对脚本进行任何更改,并且它在过去工作正常,那么恕我直言,该脚本将不是我的第一个故障排除目标。

环境在你的系统上发生了变化。意思是应用程序或主机本身。

你可以强制垃圾收集,这是一种方式......

### Clear resource environment

Function Clear-ResourceEnvironment
{
    # Clear any PowerShell sessions created
    Get-PSSession | Remove-PSSession

    # Release an COM object created
    $null = [System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$Shell)

    # Perform garbage collection on session resources 
    [System.GC]::Collect()         
    [GC]::Collect()
    [GC]::WaitForPendingFinalizers()

    # Remove any custom varialbes created
    Get-Variable -Name MyShell -ErrorAction SilentlyContinue | Remove-Variable
}

就个人而言,我真的无法想象为什么你选择这种方法,而不是制作这个永久的WMI观察者,或者说这样做......

实时监控日志...

# Take action as soon as the incoming line equals error
Get-Content -Path .\SomeLogFileName -Wait -Tail 0 | 
Where-Object {$_ -match 'error'}

你为什么多次打电话?

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(60, 305)
sleep -Seconds 01
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);

叫这个......

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(60, 305)

...在脚本的顶部。

做这个......

sleep -Seconds 01
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
sleep -Seconds 01

...一个函数并调用函数vs复制代码,所以,像。

Function Send-MouseClick
{
    sleep -Seconds 01
    $SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
    $SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
    sleep -Seconds 01    
}

if ($lastSent -ne $currentLine -and $currentLine -match "Error") 
{
    if ($currentLine -match "Error23") {
    Send-MouseClick
}

只是一些想法,但显然没有经过测试。

© www.soinside.com 2019 - 2024. All rights reserved.