There are many scripts out there using a variety of methods to delete IIS logs from servers using PowerShell. These scripts are usually written to run locally on servers, which can have two drawbacks:

  1. In environments with a large number of servers, scheduling and maintaining these scripts can require a significant amount of work;
  2. Depending on the security restrictions on production servers, administrators might not be able to schedule this script to run whether they are logged on to the server or not.

 

This was the scenario I found recently. In order to overcome both obstacles, I decided to run the script from a “script server” where security settings did not prevent cached credentials. Additionally, by deleting these logs remotely I could have a single script targeting multiple servers, making it much easier to manage!

As I have mentioned, there are many ways of deleting IIS logs from a server. The method I have been using lately uses the WebAdministration module and the Get-WebSite cmdlet to get a list of all websites on the local server.

 

To specify which servers to target, simply update the $excServers array within the script. We could also make this part of user input to allow users to specify which servers to action on more easily. Modules would be the next step    :)

 

More details regarding this script can be found here in my blog.

 

 

PowerShell
Edit|Remove
<# 
.SYNOPSIS 
Delete IIS log files from remote server 
 
.DESCRIPTION 
The script retrieves the location of IIS logs for all websites on a remote server and deletes those older than $Days days. 
 
.PARAMETER Days 
Specifies the number of days’ worth of IIS logs to keep on the server 
 
.EXAMPLE 
Deletes IIS logs older than 14 days from all servers manually specified within the script's $excServers array 
.\Delete-IISlogs.ps1 -Days 14 
 
 
.NOTES 
Name:     Delete-IISlogs.ps1 
Author:   Nuno Mota 
 
.LINK 
http://letsexchange.blogspot.ae/2017/02/delete-iis-logs-remotely-using.html 
https://gallery.technet.microsoft.com/Delete-IIS-Logs-Remotely-9d269a30 
#> 
 
 
 
[CmdletBinding()] 
Param ( 
    [Parameter(Position = 0, Mandatory = $False)] 
    [Int] $Days = 7 
) 
 
 
Function Write-Log { 
    [CmdletBinding()] 
    Param ([String] $Type, [String] $Message) 
 
    # Create a log file in the same location as the script containing all the actions taken 
    $Logfile = $PSScriptRoot + "\Delete-IISlogs_Log_$(Get-Date -f 'yyyyMMdd').txt" 
    If (!(Test-Path $Logfile)) {New-Item $Logfile -Force -ItemType File | Out-Null} 
 
    $timeStamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss") 
    "$timeStamp $Type $Message" | Out-File -FilePath $Logfile -Append 
     
    Write-Verbose $Message 
} 
 
 
 
################################################################# 
# Script Start 
################################################################# 
 
[Array] $excServers = @("server1""server2""server3""server4"ForEach ($server in $excServers) { 
    Write-Log -Type "INF" -Message "Processing $server" 
     
    If (Test-Connection -ComputerName $server -BufferSize 16 -Count 1 -ErrorAction 0 -Quiet) { 
        Try { 
            $countDel = Invoke-Command -ComputerName $server -ArgumentList $Days$server -ScriptBlock { 
                param($Days$server) 
                 
                [Int] $countDel = 0 
                Import-Module WebAdministration 
                ForEach($webSite in $(Get-WebSite)) { 
                    $dir = "$($webSite.logFile.directory)\W3SVC$($webSite.ID)".Replace("%SystemDrive%"$env:SystemDrive) 
                     
                    Write-Host "Checking IIS logs in $dir on $server" -ForegroundColor Green 
                    Get-ChildItem -Path $dir -Recurse | ? {$_.LastWriteTime -lt (Get-Date).addDays(-$Days)} | ForEach { 
                        Write-Host "Deleting"$_.FullName 
                        del $_.FullName -Confirm:$False 
                        $countDel++ 
                    } 
                } 
                 
                Return $countDel 
            } 
             
            Write-Log -Type "INF" -Message "Deleted $countDel logs from server $server" 
        } Catch { 
            Write-Log -Type "ERR" -Message "Unable to connect to $($server): $($_.Exception.Message)" 
            Send-MailMessage -From "ExchangeAdmin@domain.com” -To "user@domain.com" -Subject "ERROR – Delete IIS Logs" -Body "Unable to connect to $($server): $($_.Exception.Message)" -SmtpServer smtp.domain.com -Priority "High" 
        } 
    } Else { 
        Write-Log -Type "ERR" -Message "Unable to connect to $server" 
        Send-MailMessage -From "ExchangeAdmin@domain.com” -To "user@domain.com" -Subject "ERROR – Delete IIS Logs" -Body "Unable to connect to $server" -SmtpServer smtp.domain.com -Priority "High" 
    } 
}