powershelliis

Powershell script to get IIS app pool recycling time from servers


I got the cmd below from the following post Get Specific Recycling Time For IIS With PowerShell

Get-ItemProperty -Path IIS:\AppPools\DefaultAppPool -Name recycling.periodicRestart.schedule.collection

Which outputs

value : 04:14:00

Attributes : {value}

ChildElements : {}

ElementTagName : add

Methods :

Schema : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

However, it gives me the entire array when all i want is the 04:14:00 I've tried piping that to Select-Object -expandproperty with no luck

I've tried using Get-IISAppPool also which would be great for multiple servers, but again, I can't figure out how to get simply the recycle time.

Ultimately I need to get the recycle time of about 10 app pools on hundreds of servers. The only real output I want is $servername, $apppoolname & $recycletime

Can anyone point me to how to get simply the recycle time?

I'll probably do something like

$recycletime= Get-ItemProperty -Path IIS:\AppPools\$apppool -Name recycling.periodicRestart.schedule.collection    <=== don't know how to solve for the time only

I can wrap it all in an invoke-command because I think I'll need to import Webadministration tools on each node, but again, it's getting only the Value that I can't solve.

Any help appreciated.


Solution

  • Which version of IIS / Windows are you running? Strangely running your first query on IIS 10, Server 2022 doesn't give me any output. However, tweaking the query slight

    Import-Module WebAdministration
    (Get-ItemProperty -Path IIS:\\AppPools\DefaultAppPool -Name recycling.periodicRestart | Select-Object -ExpandProperty time).toString()
    

    Gives me that value in a usable way.

    Note, it also works if you run :

    Get-ItemProperty -Path IIS:\\AppPools\DefaultAppPool -Name recycling.periodicRestart | Select-Object time
    

    where it outputs the heading, but if you add -ExpandProperty then you need to .toString() the entire thing, otherwise it outputs all the individual timespan elements, eg Days, Hours, Minutes etc, TotalDays, TotalHours etc.

    -- EDIT --

    OK, so the above code retrieves the "Regular Time intervals in Minutes" value, but to retrieve the "Specific Times" value you need.

    (Get-ItemProperty -Path IIS:\AppPools\DefaultAppPool -Name recycling.periodicRestart.schedule.collection).Value.ToString()
    

    as with the above code you need to include .ToString() to just retrieve the time value not the individual time elements.