我创建了一个脚本来测试两点之间的互联网连接,但是〜1hr之后,脚本失败并
脚本由于通话深度溢出而失败。
我模糊地理解了我创建的递归行为的概念,但是正在寻找一种方法来使该脚本可以连续运行几天甚至没有问题。
0435081769$targetAddress = 'Target-PC'
$outputDir = 'C:\Support\Logs'
$outputFile = 'NetworkStabilityLog.csv'
function Set-ConsoleWindow() {
$pshost = Get-Host
$pswindow = $PSHost.UI.RawUI
$newsize = $PSWindow.WindowSize
$newsize.Width = 50
$newsize.Height = 15
$PSWindow.WindowSize = $newsize
}
function Check-Dir() {
if (!(Test-Path $outputDir\$outputFile)) {
New-Item -ItemType Directory -Force -Path $outputDir
}
Add-Content $outputDir\$outputFile "Target Address, Status, Date"
Clear
}
function Display-Message() {
Write-Host "Testing connection to $targetAddress in progress"
Write-Host
Write-Host "Close window to stop test."
Write-Host
}
function Ping-Network() {
$date = Get-Date
$checkIP = Test-Connection -ComputerName "$targetAddress" -Quiet -Count 1 -BufferSize 1
if ($checkIP -Match "False") {
Add-Content $outputDir\$outputFile "$targetAddress, Fail, $date"
Write-Host "Connection Down at $Date"
}
Sleep-Script
}
function Sleep-Script() {
Start-Sleep -Seconds 1
Ping-Network
}
Set-ConsoleWindow
Check-Dir
Display-Message
Ping-Network
答案 0 :(得分:3)
Ping-Network
和Sleep-Script
相互递归调用,而不会中断。当然,那里到处都是溢出。不要那样做 永远!
如果要无限循环:请进行无限循环。从代码中删除函数Sleep-Script
,并用以下内容替换最后一行(Ping-Network
):
while ($true) {
Ping-Network
Start-Sleep -Seconds 1
}