none
how to recyle a particular application pool in iis using powershell script. All i have is a site name RRS feed

  • Question

  • how to recycle a particular application pool in iis using powershell script. All i have is a site name. Do i need to retrieve the application pool name of the site and then try to recycle it? 


    Ramakrishnan

    Monday, July 8, 2013 9:13 AM

Answers

  • Try this:

    # Load IIS module:
    Import-Module WebAdministration
    # Set a name of the site we want to recycle the pool for:
    $site = "Default Web Site"
    # Get pool name by the site name:
    $pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool
    # Recycle the application pool:
    Restart-WebAppPool $pool



    Monday, July 8, 2013 9:59 AM

All replies

  • Try this:

    # Load IIS module:
    Import-Module WebAdministration
    # Set a name of the site we want to recycle the pool for:
    $site = "Default Web Site"
    # Get pool name by the site name:
    $pool = (Get-Item "IIS:\Sites\$site"| Select-Object applicationPool).applicationPool
    # Recycle the application pool:
    Restart-WebAppPool $pool



    Monday, July 8, 2013 9:59 AM
  • Thanks a lot


    Ramakrishnan

    Monday, July 8, 2013 11:20 AM
  • how would the command have to be changed if I want to run it on my machine to talk to the server where the app pool resides?

    also... i know the app pool name... so i don't need to have it figure it out

    Wednesday, December 7, 2016 3:46 PM
  • You would have to use Invoke-COmmand cmlet, but that will only work if you have PSRemoting enabled on the remote machines.

    Another way is to use WMI

    # Stops and restarts the specified application pool on the specified machine Function RecycleAppPool { param ( [string]$serverName, [string]$applicationPool ) # Return an app pool object $aPool = Get-WmiObject -Authentication PacketPrivacy -Impersonation Impersonate -ComputerName $serverName ` -Namespace "root\WebAdministration" -Class "ApplicationPool" | Where-Object { $_.Name -eq "$applicationPool" } # Check to make sure we actually have an object If ($aPool -ne $null) { If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Started") { Write-Host -ForegroundColor Green "Attempting to Stop Application Pool: $applicationPool on Server: $server" $aPool.Stop() $stopper = 0 While (((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopping")) { Write-Host -ForegroundColor Yellow "Stopping...." $stopper++ Start-Sleep 5 If ($stopper -eq 10) { Write-Host -ForegroundColor Red "There was an issue with stopping the Application Pool $applicationPool." exit } } If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopped") { Write-Host -ForegroundColor Green "Application Pool: $applicationPool on Server: $server has been stopped" } } If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Stopped") { Write-Host -ForegroundColor Green "Attempting to Start Application Pool: $applicationPool on Server: $server" $aPool.Start() If ((ConvertAppPoolState ($aPool.GetState() | Select-Object -ExpandProperty ReturnValue)) -eq "Started") { Write-Host -ForegroundColor Green "Application Pool: $applicationPool on Server: $server has been started" } } } Else { Write-Host -ForegroundColor Red "There was an error trying to recycle app pool: $applicationPool on server: $serverName, " ` + "please make sure you typed everything correctly" } }

    Function

    ConvertAppPoolState


    {

    param


    (

    [

    int]$value


    )

    switch($value)

    {

    0 {

    "Starting"; break}

    1 {

    "Started"; break}

    2 {

    "Stopping"; break}

    3 {

    "Stopped"; break}

    default{ "Unknown"; break}

    }

    }



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, December 7, 2016 4:25 PM add another function
    Wednesday, December 7, 2016 4:23 PM
  • why can't use iisreset ?

    or psexec servername iisreset? 


    Chris

    Thursday, December 8, 2016 7:53 PM
  • IISRESET restarts IIS ultimately affecting every application running on it. Restarting app pool only affects the apps running under the app pool...HUGE difference.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, December 8, 2016 8:28 PM
  • pretty noobish here.. .when saving this thing as a script, how do I fire it off?  it doesn't run via either ps window or cmd window.

    Thursday, December 8, 2016 8:42 PM
  • Which script are you referring to? Two different ones have been posted, the one I posted are two functions, so you have to actually call the first one, as it calls  the second one. So after the two functions just call it

    RecycleAppPool -serverName MyServer -applicationPool MyPool


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, December 8, 2016 11:37 PM
  • Yours was the one i was referring to.  I believe I understand.  I'll try it again tomorrow.
    Friday, December 9, 2016 12:57 AM
  • Much simpler solution:

    Import-Module WebAdministration
    Restart-WebAppPool "MyPoolName"


    • Edited by Alexus_1024 Tuesday, March 7, 2017 7:40 AM
    • Proposed as answer by OngZ Monday, October 15, 2018 5:50 AM
    • Unproposed as answer by OngZ Monday, October 15, 2018 5:50 AM
    • Proposed as answer by OngZ Monday, October 15, 2018 5:51 AM
    Tuesday, March 7, 2017 7:39 AM