views:

2560

answers:

4

I haven't really done any Windows scripting at all, so I am at a loss on how to pull this one off. Anyway, basically what we want to do is have a script that will take an argument on which IIS AppPool to recycle. I have done some research on Google and haven't had much success on getting things to work.

Here is what I am trying now:

$appPoolName = $args[0]
$appPool = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPools" Where-Object {$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"}
$appPool.Recycle()

and the error I get:

Get-WmiObject : A parameter cannot be found that matches parameter name '$_.Name -eq "W3SVC/APPPOOLS/$appPoolName"'.

Anyway, it would be nice if I also knew how to debug things like this. I already fixed one bug with the original script by doing gwmi -namespace "root\MicrosoftIISv2" -list. Any other tips like that one would be great.

Thanks!

Update: Here is some more info

$appPool = gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPools" | Get-Member

.   TypeName: System.Management.ManagementObject#root\MicrosoftIISv2\IIsApplicationPools

Name                MemberType   Definition
----                ----------   ----------
Caption             Property     System.String Caption {get;set;}
Description         Property     System.String Description {get;set;}
InstallDate         Property     System.String InstallDate {get;set;}
Name                Property     System.String Name {get;set;}
Status              Property     System.String Status {get;set;}
__CLASS             Property     System.String __CLASS {get;set;}
__DERIVATION        Property     System.String[] __DERIVATION {get;set;}
__DYNASTY           Property     System.String __DYNASTY {get;set;}
__GENUS             Property     System.Int32 __GENUS {get;set;}
__NAMESPACE         Property     System.String __NAMESPACE {get;set;}
__PATH              Property     System.String __PATH {get;set;}
__PROPERTY_COUNT    Property     System.Int32 __PROPERTY_COUNT {get;set;}
__RELPATH           Property     System.String __RELPATH {get;set;}
__SERVER            Property     System.String __SERVER {get;set;}
__SUPERCLASS        Property     System.String __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime   ScriptMethod System.Object ConvertToDateTime();
Delete              ScriptMethod System.Object Delete();
GetType             ScriptMethod System.Object GetType();
Put                 ScriptMethod System.Object Put();


gwmi -namespace "root\MicrosoftIISv2" -class "IISApplicationPools"


__GENUS          : 2
__CLASS          : IIsApplicationPools
__SUPERCLASS     : CIM_LogicalElement
__DYNASTY        : CIM_ManagedSystemElement
__RELPATH        : IIsApplicationPools.Name="W3SVC/AppPools"
__PROPERTY_COUNT : 5
__DERIVATION     : {CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER         : IRON
__NAMESPACE      : root\MicrosoftIISv2
__PATH           : \\IRON\root\MicrosoftIISv2:IIsApplicationPools.Name="W3SVC/A
                   ppPools"
Caption          :
Description      :
InstallDate      :
Name             : W3SVC/AppPools
Status           :
A: 

If you can get the process ID for the app pool you can always use

Process.Kill()

Its essentially the same thing as issuing recycle.

You can also try using some of the code from this example...

http://www.codeproject.com/KB/aspnet/AppPoolRecycle.aspx

@karmaCreeps... last I checked, powershell is a .NET integration... http://en.wikipedia.org/wiki/Windows_PowerShell, which means that you can use the the FCL to do most of these tasks. And there is nothing wrong with killing the process, thats exactly what Recycling the app pool is, and ASP.NET will do it automatically if the site is idle for a time.

StingyJack
There is a big difference between recycling the pool and killing the process. Recycling starts up a new process while keeping the old process alive to it serve existing requests. The old process is only killed when the requests are done or the shutdown timeout is hit.
hwiechers
Then I ask, Why would you recycle the pool? Because the app is not behaving properly? Because its using too much memory?
StingyJack
@StingyJack - there are many reasons to recycle an app pool. We use it to indicate the application should refetch cached configuration values.
Dan Esparza
+9  A: 

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.

Scott Saad
It's telling me I have a null-values expression. Any idea how I can find out the properties of the object?
Frew
Yes. Try and pipe the results of your Get-WmiObject to Get-Member. This will show you the properties/methods on the incoming object.
Scott Saad
Also, try taking out the Where-Object part to see the full list of results returned.
Scott Saad
Nice answer Scott.
Steven Murawski
+3  A: 

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.

EdgeVB
A: 

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.

jwmiller5