PowerShell, IIS and log settings

A function to retrieve IIS log settings from a local or remote IIS 6 server. Written to be compatible with PowerShell 1.0.

Function Get-IISLogSetting
{
  Param([String[]]$Servers = $Env:Computername)

  $Servers | %{
    $Server = $_

    $WMI = New-Object Management.ManagementScope("\\$Server\root\MicrosoftIISv2")
    $WMI.Options.Authentication = "PacketPrivacy"
    $Query = New-Object Management.ObjectQuery( `
      "SELECT Name, LogFileDirectory, LogFileTruncateSize, " + `
      "LogType, LogFilePeriod FROM IIsWebServerSetting")

    $Searcher = New-Object Management.ManagementObjectSearcher($WMI, $Query)

    Trap [UnauthorizedAccessException]
    {
      Write-Error "$($Server): Unable to connect or Access is denied"
      continue
    }
    $Searcher.Get() | Select-Object `
      @{n='Name';e={ $Server }}, `
      @{n='Site';e={ $_.Name }}, `
      @{n='IIS Logging';e={
        Switch ($_.LogType) {
          0 { "Disabled" }
          1 { "Enabled" }
        } }}, `
      @{n='Log Path';e={ $_.LogFileDirectory }}, `
      @{n='Log File Size';e={
        If ([Int]$_.LogFileTruncateSize -eq -1) { "Unlimited" } else {
          "$([Int]$_.LogFileTruncateSize / 1Gb) Gb" } }}, `
      @{n='Log File Rollover';e={
        Switch ($_.LogFilePeriod) {
          0 { "Size" }
          1 { "Date" }
        } }}
  }
}

Related posts:

  1. Accept or reject messages from This function reads delivery restrictions from objects in Active Directory....

Related posts brought to you by Yet Another Related Posts Plugin.

Respond to this post