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
    )

    trap [UnauthorizedAccessException] {
        Write-Error "$($Server): Unable to connect or Access is denied"
        continue
    }    
    
    $Servers | ForEach-Object {
        $Server = $_
        
        $WMI = New-Object Management.ManagementScope("\$ServerrootMicrosoftIISv2")
        $WMI.Options.Authentication = "PacketPrivacy"
        
        $Query = New-Object Management.ObjectQuery(
            "SELECT Name, LogFileDirectory, LogFileTruncateSize, LogType, LogFilePeriod FROM IIsWebServerSetting"
        )
        
        $Searcher = New-Object Management.ManagementObjectSearcher(
            $WMI,
            $Query
        )

        $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" }
                }
            }}
    }
}
PowerShell, IIS and log settings
Share this