Welcome to the navigation

Eu in ea commodo est incididunt eiusmod dolor esse ipsum irure consectetur fugiat nulla elit, reprehenderit nisi labore ad amet, adipisicing non do tempor aute. Sit ad ipsum labore velit reprehenderit id in sunt laborum, in magna nisi qui consequat, culpa voluptate est anim commodo et mollit veniam, elit, ut

Yeah, this will be replaced... But please enjoy the search!

Enable IIS preloadEnabled and AlwaysRunning using PowerShell

Since IIS 8 we've been able to use the built-in module "Application Initialization". It is often misunderstood how it works and that it also require an additional setting on the website instance to perform the "warm up" webadmins so often want.

It requires the Application Initialization module to be installed on the server. This can be done using the PowerShell command Install-WindowsFeature

$webAppInit = Get-WindowsFeature -Name "Web-AppInit"
Install-WindowsFeature $webAppInit -ErrorAction Stop

When you set the startMode property of your application pool to AlwaysRunning a worker process is spawned as soon as IIS starts up and does not wait for the first user request.

$site = Get-Website -Name $siteName
$appPool = Get-ChildItem IIS:\AppPools\ | Where-Object { $_.Name -eq $site.applicationPool }
$appPool | Set-ItemProperty -name "startMode" -Value "AlwaysRunning"

But this does not mean the web application is initialized.

The IIS application defaults preloadEnabled metabase setting along with the startMode setting can be used to autostart and thus warm up your web application.

Set-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled -Value True

When you set preloadEnabled to true, IIS will simulate a user request to the default page (can be changed with initializationPage metabase setting) of the website/virdir so that the application initializes. The request is not logged in the IIS logs. (ref. https://tinyurl.com/y7qpun5j)

The entire script

### Enable if required
#Import-Module ServerManager
#Add-WindowsFeature Web-Scripting-Tools
Import-Module WebAdministration


#Set the site name
$siteName = "Default Web Site"

<#
    Enable Application Initialization
#>

#Ensure Application Initialization is available
$webAppInit = Get-WindowsFeature -Name "Web-AppInit"

if(!$webAppInit.Installed) 
{
    Write-Host "$($webAppInit.DisplayName) not present, installing"
    Install-WindowsFeature $webAppInit -ErrorAction Stop
    Write-Host "`nInstalled $($webAppInit.DisplayName)`n" -ForegroundColor Green
}
else 
{
    Write-Host "$($webAppInit.DisplayName) was already installed" -ForegroundColor Yellow
}

#Fetch the site
$site = Get-Website -Name $siteName

if(!$site)
{
    Write-Host "Site $siteName could not be found, exiting!" -ForegroundColor Yellow
    Break
}


#Fetch the application pool
$appPool = Get-ChildItem IIS:\AppPools\ | Where-Object { $_.Name -eq $site.applicationPool }


#Set up AlwaysRunning
if($appPool.startMode -ne "AlwaysRunning")
{
    Write-Host "startMode is set to $($appPool.startMode ), activating AlwaysRunning"
    
    $appPool | Set-ItemProperty -name "startMode" -Value "AlwaysRunning"
    $appPool = Get-ChildItem IIS:\AppPools\ | Where-Object { $_.Name -eq $site.applicationPool }

    Write-Host "startMode is now set to $($appPool.startMode)`n" -ForegroundColor Green
} 
else 
{
    Write-Host "startMode was already set to $($appPool.startMode) for the application pool $($site.applicationPool)" -ForegroundColor Yellow
}


<#
    Enable preloadEnabled on the IIS Site instance
#>


if(!(Get-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled).Value) 
{
    Write-Host "preloadEnabled is inactive, activating"
    
    Set-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled -Value True
    
    Write-Host "preloadEnabled is now set to $((Get-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled).Value)" -ForegroundColor Green
} 
else
{
    Write-Host "preloadEnabled already active" -ForegroundColor Yellow
}

 

The process should look something like this

Or if everything is set already