Tuesday, February 19, 2013

Winter Scripting Games event 2

In this post I want to show you how I would have solved the second 2013 Winter Scripting Games event, whose statement was:

"Your organization is preparing to renew maintenance contracts on all of your server computers. You need to provide management with a report that details each server’s computer name, manufacturer, model, number of logical processors, and installed physical memory. Most of the servers do not have Windows PowerShell installed. None of the servers a separated from your client computer by a firewall. The server computer names are provided to you in a file named C:\Servers.txt, which contains one computer name per line.

Minimum Requirements

Optional Criteria
Suppress error messages if a computer cannot be contacted

Desired Output
ComputerName Model Manufacturer LogicalProcs PhysicalRAM
SERVER2 PowerEdge Dell 8 128 SERVER3 Proliant Compaq 2 4"

This is not a complex task, but it demands a good general knowledge of Powershell, especially if you are eager to solve it without Googling around for similar answers.

This is the script I would have posted:
get-wmiobject win32_computersystem -ComputerName (get-content C:\servers.txt) -erroraction silentlycontinue | format-table -property @{label='ComputerName';expression={$_.name}},Model,Manufacturer,@{label='LogicalProcs';expression={$_.NumberOfProcessors}},@{label='PhysicalRAM';expression={[Math]::Round($_.TotalPhysicalMemory/1GB)}}
As you can see I have kept it in a one-liner, because my opinion is that there is no need to write function for basic tasks which we are not going to perform again any soon. And over-doing is not a good habit, unless expressely required by the task.

The output looks like this:
ComputerName Model       Manufacturer LogicalProcs PhysicalRAM
------------ -----       ------------ ------------ -----------
server1      VMware V... VMware, Inc.            2           4
server2      VMware V... VMware, Inc.            1           4
server4      VMware V... VMware, Inc.            2           2
The computer 'server3' is missing from the list because it is powered off, but no error is showed because the optional criteria stated that we have to suppress error messages if it can't be contacted. I could have used Try Catch, but I choose to keep it simple as I feel this is what judges would prefer.

Feel free to comment!

For my solution for the first event, check here.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...