Powershell Script To Clear Old IIS Logs

If you have many sites running on your installation of Windows Server, you will soon find that there will be an accumulation of logs generated by IIS. Through my niavity, I presumed that there is a default setting in IIS that would only retain logs for a specific period of time. It is only when I started noticing over the last few weeks the hard disk space was slowly getting smaller and smaller.

Due to my sheer embaressment, I won't divulge how much space the logs had taken up. All I can say, it was quite a substantial amount. :-)

After some Googling online, I came across a Powershell script (which can be found here), that solved all my problems. The script targets your IIS logs folder and recusively looks for any file that contains ".log" for deletion. Unfortunately, the script did not run without making some minor modifications to the original source. This is due to changes in versions of Powershell since the post was written 3 years ago.

$logPath = "C:\inetpub\logs\LogFiles" 
$maxDaystoKeep = -5
$cleanupRecordPath = "C:\Log_Cleanup.log" 

$itemsToDelete = dir $logPath -Recurse -File *.log | Where LastWriteTime -lt ((get-date).AddDays($maxDaystoKeep)) 

If ($itemsToDelete.Count -gt 0)
{ 
    ForEach ($item in $itemsToDelete)
    { 
        "$($item.FullName) is older than $((get-date).AddDays($maxDaystoKeep)) and will be deleted." | Add-Content $cleanupRecordPath 
        Remove-Item $item.FullName -Verbose 
    } 
} 
Else
{ 
    "No items to be deleted today $($(Get-Date).DateTime)." | Add-Content $cleanupRecordPath 
}    

Write-Output "Cleanup of log files older than $((get-date).AddDays($maxDaystoKeep)) completed!" 

Start-Sleep -Seconds 10

If you're ever so inclined, hook this script up to a Scheduled Task to run on a daily basis to keep your log files in order.

Evernote Has Made Me An Extreme Data Hoarder

