How do I recycle an IIS AppPool with Powershell?

你离开我真会死。 提交于 2019-11-30 06:06:18

Where-Object is a filter that expects something as in input. There seems to be a missing pipe, before 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 was IISApplicationPools, which as you saw, did not show us the Recycle method when piped to Get-Member. This needs to be changed to IISApplicationPool (non-plural). With that change, you are able to use the Recycle method. The code above has been updated.

Jason

Using the data from this question I was able to create 2 very useful functions.

  • Get-IisAppPools
  • Recycle-IisAppPool

The 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 use -filter instead of piping to Where-Object. the filter parameter uses WQL syntax language instead of PowerShell's, so don't let that trip you up.

$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" -filter 'name="W3SVC/APPPOOLS/$appPoolName"'

Having said that putting the pipe there should work, and certainly makes it easier to work with unless you already know WQL.

This isn't a Powershell-specific answer, but iisapp.vbs will list the running application pools, and there is a /r flag to recycle a specific app pool.

You can also use a WQL query to get just the AppPool you want; this has the advantage of filtering the results on the WMI side, which is especially handy when getting objects from a remote machine.

(Get-WmiObject -Query "SELECT * FROM IIsApplicationPool WHERE Name = 'W3SVC/AppPools/$appPoolName'" -Namespace 'root\MicrosoftIISv2').Recycle()

With IIS 8.0 I've found I had to use -namespace root\webadministration -class ApplicationPool

For example, to recycle an Application Pool in IIS 8 remotely using PowerShell:

As always, please test this first by listing the application pools. Just remove the | where and the first ( from 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()

In Powershell:

$pool = Get-IISAppPool -Name <name>

$pool.recycle()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!