Wednesday, December 20, 2017

Remove iislogs

Built a nice script to remove older files from IISLogs.
It has two retentionmodes. Either clear based on number of last changed files or for specific days.

$scriptPath = $(split-path -Parent $myinvocation.MyCommand.Definition)
Import-Module "$($scriptPath)\XssIisTools.psm1" -Force
#$logPaths = "D:\LOGS\IIS\W3SVC2000719483","D:\LOGS\IIS\W3SVC184136160","D:\LOGS\IIS\W3SVC1417816532" #variant1 only selected folders
$logPaths = Get-ChildItem -Path "D:\LOGS\IIS\" |where-object {$_.PSIsContainer} |select -ExpandProperty FullName #variant2 all subfolders folders in selected path
$filetypes = "u_ex*.log" #what files to delete
$numerOfFileToKeep = 30
$logFileName = "LastRunLog.txt"
$logFilePath = Join-Path -Path $scriptPath -ChildPath $logFileName
Start-Transcript -Path $logFilePath
foreach ($logPath in $logPaths) {
remove-IISLogs -logPath $logpath -fileTypes $filetypes -count $numerOfFileToKeep
}
remove-IISLogs -logPath "D:\LOGS\ELMAH\App_Data" -fileTypes "*.xml" -retentionMode Days -count 30
Stop-Transcript
<#
.Synopsis
Removes files of type $filetypes from folder $logpath
.DESCRIPTION
author: xxx
created:2016-01-02
lastModified:2017-11-14
.EXAMPLE
remove-IISLogs -logPath "D:\Utils\loggTest\W3SVC442871005" -fileTypes u_ex*.log
.EXAMPLE
remove-IISLogs -logPath "D:\Utils\loggTest\W3SVC442871005" -fileTypes *.xml -retentionMode Days
#>
function remove-IISLogs {
[CmdletBinding(SupportsShouldProcess=$true,confirmImpact="Medium")]
Param (
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$logPath,
$fileTypes = "*.log",
[ValidateSet("Days","Files")]
$retentionMode = "Files",
$count = 7
)
Process {
if (Test-Path -Path $logPath) {
$AllFiles = Get-ChildItem -Path $logpath -Filter $filetypes
if ($AllFiles -ne $null -and -$allfiles.count -ne 0) {
if ($retentionMode -eq "Files") {
write-verbose "Using files as retentionMode"
$oldFiles = $allfiles |Sort-Object lastWriteTime -Descending|select -Skip $count
}
elseif ($retentionMode -eq "Days") {
write-verbose "Using days as retentionmode"
$oldFiles = $AllFiles| where-object { $_.LastWriteTime -lt (get-date).AddDays(-$count)}
}
else { write-output "OldFiles couldn't be numbered" }
if ($OldFiles.count -gt 0 -and $oldFiles -ne $null) {
try {
if ($PSCmdlet.ShouldProcess("$logPath (filetypes:$filetypes) with retentionMode $retentionMode, count $count")) {
$oldFiles |Remove-Item
} #end of whatif
write-output "$($oldFiles.count) Files removed in $logPath"
}
catch {write-output "Couldn't remove..." }
}
else { write-warning "No files found (of $($allFiles.count)) matching $($fileTypes) in $logPath" }
} #end total files found check
else { write-warning "No files found in directory matching $($filetypes)" }
}
else { write-warning "Path not found :> $($logpath)" }
} #end of process
} #end of function

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...