The majority of SCCM administrators will have included a list of maintenance tasks for ensuring optimum performance of their environment. One area that often gets overlooked however is the maintenance of IIS logs. Logs are obviously useful for troubleshooting purposes, however do you really need to know what was happening say over 30 days ago. The truth is if there was an issue you probably would have spotted it very early on and dealt with it.

So how do you deal with log files within your IIS directory filling up your disks?. The answer can be to setup a configuration item and let SCCM take care of its own compliance.

Creating Your SCCM Server Device Collection

To start with you will need to create a Device Collection to deploy the Configuration Item and subsequent Baseline to. This can be achieved by creating a device collection using the following query;

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.ResourceNames in (Select ServerName FROM SMS_DistributionPointInfo)

Creating Your SCCM IIS Prune Logs Configuration Item

Once you have the device collection created and populated you can go on to create the Configuration Item to be used. To speed up this process I have exported the CI CAB for you to download, below are some screen shots of the manual creation process;

  1. Create Configuration Item

  2. Select Supported Platforms

    In this section we will pick which operating systems are applicable for SCCM distribution points

  3. Settings

    Here we will create a new setting entry that will contain both the discovery and remediation scripts required

    Discovery Script
    # Specify the maximum log age file to maintain
    $MaxDays = 7
    
    function DiscoverIISLogs ($MaxDays)
    {
    	try
    	{
    		# Import IIS WebAdmin Module
    		Import-Module WebAdministration
    		
    		# Return list of IIS webistes
    		$IISSites = Get-Website
    		# Loop for each IIS site
    		foreach ($Site in $IISSites)
    		{
    			# Return path for IIS logs
    			$IISLogs = $Site.LogFile.Directory
    			# Condition to replace DOS %SystemDrive% variable with Powershell variable
    			If ($IISLogs -like "*%SystemDrive%*")
    			{
    				$IISLogs = $IISLogs -replace "%SystemDrive%", "$env:SystemDrive"
    			}
    			# Count IIS Log files to prune
    			$LogCount = $LogCount + (Get-ChildItem -Path $IISLogs -Recurse -Filter "*.log" | Where-Object { $(Get-Date).Subtract($_.LastWriteTime).Days -gt $MaxDays}).count
    		}
    		Return $LogCount
    	}
    	catch { return -1 }
    }
    
    DiscoverIISLogs ($MaxDays)

     

    Remediation Script
    # Specify the maximum log age file to maintain
    $MaxDays = 30
    
    function PurgeIISLogs ($MaxDays)
    {
    	try
    	{
    		# Import IIS WebAdmin Module
    		Import-Module WebAdministration
    		
    		# Return list of IIS webistes
    		$IISSites = Get-Website
    		# Loop for each IIS site
    		foreach ($Site in $IISSites)
    		{
    			# Return path for IIS logs
    			$IISLogs = $Site.LogFile.Directory
    			# Condition to replace DOS %SystemDrive% variable with Powershell variable
    			If ($IISLogs -like "*%SystemDrive%*")
    			{
    				$IISLogs = $IISLogs -replace "%SystemDrive%", "$env:SystemDrive"
    			}
    			# Purge IIS Log files
    			Get-ChildItem -Path $IISLogs -Recurse -Filter "*.log" | Where-Object { $(Get-Date).Subtract($_.LastWriteTime).Days -gt $MaxDays } | Foreach-Object { Remove-Item $_.FullName -Force -Verbose }
    		}
    	}
    
    catch {  }
    }
    
    PurgeIISLogs ($MaxDays)
    

     

  4. Compliance Rules

    Here we will specify when to run the remediation script you created in the previous step

Configuration Baseline

Now in order to apply the Configuration Item we will need to create a Configuration Baseline for the SCCM Server Device Collection.

  1. Create a new Configuration Baseline

  2. Set Evaluation Conditions

    Here we will add the Configuration Baseline we created earlier;

  3. Deploy the Configuration Baseline

    Set your schedule and generate an alert if required..

Monitoring

Now sit back, relax and watch your good work take hold across your estate;

Savings Examples

Below we have the before and after results of a distribution point running a low workload;

Before (Manual Run)

After (Manual Run)

When you have a large environment with thousands of clients you can clearly see the storage recovery benefits!.

Manual Script

If you want to run the script manually or deploy as a scheduled task, you can use the below PowerShell script (Download from https://gallery.technet.microsoft.com/scriptcenter/IIS-Log-Maintenance-6f357306);

<#	
	.NOTES
	===========================================================================
	 Created on:   	07/11/2016 22:05
	 Created by:   	Terence Beggs & Maurice Daly - DeployEverything.com
	 Organization: 	
	 Filename:     	PruneIISLogs.ps1
	===========================================================================
	.DESCRIPTION
		Clear up script for IIS logs. Removes logs older than the value specified 
		in the MaxDays variable.

		The script can be used as part of a Configuration Item in SCCM or as a scheduled
		task.
#>

# Specify the maximum log age file to maintain
$MaxDays = 7

function DiscoverIISLogs ($MaxDays)
{
	try
	{
		# Import IIS WebAdmin Module
		Import-Module WebAdministration
		
		# Return list of IIS webistes
		$IISSites = Get-Website
		# Loop for each IIS site
		foreach ($Site in $IISSites)
		{
			# Return path for IIS logs
			$IISLogs = $Site.LogFile.Directory
			# Condition to replace DOS %SystemDrive% variable with Powershell variable
			If ($IISLogs -like "*%SystemDrive%*")
			{
				$IISLogs = $IISLogs -replace "%SystemDrive%", "$env:SystemDrive"
			}
			# Count IIS Log files to prune
			$LogCount = $LogCount + (Get-ChildItem -Path $IISLogs -Recurse -Filter "*.log" | Where-Object { $(Get-Date).Subtract($_.LastWriteTime).Days -gt $MaxDays}).count
		}
		Return $LogCount
	}
	catch { return -1 }
}

DiscoverIISLogs ($MaxDays)

function PurgeIISLogs ($MaxDays)
{
	try
	{
		# Import IIS WebAdmin Module
		Import-Module WebAdministration
		
		# Return list of IIS webistes
		$IISSites = Get-Website
		# Loop for each IIS site
		foreach ($Site in $IISSites)
		{
			# Return path for IIS logs
			$IISLogs = $Site.LogFile.Directory
			# Condition to replace DOS %SystemDrive% variable with Powershell variable
			If ($IISLogs -like "*%SystemDrive%*")
			{
				$IISLogs = $IISLogs -replace "%SystemDrive%", "$env:SystemDrive"
			}
			# Purge IIS Log files
			Get-ChildItem -Path $IISLogs -Recurse -Filter "*.log" | Where-Object { $(Get-Date).Subtract($_.LastWriteTime).Days -gt $MaxDays } | Foreach-Object { Remove-Item $_.FullName -Force -Verbose }
		}
	}

catch {  }
}

PurgeIISLogs ($MaxDays)

 

References

A special thanks goes out to Johan Arwidmark for sharing this clean up script on his DeploymentResearch blog (http://deploymentresearch.com/Research/Post/563/A-little-bit-of-ConfigMgr-housekeeping).

 

Maurice Daly
Maurice has been working in the IT industry since 1999 and was awarded his first MVP Enterprise Mobility award in 2017. Technology focus includes Active Directory, Group Policy, Hyper-V, Windows Deployment (SCCM & MDT) and Office 365.

(1546)

There are no comments.

Leave a Reply