Search code examples
iis-7wmi

How can I set an app pool restart schedule in IIS7 via WMI / Powershell?


I am creating a function to manage App Pools in IIS7 to our desired specifications, and I am having trouble converting one line from our equivalent IIS6 script; to set the restart schedule.

In IIS6 the line was:

$newPool.PeriodicRestartSchedule = @( '6:15' );

The naive translation for IIS7 is:

$newPool.Recycling.PeriodicRestart.Schedule = @('00000000061500.000000:000')

however this doesn't work because it throws the exception:

Exception setting "Schedule": "Unable to cast object of type 'System.String' 
to type 'System.Management.ManagementBaseObject'.

How do I create this ScheduleElement[] array to assign to this value?

Edit: problem 1 down...:

$time = ([wmiclass]'root\WebAdministration:ScheduleElement').CreateInstance()
$time.Value = '00000000061500.000000:000'
$newPool.Recycling.PeriodicRestart.Schedule = $time;

Problem 2: This value doesn't seem to save when I call $newPool.Put(). What's next?


Solution

  • I've given up and decided to use appcmd:

    function CreateAppPool( [parameter(ValueFromPipelineByPropertyName=$true)][string]$AppPoolName ) {
        PROCESS {
            $appcmd = 'C:\Windows\SysWOW64\inetsrv\appcmd.exe'
            &$appcmd add appPool "/name:$AppPoolName" "/+recycling.periodicRestart.schedule.[value='06:15:00']"
        }
    }
    

    I am still interested in seeing how this can be done without appcmd.