Sunday, January 17, 2016

SharePoint Wakeup script Configuration in Task Scheduler

This script will access your SharePoint site specified in URL (in script) after every few minutes and will keep the application pool warm thus making it quicker for users to access the site.

1) Log on to SharePoint Server

2) Copy Script below in a  file and save that file as spwakeup.ps1

function get-webpage([string]$url,[System.Net.NetworkCredential]$cred=$null)
{
    $error = $false
    $webRequest = [System.Net.HttpWebRequest]::Create($url)
    $webRequest.Timeout = 300000
    if($cred -eq $null)
    {
        $webRequest.Credentials = [system.Net.CredentialCache]::DefaultCredentials
    }
    try {
    $res = $webRequest.getresponse()
    }catch{
    $error = $true
    }
}
   
Function Load-SharePoint-Powershell
{
     If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
     {
              Write-Host -ForegroundColor White " - Loading SharePoint Powershell Snapin"
          Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction Stop
     }
}
Load-SharePoint-Powershell
$webapplications = Get-SPWebApplication [SITEURL]
$AllSites = Get-SPSite -limit all
$Array = @()
$i=0
foreach ($wa in $webapplications)
{
    foreach ($windowsauth in $wa.AlternateUrls)
    {
        $authenticationprovider= Get-SPAuthenticationProvider -webapplication $wa -zone $windowsauth.Zone
        If ($authenticationprovider.UseWindowsIntegratedAuthentication)
        {
            $accessableURL = $windowsauth.IncomingUrl
        }
    }
    if (!$AccessableURL) {$accessableURL = $wa.url -replace ".$"}
    foreach ($site in $AllSites)
    {
        if ($Site.Url -and $Site.Url+"/" -match $wa.url)
        {
            $subsites = Get-SPSite $Site.Url | Get-SPWeb -Limit All
            Foreach ($subsite in $subsites)
            {
                $i++
                Write-Progress -activity "Looking up all sites" -status "Please Wait..." -PercentComplete (($i / 500) * 100)
                $PlainWaUrl = $wa.Url -replace ".$"
                $WakeUpSite = $Subsite.Url.replace($PlainWaUrl, $accessableURL)
                $Array = $Array + $WakeUpSite
                #$html=get-webpage -url "$WakeUpSite" -cred $cred;
                if ($i -eq 500){$i=0}
            }
        }
    }
    Remove-Variable accessableURL
}

$i=0
Foreach ($Website in $Array)
{
    $i++
    Write-Progress -activity "Waking up sites" -status "Waking: $Website" -PercentComplete (($i / $Array.Count) * 100)
    $html=get-webpage -url "$Website" -cred $cred;
}


3) Click Start-> Administrative Tools - > Task Scheduler

2) Expand the left hand tree and right click on Task Schedule Library to create a new Task.


3) Give your task a friend name.

4) In General section come to  Security Options select the following options
a) Run whether user is logged on or not
b) Run with highest privileges
c)  Hidden

5) In Triggers section , add a new trigger and specify the following.

a) Begin the task -> on a schedule
b) Settings:
 One Time
Start date and time should be today's date and time
c) Advanced Settings - > Select Enabled.

6) Select Actions and add a new Action

a) Action -> Start a Program
b) Program/script - > PowerShell.exe
c) add a arguments (optional) -> Execution Policy bypass "Path of script file which you have saved in step 2"


7) Select the Conditions tab and in power section select
a) Start the task only in the computer is on AC Power

8) Select the Settings tab and select

a) Allows task to be run on demand
b) Stop the tasks if it runs longer than 5 days
c) If the running task does not end when requested, force it to stop.
d) Do not start a new instance


9) Click Ok.



No comments: