Gather IIS logs from your IIS servers

I needed a quick way to gather all IIS logfile locations in several SharePoint farms. IIS has a nasty way to reference to numbers instead of names in their logging directory structure, so you need to watch the Advanced Settings panel in IIS to figure out which logging directory is for which website.

This could be done easier, I thought. And so thought many before me.

This is a slight modification of the script by Chris Dent, because I needed different results to link site name (which IISWebServerSetting saves as ServerComment) to the specific logfile directory (which is a mishmash of LogFileDirectory and Name).

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

    $Servers | %{
        $Server = $_ ;
        Write-Progress "Scaning" $Server;
        $WMI = New-Object Management.ManagementScope("\$ServerrootMicrosoftIISv2");
        $WMI.Options.Authentication = "PacketPrivacy";
        $Query = New-Object Management.ObjectQuery("SELECT Name, ServerComment, 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;
        }
        $result = $Searcher.Get();
        #$Host.EnterNestedPrompt();
        $result | Select-Object @{
            n = 'Server';
            e = { $Server; }
        }, @{
            n = 'Website';
            e = { $_.ServerComment; }
        }, @{
            n = 'Log Path';
            e = { "{0}{1}" -f $_.LogFileDirectory, $_.Name.Replace("/",""); }
        }
    }
}

Usage:

Get-IISLogSetting [-servers server1,server2,server3,...]

Leave a Reply

Your email address will not be published. Required fields are marked *