#From: http://www.herlitz.nu/2017/11/07/enable-IIS-preloadEnabled-and-AlwaysRunning-using-PowerShell/ ### Enable if required #Import-Module ServerManager #Add-WindowsFeature Web-Scripting-Tools Write-Host "*** Start IIS Settings Optimization Script ***" Import-Module WebAdministration #Set the site name $siteName = "Default Web Site" #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 pools $appPools = Get-ChildItem "IIS:\AppPools\" #Fetch the site collection (web apps under the site) $appCollection = Get-ChildItem "IIS:\Sites\$siteName" | Where-Object {$_.NodeType -eq 'application'} #Declare apps to preload $appsToPreload = New-Object System.Collections.ArrayList(,('AuditTrail','FederatedSearch','Forms','Laserfiche','LFCMIS','LDPS','LFDS','LFDSSTS','Mobile','WebLink','Workflow')) $appsToPreloadUndetected = $appsToPreload.Clone() #Useful resource on working with IIS recycle times in PowerShell: #https://www.habaneroconsulting.com/stories/insights/2013/set-the-specific-times-to-recycle-an-application-pool-with-powershell #Set to desired daily recycle time $recycleTimes = @("04:00") Write-Host "***************************************************" Write-Host "* Update all IIS AppPools to: *" Write-Host "* -Disable idle timeouts *" Write-Host "* -Schedule consistent app pool recycling time(s) *" Write-Host "* -Set Start Mode to AlwaysRunning *" Write-Host "***************************************************" Write-Host "`n" foreach ($appPool in $appPools) { if ($appPool.processModel.idletimeout -ne '0') { $appPool | Set-ItemProperty -Name processModel -value @{idletimeout='0'} Write-Host "$($appPool.Name) idle timeout disabled" -ForegroundColor Green }` else { Write-Host "$($appPool.Name) already has idle timeout disabled" -ForegroundColor Yellow } #Clear existing recycle schedule and add specified schedule #Easier than trying to check all the collection values and ensures setting is correct at the end $appPool | Clear-ItemProperty -Name Recycling.periodicRestart.schedule foreach ($recycleTime in $recycleTimes) { $appPool | Set-ItemProperty -Name Recycling.periodicRestart.schedule -Value @{value=$recycleTime} Write-Host "$($appPool.Name) added recycle time at $recycleTime" -ForegroundColor Green } if ($appPool.Recycling.periodicRestart.time -ne '00:00:00') { $appPool | Set-ItemProperty -Name Recycling.periodicRestart -Value @{time='00:00:00'} Write-Host "$($appPool.Name) revolving recycling disabled" -ForegroundColor Green }` else { Write-Host "$($appPool.Name) already has revolving recycling disabled" -ForegroundColor Yellow } if ($appPool.startMode -ne "AlwaysRunning") { Write-Host "$($appPool.Name) startMode is set to $($appPool.startMode), activating AlwaysRunning" $appPool | Set-ItemProperty -Name "startMode" -Value "AlwaysRunning" Write-Host "$($appPool.Name) startMode is now set to $($appPool.startMode)" -ForegroundColor Green } ` else { Write-Host "$($appPool.Name) startMode was already set to $($appPool.startMode) " -ForegroundColor Yellow } Write-Host "`n" } Write-Host "`n" #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 } Write-Host "**************************************************" Write-Host "* Activate preloadEnabled on Laserfiche web apps *" Write-Host "* and the IIS Site instance *" Write-Host "**************************************************" Write-Host "`n" #Enable preloadEnabled on the IIS Site instance if (!(Get-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled).Value) { Write-Host "$siteName preloadEnabled is inactive, activating" Set-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled -Value True Write-Host "$siteName preloadEnabled is now set to $((Get-ItemProperty "IIS:\Sites\$siteName" -Name applicationDefaults.preloadEnabled).Value)" -ForegroundColor Green } ` else { Write-Host "$siteName preloadEnabled already active" -ForegroundColor Yellow } #Enable preloadEnabled on Laserfiche web apps foreach ($app in $appCollection) { $appName = $app.Name if (!($app.preloadEnabled)) { $preloadFlag = $false foreach ($appToPreload in $appsToPreload) { if ($app.Name -eq $appToPreload) { Write-Host "$appName is in preload list and preloadEnabled is inactive, activating" Set-ItemProperty "IIS:\Sites\$siteName\$appName" -Name preloadEnabled -Value $true Write-Host " preloadEnabled is now set to $((Get-ItemProperty "IIS:\Sites\$siteName\$appName" -Name preloadEnabled).Value)" -ForegroundColor Green $appsToPreloadUndetected.Remove($appName) $preloadFlag = $true } } if ($preloadFlag -eq $false) { Write-Host "$appName is not on preload list" } } ` else { $appsToPreloadUndetected.Remove($appName) Write-Host "$appName has preloadEnabled already active" -ForegroundColor Yellow } } if ($appsToPreloadUndetected.count -gt 0) { $appsToPreloadUndetected = $appsToPreloadUndetected -join "`n" Write-Host "The following apps on the preload list were not detected in IIS:`n$appsToPreloadUndetected" } Write-Host "`n" Write-Host "***********************************************" -ForegroundColor Green Write-Host "* IIS Optimization Script Complete *" -ForegroundColor Green Write-Host "* Please restart for settings to take effect! *" -ForegroundColor Green Write-Host "***********************************************" -ForegroundColor Green