EvernoteOk. So for those of you have not heard of Evernote (and who hasn't!?), it's an online app/service that allows you to record voice, text and hand written notes that can synchronise across multiple devices and platforms.

Ever since I had my first smartphone, I've always relied on Evernote to record my daily thoughts and reminders. There are numerous note taking apps on the market, which (for me) just doesn't seem to cut the mustard and end up always coming back.

Evernote not only has the functionality, but it also has the infrastructure to make it more than just a "note taking" platform. So much so I'm hoarding major amounts of everyday things. Evernote is starting to act as a repository of things I don't want to let go of.

With the help of IFTTT, I have created numerous recipes that aggregate data from my social platforms such as Instagram and Twitter to importing RSS feeds from websites that interest me. Now Evernote is my one-stop-shop for getting everything I need on a daily basis instead of logging into different platforms individually.

If there is something I happen to like, I just Evernote it. Even if I won't ever need it. Typical sign of a hoarder! But I'm an organised data hoarder, utilising clearly named notebook stacks. Strangely enough, the more notes you add, the more useful Evernote becomes and this maybe the reason why I am hoarding so many things. It's more than a "note taker"!

One feature I didn't expect to be so useful was the ability to take pictures of printed or handwritten documents. I can take quick snapshots and go completely paperless. On top of that, Evernote makes everything searchable. It's even clever enough to search through my rubbishly written notes. I only found out how truly powerful this feature until I was going through the motions of purchasing my first property. At this time of my life, I was in constant note/documentation mode and Evernote helped me organise my thoughts, reminders and record all email correpondence neatly.

What I've done in the past with other note taking apps is delete old notes or files just to be completely sure that I will be able to search what I require quickly and easily, mainly due to the fact that sifting through large volumes of data was a headache! Nowadays, I don't delete anything in Evernote. I can now keep a record of things I previously done and refer to later without any worries at time of need.

It's safe to say my addiction to Evernote will only increase as I find more uses for it. But that's not a bad thing...right?

Update - 12/12/2014

I came across some posts from others with the same issue, which is nice to know that it's not only me with a problem:

The Easy Way To Run a PHP Site In A Windows Environment

Even though my programming weapon of choice is .NET C#, there are times (unfortunate times!) where I need to dabble in a bit if PHP. Being a .NET developer means I do not have the setup to run PHP based sites such as Apache and MySQL.

In the past I have tried to create an Apache configured server but I could never get it running 100% - possibly because I didn't have the patience or could justify the additional time required for setup when I could be working on a PHP site once in a blue moon...

Last year, I came across a program called EasyPHP that allowed me to install a local instance of Apache and MySQL altogether in just one installation. It made it really to get up and running without all the setup and configuration hassle.

Once installed you can create numerous websites and MySQL instances in a version of your own choosing. Wicked! I never been so excited about PHP in my life!

I have only scratched the surface on the features EasyPHP provides and whenever I did need to use it, there has always been great improvements. Take a look at their site for more information: www.easyphp.org.

So if you're a Windows man who needs to carry out PHP odds and ends, can't recommend EasyPHP enough.

Windows 2008 Task Scheduler Result Codes

I’ve been working on a PowerShell script that required to be automatically run every 5 minutes. As you probably guessed, using Windows Task Scheduler is the way to go.

Prior to assigning any scripts or programs to a scheduled task, I always run them manually first to ensure all issues are rectified.  We all know if there is an issue whilst running within Task Scheduler, Windows likes to help us by showing us some ambiguous error/success codes.

Luckily, MSDN provides a comprehensive list of these codes that can be found here: http://msdn.microsoft.com/en-us/library/aa383604

But there more common codes are listed below:

0 or 0x0: The operation completed successfully.
1 or 0x1: Incorrect function called or unknown function called.
2 or 0x2: File not found.
10 or 0xa: The environment is incorrect.
0x41300: Task is ready to run at its next scheduled time.
0x41301: Task is currently running.
0x41302: Task is disabled.
0x41303: Task has not yet run.
0x41304: There are no more runs scheduled for this task.
0x41306: Task is terminated.
0x8004131F: An instance of this task is already running.
0x800704DD: The service is not available (is 'Run only when an user is logged on' checked?)
0xC000013A: The application terminated as a result of a CTRL+C.
0xC06D007E: Unknown software exception.

FireShot – A Really Good Webpage Screen Capture Tool

Working in the web industry and having the opportunity to develop a wide variety of websites, I like to take a snap-shot of a few pages for my portfolio (working on that!). But I generally come into issues when taking a screen-shot of a very long webpage. In fact, I always experience issues when screen grabbing a scrolling page.

Luckily, I found a really useful add-on to Firefox called Fireshot. Fireshot makes it really easy to screenshot an entire page. Once you have made a screenshot, you can carry out the following tasks within the comfort of your browser:

  • Upload to Facebook, Picasa, Flickr.
  • Saved to disk as PDF/PNG/GIF/JPEG/BMP
  • Sent to clipboard
  • Print
  • E-Mail
  • Export

I was expecting this tool generate a screen grab really slowly. Even on long pages with a lot of content, images are generated quickly. Take a look at the screen-shot I made of "www.theverge.com" here.

Definitely try it out.

HTTP Request Script

In one of my website builds, I needed to output around a couple thousand records from a database permanently into the .NET cache. Even though I set the cache to never expire, it will get cleared whenever the application pool recycles (currently set to every 24 hours). As you can expect, if a user happens to visit the site soon after the cache is cleared, excess page loading times will be experienced.

The only way I could avoid this from happening is by setting up a Scheduled Task that would run a script that would carry out a web request straight after the application pool was set to recycle.

Luckily, I managed to find a PowerShell script on StackOverflow that will do exactly that:

$request = [System.Net.WebRequest]::Create("")
$response = $request.GetResponse()
$response.Close()

HTTrack - Website Copier

One of my colleagues pointed my to a really useful tool called HTTrack, that has the ability to download a website from the internet to a local directory by simply copying and pasting the site URL of your choice.

I found this tool helpful when working on an existing site where I was unable to 100% recreate the files and directory structure within my local development environment. For example, you could be developing a new micro-site for an existing online site you may not currently have in your local environment.

HTTrack has the ability to build recursively all directories, images and other files from the server to your computer. What’s even better, HTTrack arranges the original site's relative link-structure.

My First PowerShell Script - Moving Files From One Directory to Another

Microsoft’s new command-line tool, PowerShell has been out for quite a few years now and I thought today will be the day I would start using it. I needed to write a script that would move n number of files from one directory to another. This job seemed a perfect fit for PowerShell.

#Get 'n' number of files
$FileLimit = 10 

#Destination for files
$DropDirectory = "C:\Drop\"

$PickupDirectory = Get-ChildItem -Path "C:\Pickup\"

$Counter = 0
foreach ($file in $PickupDirectory)
{
    if ($Counter -ne $FileLimit)
    {
        $Destination = $DropDirectory+$file.Name

        Write-Host $file.FullName #Output file fullname to screen
        Write-Host $Destination   #Output Full Destination path to screen
        
        Move-Item $file.FullName -destination $Destination
        $Counter++
    }  
}

From the get go, I was really impressed with the flexibility of the scripting language. This is where command line fails. It is sufficient for simple tasks but not so much for complex jobs.

As you can see from the code above, I can implement complex operations that supports variables, conditional statements, loops (while, do, for, and foreach), and that’s just the start. I don’t know why I hadn’t used PowerShell sooner. If I didn’t have the option to use PowerShell, I would have probably created a C# service or executable to do the exact same thing. Time saver!

Since PowerShell is built on the .NET Framework, Windows PowerShell helps control and automate the administration of the operating system and applications that run on Windows. So if you are a C# programmer, you should feel comfortable in writing PowerShell scripts. All you need to be aware of is syntax differences when declaring variables and keywords.

To end with, I will quote an amusing forum post I found when researching the difference between good ol’ Command Prompt and PowerShell:

PowerShell has a default blue background and Command Prompt has a default black background.”

Speed Up Firebug

I noticed recently that Firebug was running very slow whilst inspecting elements or debugging client-side scripts. In the past when noticing performance issues in Firefox, a straight-forward opening and closing the browser normally resolved any issues.

What I found after investigating this problem online is that Firebug keeps a record of all breakpoints and sites where Firebug was used. As you can imagine, this will accumulate over time. To remove all history, go to your Firefox profile directory which can be found here:

C:\Users\<Windows User Account Name>\AppData\Roaming\Mozilla\Firefox\Profiles\<Firefox Profile ID>.default\firebug

You will find two files within Firebug folder directory:

  • annotations.json - contains a history of website browsing.
  • breakpoints.json - contains currently set breakpoints.

 

Close all running instances of Firefox and delete both files within the directory.

Judging by posts from other users online, there could be other contributing factors to Firebug's sluggishness at times. Hopefully, by carrying out the steps above you will notice a difference.

Stay out of trouble! Backup your files with RoboCopy

robocopyApologies for making a reference from the social-satire/sci-fi film that is RoboCop (1987) in my post title. It just had to be done when talking about some tool called RoboCopy. For those who aren’t aware of what RoboCopy is, where have you been? In all honesty, I myself never heard of it until a few days ago.

RoboCopy is a command-line run tool that allows you to copy files from one directory to another. One of its most popular uses for RoboCopy is it’s ability to copy large volumes of files quicker than carrying out a manual copy and paste through a GUI, making it ideal for backup jobs. So you could easily write a backup script to run via a Schedule Task on a daily basis.

I managed to backup around 80Gb of files in less than an hour. What’s even more impressive is that I could run numerous RoboCopy scripts at the same time. Currently, I have only run two scripts simultaneously just to be on the safe side.

Prior to RoboCopy, I was using another command-line tool: XCopy. For my backup purposes XCopy did exactly what I wanted it to do until I came across a misleading error message: “Insufficient memory”. You would think this message would mean the destination directory to where your files are copying to is full or not enough memory resources. In matter of fact this error only happens when the fully qualified file path is longer than 254 characters. Unfortunately, I couldn’t get around this error due to the nature of how my directories are structured. Luckily, RoboCopy doesn’t have this limitation.

One of the major strength’s of RoboCopy is the number of useful options you have at your disposal. A few example’s are:

  • Moving files.
  • Exclude certain files and file types.
  • Detailed logging that tells you new the files that have been copied or over-written.
  • Parameterised scripting.

Example Script

@ECHO OFF

ECHO PROCESSING BACKUP ...

robocopy \\work\Projects\ F:\Projects\Backup\ /mir /sl
/log:"F:\Logs\Projects-%date:/=%.log"

ECHO BACKUP COMPLETED!

 

The script I have provided is what I use to backup files through a Schedule Task that runs at the end of every day. This script mirrors the source drive exactly. So any files that have been deleted, updated or created will have the same effect on the backup drive. In addition, a log file is created when RoboCopy is running.

More Information