From 5dcc9965d393934a9932925e4576bc67b92e6328 Mon Sep 17 00:00:00 2001 From: Chris Long Date: Mon, 23 Mar 2020 17:27:57 -0700 Subject: [PATCH] Add a wait for autoruns scheduled task --- Vagrant/scripts/install-autorunstowineventlog.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Vagrant/scripts/install-autorunstowineventlog.ps1 b/Vagrant/scripts/install-autorunstowineventlog.ps1 index 693331c..fd36469 100644 --- a/Vagrant/scripts/install-autorunstowineventlog.ps1 +++ b/Vagrant/scripts/install-autorunstowineventlog.ps1 @@ -6,6 +6,15 @@ If ((Get-ScheduledTask -TaskName "AutorunsToWinEventLog" -ea silent) -eq $null) . c:\Users\vagrant\AppData\Local\Temp\windows-event-forwarding-master\AutorunsToWinEventLog\Install.ps1 Write-Host "AutorunsToWinEventLog installed. Starting the scheduled task. Future runs will begin at 11am" Start-ScheduledTask -TaskName "AutorunsToWinEventLog" + # https://mcpmag.com/articles/2018/03/16/wait-action-function-powershell.aspx + # Wait 30 seconds for the scheduled task to enter the "Running" state + $Timeout = 30 + $timer = [Diagnostics.Stopwatch]::StartNew() + while (($timer.Elapsed.TotalSeconds -lt $Timeout) -and ((Get-ScheduledTask -TaskName "AutorunsToWinEventLog").State -ne "Running")) { + Start-Sleep -Seconds 3 + Write-Host "Still waiting for scheduled task to start after "$timer.Elapsed.Seconds" seconds..." + } + $timer.Stop() $Tsk = Get-ScheduledTask -TaskName "AutorunsToWinEventLog" if ($Tsk.State -ne "Running") {