I'm doing a timer that counts to milliseconds, and when milliseconds reached their end its stops the script. Why I wont the code repeating until its finish its because the Windows performance recorder is working behind the scene and wpr is working with time and we have to stop the wpr manually. The problem is the timer that I have doesn't stop the WPR when its remotely launched. timer working great locally.
Here the code below:
$scriptwpr = {
wpr -start cpu
function Stop-Script
{
Write-Host "Called Stop-Script."
wpr -stop C:\$env:computername-PerformanceAnalyzer.etl
#[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.CloseAsync()
}
$elapsedEventHandler = {
param ([System.Object]$sender, [System.Timers.ElapsedEventArgs]$e)
Write-Host "Event handler invoked."
($sender -as [System.Timers.Timer]).Stop()
Unregister-Event -SourceIdentifier Timer.Elapsed
Stop-Script
}
$timer = New-Object System.Timers.Timer -ArgumentList 120000 # setup the timer to fire the elapsed event after 20 minutes
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $elapsedEventHandler
$timer.Start()
}
Invoke-command -script $scriptwpr -computer computernamehere
Do you have a better way to resolve my problem?