[ACCEPTED]-How do I recycle an IIS AppPool with Powershell?-iis-6
Where-Object is a filter that expects something as in 7 input. There seems to be a missing pipe, before 6 the where filter.
Try:
$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool" | Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()
Edit: I noticed that the WMI class 5 was IISApplicationPools, which as you saw, did not show us 4 the Recycle method when piped to Get-Member. This 3 needs to be changed to IISApplicationPool (non-plural). With 2 that change, you are able to use the Recycle 1 method. The code above has been updated.
Using the data from this question I was 2 able to create 2 very useful functions.
- Get-IisAppPools
- Recycle-IisAppPool
The 1 code:
function Get-IisAppPools {
Get-WmiObject -Namespace "root\MicrosoftIISv2" -Class "IIsApplicationPool" -Filter 'name like "W3SVC/APPPOOLS/%"'
| ForEach-Object { $_.Name.ToString().SubString(15) }
}
function Recycle-IisAppPool([string]$appPoolName) {
Invoke-WmiMethod -Name Recycle -Namespace "root\MicrosoftIISv2" -Path "IIsApplicationPool.Name='W3SVC/APPPOOLS/$appPoolName'"
}
You can use these functions like this
Recycle-IisAppPool DefaultAppPool
Get-IisAppPools | ? { $_ -match "v4.0$" } | % { Recycle-IisAppPool $_ }
When using get-WMIObject you should probably 7 use -filter instead of piping to Where-Object. the 6 filter parameter uses WQL syntax language 5 instead of PowerShell's, so don't let that 4 trip you up.
$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" -filter 'name="W3SVC/APPPOOLS/$appPoolName"'
Having said that putting the 3 pipe there should work, and certainly makes 2 it easier to work with unless you already 1 know WQL.
This isn't a Powershell-specific answer, but 3 iisapp.vbs will list the running application 2 pools, and there is a /r flag to recycle 1 a specific app pool.
You can also use a WQL query to get just 4 the AppPool you want; this has the advantage 3 of filtering the results on the WMI side, which 2 is especially handy when getting objects 1 from a remote machine.
(Get-WmiObject -Query "SELECT * FROM IIsApplicationPool WHERE Name = 'W3SVC/AppPools/$appPoolName'" -Namespace 'root\MicrosoftIISv2').Recycle()
In Powershell:
$pool = Get-IISAppPool -Name <name>
$pool.recycle()
0
With IIS 8.0 I've found I had to use -namespace root\webadministration -class ApplicationPool
For 5 example, to recycle an Application Pool 4 in IIS 8 remotely using PowerShell:
As always, please 3 test this first by listing the application 2 pools. Just remove the | where
and the first (
from 1 the command:
gwmi -comp WebServer01 -namespace root\webadministration -class ApplicationPool
#Recycle app pool by name.
(gwmi -comp WebServer01 -namespace root\webadministration -class ApplicationPool | `
where {$_.Name -eq 'YourAppPool'}).recycle()
And on one line:
(gwmi -comp WebSserver01 -namespace root\webadministration -class ApplicationPool | where {$_.Name -eq 'YourAppPool'}).recycle()
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.