Powershell – get iis7 log directory within powershell

iis-7powershell

I am sure this is easy, but I can not find the answer. I am setting up a process to run a few tasks on the logging folder. Obviously I can manually create a variable with the log directory but this does change from machine to machine. I would like to be able to use some generic process.

What I have at the moment:

$iisLogDir = "D:\inetpub\Logs\LogFiles"
$now=get-date
$daysOld=31

get-childitem $iisLogDir -recurse | where { !$_.PSIsContainer -and ($_.lastWriteTime -le $now.addDays(-$daysOld)) } | remove-item -whatif

Thanks, These are 2008R2 machines.

Best Answer

You can use the IIS WebAdministration Module and the Get-WebConfigurationProperty cmdlet.

Import-Module WebAdministration
(Get-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory).Value