Thursday, March 19, 2015

Changing Application Pool's CPU settings.


<#
     .SYNOPSIS
        Change Application Pool's CPU settings.

     .DESCRIPTION
        Sets CPU Limit(percent) to 80% and CPU Limit Action to "Throttle Under Load"
        for all application pools on Windows 2012+

     .PARAMETER ApplicationPoolName
        Name of the Application Pool.

     .LINK
        References:
        http://www.iis.net/learn/manage/powershell/powershell-snap-in-making-simple-configuration-changes-to-web-sites-and-application-pools
        http://stackoverflow.com/questions/11187304/how-to-set-iis-applicationpool-private-memory-limit-value-using-powershell
     
     .NOTES
        Author: Antonio Sotelo
        Email: 

     .EXAMPLE
        Set-AppPoolCPUDefaults -Verbose
        Changes default app pool's cpu settings to cpu= 80% and cpu action= throttle under load.

        Set-CPUSettings -ApplicationPoolName DefaultAppPool -Verbose -verbose
        Changes all app pool's cpu settings to cpu= 80% and cpu action= throttle under load.
#>


Function Set-AppPoolCPUDefaults () {
[cmdletbinding()]
    param (
    [Parameter( Mandatory=$False)]
        [int]$Percent = 80000,
    [Parameter( Mandatory=$False)]
        [string]$Action = "ThrottleUnderLoad"   
                
    )

    Set-WebConfiguration -filter '/system.applicationHost/applicationPools/applicationPoolDefaults/cpu/@limit' -PSPath IIS:\ -value $Percent
    Set-WebConfiguration -filter '/system.applicationHost/applicationPools/applicationPoolDefaults/cpu/@action' -PSPath IIS:\ -value $Action

    Write-Verbose "Default CPU Limit = $((Get-WebConfiguration /system.applicationHost/applicationPools/applicationPoolDefaults).cpu.limit)"
    Write-Verbose "Default CPU Action = $((Get-WebConfiguration /system.applicationHost/applicationPools/applicationPoolDefaults).cpu.action)"

}#End of function

Function Set-CPUSettings (){
[cmdletbinding()]
    param (
    [Parameter( Mandatory=$False)]
        [string]$ApplicationPoolName,

    [Parameter( Mandatory=$False)]
        [int]$Percent = 80000,
    [Parameter( Mandatory=$False)]
        [string]$Action = "ThrottleUnderLoad"   
                
    )

    Set-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name cpu -Value @{limit=$Percent;action=$Action}
    $cpuLimit = (Get-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name cpu).limit
    $cpuAction = (Get-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name cpu).action
    Write-Verbose "$ApplicationPoolName CPU settings: CPU limit = $cpuLimit , CPU action = $cpuAction"

}#End of function

Write-Output "Changing Application Pool Default CPU settings."
Set-AppPoolCPUDefaults -Verbose

Write-Output "Changing CPU settings for all Application Pools on $env:ComputerName "

get-item IIS:\AppPools\* | %{

Set-CPUSettings -ApplicationPoolName $_.name -Verbose

}



No comments:

Post a Comment