10

I have been scripting the configuration of our IIS 7.5 instance and through bits and pieces of other peoples scripts I have come up with a syntax that I like:

$WebAppPoolUserName = "domain\user"
$WebAppPoolPassword = "password"

$WebAppPoolNames = @("Test","Test2")

ForEach ($WebAppPoolName in $WebAppPoolNames ) {
    $WebAppPool = New-WebAppPool -Name $WebAppPoolName    
    $WebAppPool.processModel.identityType = "SpecificUser"
    $WebAppPool.processModel.username = $WebAppPoolUserName
    $WebAppPool.processModel.password = $WebAppPoolPassword
    $WebAppPool.managedPipelineMode = "Classic"
    $WebAppPool.managedRuntimeVersion = "v4.0"
    $WebAppPool | set-item
}

I have seen this done a number of different ways that are less terse and I like the way this syntax of setting object properties looks compared to something like what I see on TechNet:

Set-ItemProperty 'IIS:\AppPools\DemoPool' -Name recycling.periodicRestart.requests -Value 100000

One thing I haven't been able to figure out though is how to setup recycle schedules using this syntax.

This command sets ApplicationPoolDefaults but is ugly:

add-webconfiguration  system.applicationHost/applicationPools/applicationPoolDefaults/recycling/periodicRestart/schedule -value (New-TimeSpan -h 1 -m 30)

I have done this in the past through appcmd using something like the following but I would really like to do all of this through powershell:

%appcmd% set apppool "BusinessUserApps" /+recycling.periodicRestart.schedule.[value='01:00:00']

I have tried:

$WebAppPool.recycling.periodicRestart.schedule = (New-TimeSpan -h 1 -m 30)

This has the odd effect of turning the .schedule property into a timespan until I use $WebAppPool = get-item iis:\AppPools\AppPoolName to refresh the variable.

There is also $WebappPool.recycling.periodicRestart.schedule.Collection but there is no add() function on the collection and I haven't found any other way to modify it.

Does anyone know of a way I can set scheduled recycle times using syntax consistent with the code I have written above?

Chris Magnuson
  • 3,771
  • 10
  • 42
  • 46
  • Did you ever figure it out? – JohannesH Aug 14 '12 at 19:55
  • It's not exactly what you want, but possibly you could use `New-ItemProperty` similar to in https://docs.microsoft.com/en-us/iis/manage/powershell/powershell-snap-in-making-simple-configuration-changes-to-web-sites-and-application-pools or use `Add-WebConfiguration` as in https://blogs.iis.net/jeonghwan/iis-powershell-user-guide-comparing-representative-iis-ui-tasks (item 13, case 2) – Rory Aug 05 '21 at 20:30
  • What's so ugly with the `add-webconfiguration` example you gave? – Rory Aug 05 '21 at 20:33

2 Answers2

10

I could never figure out how to set this on the object itself, but after creating it the following works:

clear-ItemProperty IIS:\AppPools\MyPoolName -Name Recycling.periodicRestart.schedule #clear values
set-ItemProperty IIS:\AppPools\MyAppPoolName -Name Recycling.periodicRestart.schedule -Value @{value="00:00:00"} #to set it to midnight
geographika
  • 234
  • 2
  • 7
0

So not just

$webapppool.recycling.periodicrestart.schedule -Value "01:30:00" 

then? Or a ToString equivalent of the TimeSpan?

(I don't PowerShell; just a syntactic guess based on your other bits).

TristanK
  • 9,073
  • 2
  • 28
  • 39