Wednesday, July 22, 2015

Wait for timerjob to finish

I had an old PowerShell latying around. The PowerShell script wait for a given timerjob to be finished.
1 $sleeptime = 5
2 $timerjobname = "MetadataHubTimerJob"
3
4 $timerjob = Get-SPTimerJob | Where {$_.Name -eq $timerjobname}
5 if($timerjob)
6 {
7 $countBefore = $timerjob.HistoryEntries | select "EndTime"
8
9 $timerjob.RunNow()
10 "Waiting for timerjob: $($timerjob.Name) to finish"
11 while($true)
12 {
13 Start-Sleep -Seconds $sleeptime
14 $countAfter = $timerjob.HistoryEntries | select "EndTime"
15
16 if($countAfter.Count -gt $countBefore.Count)
17 {
18 "The timejob $($timerjob.Name) is done"
19 break
20 }
21 else
22 {
23 "Still waiting on timerjob: $($timerjob.Name) to finish..."
24 }
25 }
26 }
27 else
28 {
29 "The timerjob: $($timerjob.Name) was not found."
30 }

No comments:

Post a Comment