IT Dribble

Mutterings, inconsistant tips, rants and randomness

Delete Files Older than 90 Days with Powershell

by

Here is a powershell script that has been stolen, chopped up and modified.
Its logs the changes to Windows Event Viewer (because log files are so yesterday!) and removes files more than 90 days old from the directories specified.

# Script that deletes files older than 90 days in the E:Data directory.

Function Write-Log {
    [cmdletbinding()]

    Param(
    [Parameter(Position=0)]
    [ValidateNotNullOrEmpty()]
    [string]$Message
    )
	$evt=new-object System.Diagnostics.EventLog("Application")
	$evt.Source="Scans Directory Cleanup"
	$infoevent=[System.Diagnostics.EventLogEntryType]::Information
	$vdate=Get-Date
	$val="$Message"
	$evt.WriteEntry($val,$infoevent,2455) 

}

Function RemoveFilesAndDirs {

    Param(
    [Parameter(Position=0)]
    [array]$Paths,
    [Parameter(Position=1)]
    [ValidateNotNullOrEmpty()]
    [DateTime]$TargetDate
    )

    foreach ($path in $Paths){
        $files = (Get-ChildItem $path -recurse | where {$_.Lastwritetime -lt $TargetDate})
        Write-Log "Attempting to remove old files on $path"
        # Test if we are really moving files
        $re=0
        try {
            foreach ($file in $files){
                Write-Log ($file.fullname).ToString()
            }
        }
        catch [System.Management.Automation.RuntimeException] {
            $re = 1
            Write-Log "Nothing to delete"
        }
        if($re -ne 1){
            Get-ChildItem $path -recurse | where {$_.Lastwritetime -lt $TargetDate} | remove-item -recurse
        }
    }
}

$today = Get-Date -displayhint date
$targetdate = ($today.AddDays(-90))

$path = "E:/Data"
$directories = @("$path/temp","$path/scratch","$path/dev-temp","$path/rubbish")

Write-Log "Starting cleanup"
Write-Log "Files older than $targetdate will be deleted"

RemoveFilesAndDirs $directories $targetdate

Write-Log "*** Done ***"

Grant FullAccess to regular Active Directory user in Exchange 2010

by

In Exchange 2010 I needed to add a non mail-enabled user to use a shared mailbox. (Although technically it wasn’t a shared mailbox but a users mailbox in Exchange terminology) Add when I tried to grant the user Full Access Permissions I could only see Mail-Enabled users.

This required me to use the Powershell Command:

 Add-MailBoxPermission reception@localdomain.com -User:'CN=Full Name,OU=Users,OU=City,DC=localdomain,DC=com -AccessRights FullAccess 

What is interesting is that after performing the above, other non-mail enabled users could be added via the GUI afterwards….

Crack SHA512crypt ($6$) with John the Ripper with Native OpenMPI multi-threading

by

JtR now natively supports multi-threading through the OpenMPI interface. All the code is right there in the jumbo version of JtR all you need to do is install OpenMPI and un comment the lines of code in the makefile. Now you can crack SHA512crypt passwords with all cores.

Alright lets get started:

yum install openmpi
wget http://www.openwall.com/john/g/john-1.7.9-jumbo-6.tar.gz
tar -zxvf john-1.7.9-jumbo-6.tar.gz
cd john-1.7.9-jumbo-6/src

Now we need to edit Makefile and uncomment the ‘OpenMP’ lines.

vi Makefile

and uncomment the following line as per below:

OMPFLAGS =
# gcc with OpenMP
#OMPFLAGS = -fopenmp
OMPFLAGS = -fopenmp -msse2
# Sun Studio with OpenMP (set the OMP_NUM_THREADS env var at runtime)
#OMPFLAGS = -xopenmp
# icc with OpenMP (for make target linux-x86-64-icc)
#ICCOMPFLAGS = -openmp

now compile and run as per normal noticing that you now have 100% cpu usage!

Delete certain messages from Postfix Queue

by

Ever had some alerting software spam your production mail queue with 1000’s of alerts?
And you dont want to delete every message individually…

Here is the solution to delete all messages destined for user@example.com:

mailq | tail -n +2 | grep -v '^ *(' | 
gawk 'BEGIN {RS = ""} /user@example.com/ {print $1}' | 
tr -d '*!' | postsuper -d - 

Thanks to http://www.keithscode.com for that one!