Sunday, February 28, 2016

Warm up SharePoint - Vickers Style

There are about a million of these scripts on the Internet, but this one is mine.  Put it on a server, run it regularly in Task Scheduler, and forget about it.

The nice thing about this one is you don't have to change anything about it to run it on your farm.  It will query SharePoint to get the relevant web applications, search centers, mysites, etc.  

Add-PSSnapin Microsoft.SharePoint.PowerShell


try
{
    # Set Variables
    $MultiThreadSites = @()
    $SingleThreadSites = @()
    $WebServers = @()
    $SearchServers = @()
    $Servers = Get-SPServer | where {$_.Role -ne "Invalid"}
   

    # Get Web Applications
    $WebApps = Get-SPWebApplication

    # Get Global Search Center Url
    $SearchSvc = Get-SPEnterpriseSearchServiceApplication | Where {$_.Name -eq "Search Service Application"}
    $SrcCenterUrl = $SearchSvc.SearchCenterUrl -Replace "/default.aspx", ""

    # If search center is set
    if($SrcCenterUrl)
    {
        # Set Various Search Urls
        $SrcUrls = @("$($SrcCenterUrl)/default.aspx")
        $SrcUrls += ,@("$($SrcCenterUrl)/results.aspx")
        $SrcUrls += ,@("$($SrcCenterUrl)/peopleresults.aspx")
        $SrcUrls += ,@("$($SrcCenterUrl)/conversationresults.aspx")
        $SrcUrls += ,@("$($SrcCenterUrl)/results.aspx?k=test")
    }

    # Get Central Admin Url
    $CentralAdmin = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local
    $CentralAdminUrl = $CentralAdmin.Url
    $HealthReportUrl = "$($CentralAdminUrl)/Lists/HealthReports"
    $CAServer = $CentralAdmin.Url.split('//')[2].split('`:')[0]

    # Loop through all the servers in farm
    foreach($Server in $Servers)
    {
        # Create a variable of Server Name
        $ServerName = $Server.Name
       
        # Determine the farm Web Servers
        If ($ServerName -notlike "SRPAPPPW003") {
        $WebServiceIns = Get-SPServiceInstance -Server $ServerName | where {$_.TypeName -eq "Microsoft SharePoint Foundation Web Application"}
        if($WebServiceIns.Status -eq "Online")
        {
            $WebServers += ,@($ServerName)
        } }

        # Determine the farm Search Servers
        $SearchServiceIns = Get-SPServiceInstance -Server $ServerName | where {$_.TypeName -eq "SharePoint Server Search"}
        if($SearchServiceIns.Status -eq "Online")
        {
            $SearchServers += ,@($ServerName)
        }
    }

    # Loop through all the Web Applications
    foreach($WebApp in $WebApps)
    {
        # Get the first site collection Url
        $SiteCols = Get-SPSite -WebApplication $WebApp -Limit 5
       
        # If there are Site Collections in this Web Application
        if($SiteCols)
        {
            # Loop through all the Site Collections
            foreach($SiteCol in $SiteCols)
            {
                $MultiThreadSites += $SiteCol.Url | Where {$_ -notlike "*$CAServer*"}
            }
        }
    }

    # Add Central Admin and Helath Check to the Site Array
    #$SingleThreadSites += ,@($CentralAdminUrl)
    #$SingleThreadSites += ,@($HealthReportUrl)

    # Loop through all the Search Urls
    foreach($SrcUrl in $SrcUrls)
    {
        # Add Url to the Site Array
        $SingleThreadSites += ,@($SrcUrl)
    }

    # Loop through all Search Servers
    foreach($SearchServer in $SearchServers)
    {
        # Add Url to the Site Array
        $TopologyUrl = "http://$($SearchServer.ToLower()):32843/Topology/topology.svc"
        $SingleThreadSites += ,@($TopologyUrl)
    }

    # Loop through all urls
    foreach($Site in $SingleThreadSites)
    {
        Write-host "Loading $($Site)..." -NoNewline
        $Url =  "$($Site)"
        $Request = [System.Net.WebRequest]::Create($Url)
        $Request.UseDefaultCredentials = $true
       
        try
        {
            $Response = $Request.GetResponse()
        }
        catch [System.Net.WebException]
        {
            $Response = $_.Exception.Response
        }
       
        $Status = [int]$Response.StatusCode

        if($Status -eq 200)
        {
            $WebClient = New-Object Net.WebClient
            $WebClient.UseDefaultCredentials = $true
            $PageContents = $WebClient.DownloadString($Url)
            $WebClient.Dispose()
            Write-host "200 OK" -ForegroundColor Green
        }
        else
        {
            Write-host "Error: $Status" -ForegroundColor Red
        }
    }

    # Loop through all site collections
    foreach($Site in $MultiThreadSites)
    {
        # Loop through all web servers
        foreach($WebServer in $WebServers)
        {
            Write-host "Loading $($Site) on server $($WebServer)..." -NoNewline
            $Url = $($Site)
            $BypassLocal = $false
            $ProxyUri = "http://" + $WebServer
            $Proxy = New-Object System.Net.WebProxy($ProxyUri, $BypassLocal)
            $Request = [System.Net.WebRequest]::Create($Url)
            $Request.UseDefaultCredentials = $true
            $Request.Proxy = $Proxy
           
            try
            {
                $Response = $Request.GetResponse()
            }
            catch [System.Net.WebException]
            {
                $Response = $_.Exception.Response
            }
           
            $Status = [int]$Response.StatusCode

            if($Status -eq 200)
            {
                $WebClient = New-Object Net.WebClient
                $WebClient.UseDefaultCredentials = $true
                $WebClient.Proxy = $Proxy
                $PageContents = $WebClient.DownloadString($Url)
                $WebClient.Dispose()
                Write-host "200 OK" -ForegroundColor Green
            }
            else
            {
                Write-host "Error: $Status" -ForegroundColor Red
            }
        }
    }

            Write-host "Loading Central Admin on server $($CAServer)..." -NoNewline
            $Url = $($CentralAdminUrl)
            $BypassLocal = $false
            $ProxyUri = $CentralAdminUrl
            $Proxy = New-Object System.Net.WebProxy($ProxyUri, $BypassLocal)
            $Request = [System.Net.WebRequest]::Create($Url)
            $Request.UseDefaultCredentials = $true
            $Request.Proxy = $Proxy

            try
            {
                $Response = $Request.GetResponse()
            }
            catch [System.Net.WebException]
            {
                $Response = $_.Exception.Response
            }
           
            $Status = [int]$Response.StatusCode

            if($Status -eq 200)
            {
                $WebClient = New-Object Net.WebClient
                $WebClient.UseDefaultCredentials = $true
                $WebClient.Proxy = $Proxy
                $PageContents = $WebClient.DownloadString($Url)
                $WebClient.Dispose()
                Write-host "200 OK" -ForegroundColor Green
            }
            else
            {
                Write-host "Error: $Status" -ForegroundColor Red
            }

}
catch
{
    Write-host "Error" -ForegroundColor Red
    Write-host "Error at line $($_.InvocationInfo.ScriptLineNumber): $_" -ForegroundColor DarkGray
}

# Remove created variables
#Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0

1 comment:

  1. Hmm... This Vickers Style script looks awfully familiar.

    ReplyDelete