Wednesday, December 31, 2014

Powershell script to wakeup all sites in the SharePoint farm

$ErrorActionPreference = "Stop"

function Wakeup-Site{
     param(
          [Parameter(Mandatory = $true)]
          [String] $Url
     )
     $request = [System.Net.HttpWebRequest] [System.Net.WebRequest]::Create($Url)
     $request.UseDefaultCredentials = $true
     $request.Method = "GET"
     $request.Accept = "text/html"
     $request.Timeout = 120000 #2 minutes

     Write-Host "Waking up site at $Url"
     try
     {
          # Get the response of $request
          $response = [System.Net.HttpWebResponse] $request.GetResponse()
     }
     catch [Net.WebException]
     {       
    Write-Warning $_.Exception.Message
     }
     finally
     {
          if ($response)
          {
              $response.Close()
              Remove-Variable ResponseObject
          }
     }
}

if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq$null){
     Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Get-SPWebApplication |%{`
     $_.Sites |% { Wakeup-Site $_.Url}
}


No comments: