Hi Guys and fellow powershell scripters :),
Well, i have been busy for the past few days and now time to write my next blog.
As you already know from the title, yes am going to write a powershell script which generate a html report of the disk spaces details on a given list of servers and then email that report to the administrator or any email address.
What is needed?
A file named sl.txt on the directory where you have this script copied, this file will have the list of computers, one per line.
Before i start to go into details, you can simply get the diskspace info for any computer by the following powershell cmd-let
Get-WmiObject win32_logicaldisk | format-table DeviceID, VolumeName,status,Size,FreeSpace | Out-File FreeSpace.txt
Well many in the world dont like some black&white text report, they just dont like it, well lets create a HTML report of this diskspace info and then email it.
So, lets jump in and get his badboy started.
My Powershell script contains 5 important functions, which do their own job.
Below are the details of those 5 functions:
1.writeHtmlHeader: This function write the HTML tags to create the html file and then kick start the process.
This function also write some CSS styling info, so dont worry about any of this, i have already taken the load of going through them and creating it, so this is just for your info.
Below is the code for this function:
2.writeHtmlFooter - This function simply updates the file in the end to add the closing html tags , the same old </body> and the </html> tags.
3. sendEmail - This function sends email to the given address, it takes in from,to,subject,smtphost,htmlfile as the input and then composes the message and then emails the file.
4.writeTableHeader - This function simply writes the html tags for a table, my idea was for each and every computer to have a seperate table header, for eg., drive letter C:\ and the the lable of the drive "OS", and then total space ,free space etc., these info will be the table header for each computer.
5. writeDiskInfo - This is the actual function which write the disk details to the html file, it also calculates the free space percentage etc.,
On top of the script, i have setup some variable like the sl.txt file which will contain the server names, you can change it to whatever name of the file you want or you take in parameter from the user and then process it, it is left to you.
I have two more variables named lke $warning and $critical, i have set them to 85 and 70, these values will be compared against the free disk space percentage and then if a particular drive is above the critical or warning threshold, that particular drive will marked in RED and Yellow respectively.
You will get an idea of what i am talking about when you see the screen shot below:
Here is the complete Script: