Old File Cleaner

To go along with the purging of IIS log cleaner, this is a more generic recursive file cleaner using PowerShell for files based up on date specified.

#==============================================
#       --- Configuration Options ---
#Provide the name of the site
$mydir  = 'C:temp'
#Provide the number of days you wish to keep
$days = 4
#whatif toggle, verify only option
$whatif = $true
#==============================================

if($whatif)
{
get-childitem $mydir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -WhatIf }
	}
else
{
get-childitem $mydir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -ErrorAction SilentlyContinue} | Write-Host $_.fullname
}

In the case you need to run a similar function on older systems, here’s a more basic version suitable for a BAT file. FYI, there is no error checking option in this method.

FORFILES /P "C:temp" /S /D -7 /c "CMD /C del /F /Q @file"

Where 7 is the number of days you wish to keep the files. This also requires the FORFILES command to be available

IIS Log File cleaner

All too often I run across web servers with log files being stored until the drive runs out of space. If you are running IIS 7 or later, the following powershell script can automate this purging process.

#==============================================
#       --- Configuration Options ---
#Provide the name of the site
$myweb = "Default Web Site"
#Provide the number of days you wish to keep
$days = 90
#whatif toggle, verify only option
$whatif = $true
#==============================================

Import-Module WebAdministration

$myweb = Get-WebSite -Name "$myweb"
$mylogdir = $myweb.logFile.directory

if($whatif)
{
get-childitem $mylogdir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -WhatIf }
	}
else
{
get-childitem $mylogdir -recurse | 
	where {$_.lastwritetime -lt (get-date).adddays(-$days) -and -not $_.psiscontainer} |% {remove-item $_.fullname -force -ErrorAction SilentlyContinue} | Write-Host $_.fullname
}

Bulk DNS query test

If you ever run into periodic DNS look-up or connectivity problems this script may come into use. When pairing this with www.wireshark.org you can help troubleshoot periodic packet-loss issues.

@ECHO off
mode con:cols=110 lines=40
COLOR A
del ns_log.txt
cls
echo.
echo.
echo -----------------------------------------------------
echo starting lookup loop
echo -----------------------------------------------------
for /L %%X in (1,1,100) do (
nslookup %%X-DNS-test.mycompany.org 8.8.8.8
		
if "google"==^%%i (echo Fail on ^%%X) else (echo ok on ^%%X)
)
echo -----------------------------------------------------
echo lookup look finished
echo -----------------------------------------------------
echo.
echo.
echo >NUL 2>&1