Recycling an IIS AppPool in powershell...

rated by 0 users
This post has 6 Replies | 2 Followers

Top 50 Contributor
Posts 69
jwhite Posted: 02-13-2012 9:50 PM

I have put together a script that pulls a list of servers in the array and then tests outlook and owa connectivity to each.  I also added a menu that allows a restart of selected services on all servers that show an error in connectivity. 

What im wanting to do is be able to recycle or restart an IIS AppPool within the powershell session.  So everytime I run get-wmiobject -class IISApplicationPool -namespace root\MicrosoftIISv2 I get the error:

"Invalid Namespace".  Anyone know how I can 1. get a list of all the namespaces, and 2. recycle an iisapppool using get-wmiobject in powershell?  Thanks..

Top 25 Contributor
Posts 341
Top Contributor

To get a list of namespaces, do this:

gwmi  -namespace root -class __namespace | ft name

I do not have the relevant namespace on this machine so I can't help with the second question.

 

 

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

To get a full list of WMI namespaces you need a little bit of recursion

function get-namespace {
param ([string]$name)
     Get-WmiObject -Namespace $name -Class "__NAMESPACE" | foreach {
        "$name\" + $_.Name
        get-namespace $("$name\" + $_.Name)
     }
}
"root"
get-namespace "root"

This starts at the root namespace and for each namespace in root chaecks for any namespaces and so on

You need to run it with elevated privileges as the security namespaces will throw an error

 

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

To recycle an application pool on IIS 7 - Windows 2008/2008 R2

Get-WmiObject -Namespace 'root\webadministration' -Class ApplicationPool -ComputerName <computer name> `
-Authentication 6 -Filter "Name='<application pool name>'" |
Invoke-WmiMethod -Name Recycle

Run this with elevated privileges

You only need -Authentication 6  if you are accessing IIS remotely

More detail on this and investigating the structure of WMI in PowerShell and WMI - www.manning.com/siddaway2

 

Top 50 Contributor
Posts 69

Wonderful!  I'm gonna try that.  Cheers.

Top 50 Contributor
Posts 69

This is what I'm looking for.  Ill post again if i have issues.  Cheers.

Top 25 Contributor
Posts 296
Microsoft MVP
Top Contributor

You are very welcome

Page 1 of 1 (7 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.