Answered by:
how to recyle a particular application pool in iis using powershell script. All i have is a site name

Question
-
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
- Edited by Tomas Dabasinskas Monday, July 8, 2013 10:32 AM Mistype
- Marked as answer by Ramakrishnan Raman Monday, July 8, 2013 11:19 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
- Edited by Tomas Dabasinskas Monday, July 8, 2013 10:32 AM Mistype
- Marked as answer by Ramakrishnan Raman Monday, July 8, 2013 11:19 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
- Edited by CarphuntinGod Wednesday, December 7, 2016 3:50 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
-
-
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''
-
-
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''
-
-
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