I want to create a loop in my Powershell script that will run for a specific number of seconds, is that possible?
Here's what I have so far, not sure if it's right. I was hoping for a more straightforward way to do this. is it possible?
function collectProcess {
param (
#processnamn
$process,
#limit for the WS
$limit,
#Amount of seconds the loop will run
$length
)
#Creating a new time span with the amount of seconds given
$timeout = new-timespan -Seconds $length
#starting a timer to compare later with the timeout
$timer = [diagnostics.stopwatch]::StartNew()
while ($timer.elapsed -lt $timeout) {
$tempProcess= Get-Process -Name $process
if ($tempProcess.WS -gt $limit) {
Stop-Process -Name $process -Force
Write-Host ("Process has stopped now"+ " The WS is: "+ $tempProcess.WS + " and our limit is: "+$limit )
break;
}
}
}
Powershell version: 5.1