3 minute read

Normal warm up scripts for SharePoint open a site object or web object and make a request to that specific site. When you use form based authentication and do not allow anonymous access, you would only warm up the login page.

In order to warm up a application form based SharePoint site you would have to apply your credentials. All off this can be done by using PowerShell and specific COM objects. This script can then be scheduled with “Task Scheduler” that is included in windows.

function Warm-FBASite{
    [CmdletBinding()]
    param (
        [string] $RootUrl = $(Read-Host -prompt "Root Url"),
        [string] $LoginUrl = $(Read-Host -prompt "Login Url"),
        [string] $SiteUrl = $(Read-Host -prompt "Site Url"),
        [string] $UserName = $(Read-Host -prompt "Username"),
        [string] $Password = $(Read-Host -prompt "Password"),
        [bool] $loggedIn = $(Read-Host -prompt "Already logged in")
    )
    $ieMain=New-Object -ComObject "InternetExplorer.Application";
    if($loggedIn -ne $true){
        Login-FBA -RootUrl $RootUrl -LoginUrl $LoginUrl -UserName $UserName -Password $Password -Browser $ieMain;
    }
    Navigate-Site -RootUrl $RootUrl -SiteUrl $SiteUrl;
    $ieMain.Quit();

}

The Warm-FBASite function is the main function that calls other functions. This function needs the following parameters:

  • RootUrl: The root URL of the application you would like to warm up.
  • LoginUrl: The relative URL to the login page.
  • SiteUrl: The relative URL to the site you would like to warm up.
  • UserName: The username off the forms user.
  • Password: The password off the forms user.
  • LogginIn: Boolean value whether the user has already been logged in.

In this function we open a Com object of internet explorer and provide it as a parameter to the function “Login-FBA”.

function Login-FBA{
    [CmdletBinding()]
    param (
        [string] $RootUrl = $(Read-Host -prompt "Root Url"),
        [string] $LoginUrl = $(Read-Host -prompt "Login Url"),
        [string] $UserName = $(Read-Host -prompt "Username"),
        [string] $Password = $(Read-Host -prompt "Password"),
        $Browser = $(Read-Host -prompt "Browser")
    )

    $url = $RootUrl + $LoginUrl;

    Write-Host "Trying to login the user:" $UserName " on the Url: " $url -ForegroundColor Green
    $Browser.navigate($url);
    $Browser.visible=$false;

    if ((Wait-Browser $Browser -Url $url ) -eq $false){
        Write-Host "Something went wrong with requesting the page";
        return
    }else{
        $doc=$Browser.Document
    }

    $txtUsername=$doc.getElementByID("ctl00_PlaceHolderMain_signInControl_UserName"); #Replace by your own ID
    $txtPassword=$doc.getElementByID("ctl00_PlaceHolderMain_signInControl_password"); #Replace by your own ID
    $btnSubmit=$doc.getElementByID("ctl00_PlaceHolderMain_signInControl_login"); #Replace by your own ID
    
    $txtUsername.value=$UserName;
    $txtPassword.value=$Password;
    $btnSubmit.click();
    if ((Wait-Browser $Browser -Url $url ) -eq $false){
        return;
    }
}

This function tries to open the login page in the com object you send in the parameters. When this page is loaded it inserts the username and password in the login controls by finding these controls by there id.

In this function you will have to insert your own Id’s of the username textbox, password textbox and submit button. In this function you also see a reference to the “Wait-Browser” function. This function is created to make sure the site you are requesting in the COM object is fully loaded.

function Wait-Browser{
    [CmdletBinding()]
    param (
            $Browser = $(Read-Host -prompt "Browser"),
            $Url = $(Read-Host -prompt "Url")
    )

    $maxRetries=50;
    $retry = 2;
    $retryCount = 0;

    while ($Browser.Busy -eq $true){  
        Write-Host "Waiting for browser: " $URl  -ForeGroundColor Yellow;
        
        if ($retryCount -gt $maxRetries){
            return $false;
        }
        
        $retryCount++;
        start-sleep $retry ;
    } 

    return $true;
}

When the login request succeeds it is time to open the site and let that document load within the COM object. When the site is loaded it will write the page title to the PowerShell window.

function Navigate-Site{
    [CmdletBinding()]
    param (
        [string] $RootUrl = $(Read-Host -prompt "Root Url"),
        [string] $SiteUrl = $(Read-Host -prompt "Site Url")
    )

    $url = $RootUrl + $SiteUrl;

    $iePage=New-Object -ComObject "InternetExplorer.Application";
    $iePage.navigate($url);
    $iePage.visible=$false;
    $retryCounter=0
    
     if ((Wait-Browser $iePage -Url $url ) -eq $false){
        Write-Host "Something went wrong with requesting the page" -ForegroundColor Red;
        return
    }else{