This project has moved. For the latest updates, please go here.

Adding Central Administration

Aug 13, 2014 at 1:52 PM
Great script - for myself, I added a loop through the central administration pages as well. I added a command file that invokes it which enables the proper account (farm) to be used to start the command through the windows task scheduler.

Create the script/command file and put into a folder c:\SPWakeUp - use the Task Scheduler to invoke the script on a schedule and on system startup up (with a five minute delay) - be sure to set the tasks so that they will run when user is NOT logged in.

Script (SPWakeUp.ps1):
#
# Load the SharePoint PowerShell:
#
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
     }
}
#
# Wake up CA:
#
function SP-WakeUpCA()
{   
    # Get the CA Application:
    Write-Host -ForegroundColor White "Getting the CA Application URL..."
    $adminWebApp = Get-spwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
        $url = $adminWebApp.url
    Write-Host -ForegroundColor White "CA Application URL is " $url
    # Set up the URLs to hit:
    Write-Host -ForegroundColor White "Setup URLs"
    $urlHome = $url + "default.aspx" 
    $urlApps = $url + "applications.aspx"
    $urlSvcApps = $url + "_admin/ServiceApplications.aspx"
    $urlSvcsOnSvr = $url + "_admin/Server.aspx"
    $urlMonitor = $url + "monitoring.aspx"
    $urlHealth = $url + "Lists/HealthReports/AllItems.aspx"
    #
    # Hit them all:
    #
        Write-Host -ForegroundColor White "Setup Web Requests"
    $webRequestHome = [System.Net.HttpWebRequest]::Create($urlHome) 
    $webRequestHome.Timeout = 300000
    $webRequestHome.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    $webRequestApps = [System.Net.HttpWebRequest]::Create($urlApps) 
    $webRequestApps.Timeout = 300000
    $webRequestApps.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    $webRequestSvcApps = [System.Net.HttpWebRequest]::Create($urlSvcApps) 
    $webRequestSvcApps.Timeout = 300000
    $webRequestSvcApps.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    $webRequestSvcsOnSvr = [System.Net.HttpWebRequest]::Create($urlSvcsOnSvr) 
    $webRequestSvcsOnSvr.Timeout = 300000
    $webRequestSvcsOnSvr.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    $webRequestMonitor = [System.Net.HttpWebRequest]::Create($urlMonitor) 
    $webRequestMonitor.Timeout = 300000
    $webRequestMonitor.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    $webRequestHealth = [System.Net.HttpWebRequest]::Create($urlHealth) 
    $webRequestHealth.Timeout = 300000
    $webRequestHealth.Credentials = [system.Net.CredentialCache]::DefaultCredentials 
    #
    # Hit them all:
    #
    try {
            Write-Host -ForegroundColor Green "Hitting Home Page - " $urlHome
        $resHome = $webRequestHome.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Home Page"
        $error = $true
    }
    try {
            Write-Host -ForegroundColor Green "Hitting Applications Page - " $urlApps
        $resApps = $webRequestApps.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Applications Page"
        $error = $true
    }
    try {
            Write-Host -ForegroundColor Green "Hitting Service Applications Page - " $urlSvcApps
        $resSvcApps = $webRequestSvcApps.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Service Applications Page"
        $error = $true
    }
    try {
            Write-Host -ForegroundColor Green "Hitting Services On Server Page - " $urlSvcsOnSvr
        $resSvcsOnSvr = $webRequestSvcsOnSvr.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Services on Server Page"
        $error = $true
    }
    try {
            Write-Host -ForegroundColor Green "Hitting Monitoring Page - " $urlMonitor
        $resMonitor = $webRequestMonitor.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Monitoring Page"
        $error = $true
    }
    try {
            Write-Host -ForegroundColor Green "Hitting Health Analyzer List Page - " $urlHealth
        $resHealth = $webRequestHealth.GetResponse()
    }catch{
            Write-Host -ForegroundColor Red "ERROR Hitting Health Analyzer Page"
        $error = $true
    }
        Write-Host -ForegroundColor White "CA Wakeup process completed...."
}
#
# Get the web page passed:
#
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
    }
}
#
# Start the process:
#
Load-SharePoint-Powershell
#
# Wake up Central Administration:
#
$caURL = Get-SPWebApplication -includecentraladministration | where {$_.DisplayName -eq "SharePoint Central Administration v4"} | select Url
SP-WakeUpCA -url $caURL
#
# Now wake up any applications:
#
$webapplications = Get-SPWebApplication
$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;
}
Command file (SPWakeUp.cmd):

c:\Windows\System32\WindowsPowershell\v1.0\Powershell.exe -ExecutionPolicy Bypass -File c:\SPWakeup\SPWakeup.ps1

Have fun!

David M. Sterling
Sterling International Consulting Group