PowerShell and IIS Logs

This is more of a pointer than an actual article.

Chris Dent at www.intended.co.uk has a great post on getting IIS log settings from hosts here: https://www.indented.co.uk/index.php/2010/01/12/powershell-iis-and-log-settings/

The best part is how he uses one of my favourite tricks - Select-Object -property @{...} synthetic properties.

However, I need to lear how to modify those settings, so, while this doesn't give me what I need, it gives me some starting points.

 ===

 # this is Chris' script

Function Get-IISLogSetting {
    param(
       [String[]]$Servers = $Env:Computername
    );
   
    $Servers | %{
        $Server = $_ ;
        Write-Progress "Scaning" $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;
        }
        $result = $Searcher.Get();
        #$Host.EnterNestedPrompt();
        $result | 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 = { $_.LogFileTruncateSize; }
        }, @{
            n = 'Log File Rollover';
            e = {
                Switch ($_.LogFilePeriod) {
                    0 { "Size"; }
                    1 { "Date"; }
                }
            }
        }
    }
}

 

# this is me forcefeeding the settings using adsutil.vbs and psexec, because adsutil.vbs GET <param> -s:computername doesn't work

function Set-IisLogSetting {
    param (
        [String[]]$computers = @(),
        [int]$size = 209715200,
        [switch]$byDate,
        [string]$psExecPath = "\\$env:userdnsdomain\olm\software\livemeeting\tools\pstools\psexec.exe"
    );
   
    if (!(Test-Path $psExecPath)) {
        Write-Error "Cannot find $psExecPath";
        return;
    }
   
    foreach ($computer in $computers) {
        $script:pingStatus = $null;
        & {
            trap { continue; }
            $script:pingStatus = (New-Object System.Net.NetworkInformation.Ping).Send($computer, 2000).Status;
        }

        if ($script:pingStatus -ne 'Success') {
            Write-Warning "$computer is not pingable.";
            continue;
        }
       
        $adsUtilVbs = "\\$computer\c$\inetpub\adminscripts\adsutil.vbs"
        if (!(Test-Path $adsUtilVbs)) {
            Write-Warning "Unable to find $adsUtilVbs.";
            continue;
        }
       
        Write-Progress $computer "Log File Size $size bytes";
        $null = & $psExecPath -acceptEula \\$computer -s C:\WINDOWS\system32\cscript.exe c:\inetpub\adminscripts\adsutil.vbs SET W3SVC\LogfileTruncateSize $size;
       
        Write-Progress $computer "Roll on $(if ($byDate) { 'date'; } else { 'size'; })";
        $null = & $psExecPath -acceptEula \\$computer -s C:\WINDOWS\system32\cscript.exe c:\inetpub\adminscripts\adsutil.vbs SET W3SVC\LogfilePeriod ([int][bool]$byDate);
       
        Get-IISLogSetting $computer;
    }
   
}