Search code examples
powershelliisapplication-pool

Powershell App Pool set periodicRestart syntax


I'm trying to set the periodicRestart property using a powershell script, but I'm trying to use a slightly different syntax than what I've seen in code samples.

Here's one way to do according to Set the specific times to recycle an application pool with PowerShell:

Clear-ItemProperty $iisAppPoolName -Name Recycling.periodicRestart.schedule
Set-ItemProperty   $iisAppPoolName -Name Recycling.periodicRestart.schedule `
                                   -Value @{value="01:00:00"}

However, I already have a block of code where I'm setting properties on the $appPool itself like this:

$appPool = New-WebAppPool $iisAppPoolName 
$appPool.managedPipelineMode = "Classic"
$appPool.managedRuntimeVersion = "c4.0"
$appPool.recycling.periodicRestart.time = [TimeSpan]"00:00:00"
$appPool | Set-Item

Which works fine, so I'd like to add the following line:

$appPool.recycling.periodicRestart.schedule = @{value="01:00:00"}

But I can't get the syntax for @{value="01:00:00"} to take. The schedule property is expecting a hashtable which is what I'm passing it.

example

Any ideas?


Solution

  • It's interesting that you're seeing it as a [Hashtable]. I see it as a [Microsoft.Iis.Powershell.Framework.ConfigurationElement].

    It has a method called .UpdateCollection() which expects a [PSObject[]], so it's looking for an array of objects.

    The thing is, calling that method, whether on a pool object returned from New-WebAppPool or from Get-Item IIS:\AppPools\ExistingPool, results in an error stating that it's read only.

    I tried replacing the entire .Collection with a new arraylist with timespan objects added to it, and I got no errors, but it didn't set the values.

    I also tried creating a ConfigurationElement object, but it seems to have no constructor, so it's probably a private class somewhere in the code.

    I'm not saying there's definitely no way to do it like you want, but it seems like you'll be best off just using Set-ItemProperty as it seems that some of these attributes were designed to be updated only through the PS Provider.