Tuesday, 14 May 2013

Scripting Games - Beginner Event 3


The Scripting Games are a six week event that lets you showcase your scripting skills based on six challenges. You can either choose compete in the Advanced or the Beginner’s track. I have registered for the beginner’s event since it is my first year being part of the games. It is a great learning experience to see such diverse approaches on any challenge. If you haven’t registered yet, it’s not too late. Please follow the link below for more details:


The Games just concluded its third event. The beginner track event challenged the competitors to come up with a one liner for the below scenario:  

Dr. Scripto has been fielding a lot of calls from the Help Desk lately. They’ve been asking him to look up information about the local hard drives in various servers –mainly size and free space information. He doesn’t mind helping, but all the requests have been getting in the way of his naps. He’s asked you to write a one-liner command that can get the information for him – and he wants the output in an
HTML file.

















The Doctor says you don’t need to parameterize your command – it’s okay to write it to run against localhost, and he can just change that computer name as needed in the future. The resulting HTML does need to go into an HTML file on disk someplace, and he wants you to pay special attention to the following:


• The browser displays “Disk Free Space Report” in the page tab when viewing the report.
• “Local Fixed Disk Report” is in the H2 (“Heading 2”) HTML style.
• The report ends with an HTML horizontal rule and the date and time that the report was generated.
• The size and free space values are shown as gigabytes (GB) and megabytes (MB) respectively, each to two decimal places.

The command you write can assume that both WMI and CIM are available on the remote computers, and that all the necessary firewall rules and authentication have already been taken care of.


 The one-liner solution I came up for the challenge was:

Get-WmiObject -Class "win32_logicaldisk" -Computername "localhost"-Filter "Drivetype = 3" | Select-Object @{Name="Drive";Expression={$_.DeviceID}},@{Name="Size(GB)";Expression={"{0:N2}" -f ($_.Size/1GB)}},@{Name="FreeSpace(MB)";Expression={"{0:N2}" -f ($_.FreeSpace/1MB)}} | ConvertTo-Html -title "Drive Free Space Report" -precontent "<H2>Local Fixed Disk Report</H2>" -body $_  -postcontent "<hr>",(get-date) | Set-Content "C:\FreeDiskReport.htm"

The report generated using the above command:





1 comment: