clear memory/Buffer of powershell running endless loop every 3 seconds

0

I have created a powershell script which is just parsing the log file and if last line contains keyword "error" it sends out an email and runs a tiny mouse macro - cycle repeats itself every 3 seconds - endless loop

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

}

I have tested in past, left running the application for days and it was fine, now its running the application and this powershell script.

however after 12h to 18h sometimes even a day or so the computer freezes and i cannot RDP or ping or anything, completely freezes - so it is the powershell script causing it.

Can someone tell my any powershell cmd or something i can add it to the loop which clears the memory or buffer

Thank You

powershell
powershell-3.0
powershell-4.0
asked on Stack Overflow Mar 3, 2019 by Aj Saini

1 Answer

0

If you've made no changes to the script and it worked fine in the past then IMHO the script would not be my first troubleshooting target.

Something environmental has change on you system. Meaning that application or the host itself.

You can force garbage collection, this is one way …

### 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
}

Personally, I really can't imagine why you choose this approach, vs making this permanent WMI watcher, or say doing this...

Monitor the log real time...

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

Why are you calling this multiple times?

[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);

Call this ...

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

...at the top of your script.

Make this ...

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

... a function and call the function vs duplicating code, so, something like.

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
}

Just some ideas off the top of my head, but obviously not tested.

answered on Stack Overflow Mar 3, 2019 by postanote • edited Mar 3, 2019 by postanote

User contributions licensed under CC BY-SA 3.0