清除PowerShell每3秒运行一次死循环的内存/缓冲区 [英] clear memory/Buffer of powershell running endless loop every 3 seconds
本文介绍了清除PowerShell每3秒运行一次死循环的内存/缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个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脚本了。
然而,在12小时到18小时之后,有时甚至一天左右的时间,计算机都会死机,我无法进行RDP或ping或其他操作,完全死机--所以是PowerShell脚本导致的。
有人能告诉我任何PowerShell命令吗?我可以将其添加到清除内存或缓冲区的循环中
谢谢
推荐答案
如果您没有对脚本进行任何更改,并且它过去运行良好,那么我认为该脚本不会是我的第一个疑难解答目标。
您的系统上发生了某些环境变化。表示该应用程序或主机本身。
您可以强制垃圾收集,这是单向…### 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
.函数并调用该函数,而不是复制代码,因此,类似于。
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
}
只是我脑海中的一些想法,但显然没有经过测试。
这篇关于清除PowerShell每3秒运行一次死循环的内存/缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文