SharePoint 预热脚本
作为.net程序员,大家都知道,IIS有默认回收机制;
Sharepoint更是有多个应用池,且每天夜里都会默认回收;这也就造成了第二天为什么第一个打开站点的人,访问速度非常慢的原因了。解决此问题最好的办法就是预热
下面具体介绍通过Windows计划任务程序部署PowerShell脚本,来对SharePoint环境进行预热。
如下为预热代码,保存为.ps1文件
<#
.SYNOPSIS
Warm up SharePoint IIS W3WP memory cache by loading pages from WebRequest
.DESCRIPTION
Loads the full page so resources like CSS, JS, and images are included. Please modify lines 331-345
to suit your portal content design (popular URLs, custom pages, etc.)
Comments and suggestions always welcome! Please, use the issues panel at the project page.
.PARAMETER url
A collection of url that will be added to the list of websites the script will fetch.
.PARAMETER install
Typing "SPBestWarmUp.ps1 -install" will create a local Task Scheduler job under credentials of the
current user. Job runs every 60 minutes on the hour to help automatically populate cache.
Keeps cache full even after IIS daily recycle, WSP deployment, reboot, or other system events.
.PARAMETER installfarm
Typing "SPBestWarmUp.ps1 -farminstall" will create a Task Scheduler job on all machines in the farm.
.PARAMETER uninstall
Typing "SPBestWarmUp.ps1 -uninstall" will remove Task Scheduler job from all machines in the farm.
.PARAMETER skiplog
Typing "SPBestWarmUp.ps1 -skiplog" will avoid writing to the EventLog.
.PARAMETER allsites
Typing "SPBestWarmUp.ps1 -allsites" will load every site and web URL.
.EXAMPLE
.\SPBestWarmUp.ps1 -url "http://domainA.tld","http://domainB.tld"
.EXAMPLE
.\SPBestWarmUp.ps1 -i
.\SPBestWarmUp.ps1 -install
.EXAMPLE
.\SPBestWarmUp.ps1 -f
.\SPBestWarmUp.ps1 -installfarm
.EXAMPLE
.\SPBestWarmUp.ps1 -u
.\SPBestWarmUp.ps1 -uninstall
.NOTES
File Name : SPBestWarmUp.ps1
Author : Jeff Jones - @spjeff
Author : Hagen Deike - @hd_ka
Version : 2.2.4
Modified : 05-13-2016
.LINK
https://github.com/spjeff/spbestwarmup
#>
Function WarmUp() {
NavigateTo "http://atdspsfr03"
}
Function NavigateTo([string] $url) {
if ($url.ToUpper().StartsWith("HTTP")) {
Write-Host " $url" -NoNewLine
# WebRequest command line
try {
$wr = Invoke-WebRequest -Uri $url -UseBasicParsing -UseDefaultCredentials -TimeoutSec 120
FetchResources $url $wr.Images
FetchResources $url $wr.Scripts
Write-Host "."
} catch {
$httpCode = $_.Exception.Response.StatusCode.Value__
if ($httpCode) {
Write-Host " [$httpCode]" -Fore Yellow
} else {
Write-Host " "
}
}
}
}
Function FetchResources($baseUrl, $resources) {
# Download additional HTTP files
[uri]$uri = $baseUrl
$rootUrl = $uri.Scheme + "://" + $uri.Authority
# Loop
$counter = 0
foreach ($res in $resources) {
# Support both abosolute and relative URLs
$resUrl = $res.src
if ($resUrl.ToUpper().Contains("HTTP")) {
$fetchUrl = $res.src
} else {
if (!$resUrl.StartsWith("/")) {
$resUrl = "/" + $resUrl
}
$fetchUrl = $rootUrl + $resUrl
}
# Progress
Write-Progress -Activity "Opening " -Status $fetchUrl -PercentComplete (($counter/$resources.Count)*100)
$counter++
# Execute
$resp = Invoke-WebRequest -UseDefaultCredentials -UseBasicParsing -Uri $fetchUrl -TimeoutSec 120
Write-Host "." -NoNewLine
}
Write-Progress -Activity "Completed" -Completed
}
Function ShowW3WP() {
# Total memory used by IIS worker processes
$mb = [Math]::Round((Get-Process W3WP -ErrorAction SilentlyContinue | Select-Object pm | Measure-Object pm -Sum).Sum/1MB)
WriteLog "Total W3WP = $mb MB" "Green"
}
Function CreateLog() {
# EventLog - create source if missing
if (!(Get-EventLog -LogName Application -Source "SPBestWarmUp" -ErrorAction SilentlyContinue)) {
New-EventLog -LogName Application -Source "SPBestWarmUp" -ErrorAction SilentlyContinue | Out-Null
}
}
Function WriteLog($text, $color) {
$global:msg += "`n$text"
if ($color) {
Write-Host $text -Fore $color
} else {
Write-Output $text
}
}
Function SaveLog($id, $txt, $error) {
# EventLog
if (!$skiplog) {
if (!$error) {
# Success
$global:msg += $txt
Write-EventLog -LogName Application -Source "SPBestWarmUp" -EntryType Information -EventId $id -Message $global:msg
} else {
# Error
$global:msg += $error[0].Exception.ToString() + "`r`n" + $error[0].ErrorDetails.Message
Write-EventLog -LogName Application -Source "SPBestWarmUp" -EntryType Warning -EventId $id -Message $global:msg
}
}
}
# Main
CreateLog
WriteLog "SPBestWarmUp v2.2.4 (last updated 05-13-2016)`n------`n"
# Check Permission Level
if (!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning "You do not have elevated Administrator rights to run this script.`nPlease re-run as Administrator."
break
} else {
try {
# SharePoint cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
# Task Scheduler
$cmdpath = $MyInvocation.MyCommand.Path
$tasks = schtasks /query /fo csv | ConvertFrom-Csv
$spb = $tasks |Where-Object {$_.TaskName -eq "\SPBestWarmUp"}
if (!$spb -and !$install -and !$installfarm) {
Write-Warning "Tip: to install on Task Scheduler run the command ""SPBestWarmUp.ps1 -install"""
}
if ($install -or $installfarm -or $uninstall) {
Installer
SaveLog 2 "Installed to Task Scheduler"
}
if ($uninstall) {
break
}
# Core
ShowW3WP
WarmUp
ShowW3WP
SaveLog 1 "Operation completed successfully"
} catch {
SaveLog 201 "ERROR" $error
}
}
再开始中搜索任务计划程序
新建任务,将执行程序路径标志为刚刚保存的sp1文件,设定每小时跑一次;至此可以看到效果,sharepoint页面基本打开都在5S内