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 }

Wednesday, July 8, 2015

PowerShell script to restart application pools, with remote Powershell

Has a follow up on my blog post about a PowerShell script to restart application pools. This script do it with remote PowerShell

$computername = "myserver"
$script = {   
    Import-Module WebAdministration
    (Get-Item "IIS:\Sites\*"| Select-Object applicationPool).applicationPool | % { Restart-WebAppPool $_ }
}
$cred = Get-Credential
$remotesession = New-PSSession -ComputerName $computername -Credential $cred
Invoke-Command -Session $remotesession -ScriptBlock $script

PowerShell script to restart application pools

Two line PowerShell script to restart all application pools on a server

1 #Import WebAdministration
2 Import-Module WebAdministration
3 #Find, pipe and restart
4 (Get-Item "IIS:\Sites\*" | Select-Object applicationPool).applicationPool | % { Restart-WebAppPool $_ }
5