Jaa


IIS7 WMI Acess through Powershell to recycle Apppools

Recently while performing application installs, I had to recycle apppools on around 100 servers I was thinking this should be easy, it should be just a one liner

Invoke-command -computername (get-content \\utilityserver\serverlist\servers.txt) -scriptblock {import-module webadministration;Restart-WebAppPool -name myapppool}

Or for all the apppools

Invoke-command -computername (get-content \\utilityserver\serverlist\servers.txt) -scriptblock {import-module webadministration;gci 'IIS:\AppPools' -verbose | Restart-WebAppPool}

But suddenly I found out that it was not as easy as I thought as PowerShell remoting was not set up , I had to look at other alternatives like a for loop in conjunction with psexec and appcmd to get the job done. This greatly bugged me though, I remembered that IIS had changed the WMI namespace to root\webadministration from root\MicrosoftIISv2, and should be able to use PowerShell to remote in through WMI and recycle the apppools without PowerShell remoting. Researching IIS WMI and powershell I found that there was not much documentation on it that can be really helpful. In the process of writing this it turned into a PowerShell advanced function that will

  • Recycle all apppools on remote server
  • Recycle specified apppool on remote server
  • Take pipeline inputs of server names
  • Check for application pool existence before executing the recycle
  • Check if server is online before executing the Script.

Here is the advanced function, load this into your profile and use it like a cmdlet

PS C:\Windows\system32> get-help Operationsguy-Recycleapppool

Operationsguy-Recycleapppool [-Server] <String> [-apppool] <String> [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningAction <ActionPreference>] [-ErrorVariable <String>] [-WarningVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]

Function Operationsguy-Recycleapppool{
[CmdletBinding()]
param(
[parameter(Mandatory=$true, Position=0,ValueFromPipeLine= $true)]
[string]$Server,
[parameter(Mandatory=$true,Position=1)]
[string]$apppool
)
process{
if ((Test-Connection $Server -count 1 -ErrorAction SilentlyContinue) -and ($apppoolarray=gwmi -namespace "root\webadministration" -Class applicationpool -ComputerName $Server -Authentication 6 -Property name -ea 'SilentlyContinue' | select -ExpandProperty name))
{
if ($apppool -eq "ALL")
{gwmi -namespace "root\webadministration" -Class applicationpool -ComputerName $Server -Authentication 6 | Invoke-WmiMethod -Name recycle -ErrorAction SilentlyContinue
if ($?)
{Write-Host "All apppools recycled on $server"}
else {Write-Host -BackgroundColor Red "One apppool is either stopped or did not start backup"}
}
elseif ($apppool)
{if ($apppoolarray -contains $apppool)
{
gwmi -namespace "root\webadministration" -ComputerName $server -Authentication 6 -Query "select * from applicationpool where name='$apppool'" | Invoke-WmiMethod -Name recycle -ErrorAction SilentlyContinue
if ($?)
{Write-Host "$apppool recycled on $server"}
else
{Write-Host "$apppool Apppool state Error"}
}
else
{Write-Host -BackgroundColor Red "$apppool does not exist on $server"}}
}
else
{Write-Host -BackgroundColor Red "$server not reachable or WMI Windows feature for IIS not installed"}
}
}

e.g: "localhost","mypc" | Operationsguy-Recycleapppool -apppool all

get-content d:\servers\server.txt | Operationsguy-Recycleapppool -apppool myapppool

For this to work WMI connectivity between management server and remote hosts is needed, also IIS Management Scripts and Tools component under Management Tools needs to be installed on the remote IIS 7 or 7.5 machines. This will not work for IIS 6.0 servers (try replacing the namespace with root\MicrosoftIISv2 in the function, I have not tried it it may work).

See you guys soon with another post on AD PowerShell cmdlets and how it can be used to audit your groups and get some good information about your servers.