Wednesday, 18 July 2012

LttP: Powershell Games 2012 - Beginner Event 3

Again, here's my entry for the Beginner Event 3 before I read the expert commentary.

<#
 .Synopsis
 2012SG_BE3 is the entry for the 2012 Powershell Scripting Games Beginner Event 3
 
 When run it will create a text file listing the running Process Name and ID.
 
 .Description
 2012SG_BE3 will create a text file listing the name and ID of all the running Process on the specified computer.
 
 If run multiple times results will be appended to the end of the file.
 
 .Example 2012SG_BE3.ps1 -ResultsRoot "D:\ScriptingGames" -ComputerName "HomeLaptop"
 
 Will store the name and ID's of the running processes on the computer named HomeLaptop in "D:\ScriptingGames\2012SG\event3\process3.txt"
 
 .Example 2012SG_BE3.ps1
 
 Will store the name and ID's of the running processes on the localhost in "C:\2012SG\event3\process3.txt"
 
 .Parameter ResultsRoot
 The -ResultsRoot parameter is used to change the Root folder of the Results.  
 If left blank the System Drive (as defined in the Environment Variables) will be used.
 
 If the trailing "\" is missing it will be added by default.
 
 .Parameter ComputerName
 The -ComputerName parameter is used to define the computer to collect the running Processes from.  
 If left blank "localhost" will be used.
    
 .Notes
    NAME: 2012SG_BE3
 AUTHOR: Owen Ballentine
 LASTEDIT: 04/27/2011 12:11:58
 
 .Link 
 http://sid351.blogspot.co.uk/
#>
Param([string]$ResultsRoot,[string]$ComputerName)
$FileLocation=""
If($ResultsRoot)
{
 $FileLocation=$ResultsRoot
 If($ResultsRoot.Substring($ResultsRoot.Length-1) -ne "\")
 {
  $FileLocation+="\"
 }
}
else
{
 $FileLocation=$env:SystemDrive
 $FileLocation+="\"
}
If(!$ComputerName)
{
 $ComputerName="localhost"
}
$FileLocation+="\2012SG\event3\"
Function Check-Path
{
param($Location, $TypeOfPath)
 If(!(Test-Path $Location))
 {
  New-Item -Path $Location -ItemType $TypeOfPath
 }
}
Check-Path $FileLocation "directory"
$FileLocation += "process3.txt"
Add-Content $FileLocation (Get-Process -ComputerName $ComputerName | Format-Table -Property "ProcessName","ID" -AutoSize | Out-String) 
I've made a mental note to look for a way to make PS scripts look nicer in the blog. Done...a bit. :)

UPDATE:

Looking at the expert commentary, I would've lost a point or two for not putting any error handling in the Check-Path function.  The expert setup error handling and then went one step further to default to the current users "My Documents".  Also, I needlessly added the "ComputerName" element.

At least I didn't fall for the hardcoded "C:\" trap that was set :)

1 comment:

Mike F Robbins said...

Actually, the hard coded "C:\" wasn't a trap and it was clarified in the comments posted at the bottom of the page for this event:
http://blogs.technet.com/b/heyscriptingguy/archive/2012/04/04/2012-scripting-games-beginner-event-3-create-a-file-in-a-folder.aspx

Here's what I submitted for this event:
http://2012sg.poshcode.org/2919

And here's the blog I wrote about it:
http://mikefrobbins.com/2012/04/12/toughest-event-yet-2012-powershell-scripting-games-beginner-event-3/

Mike F Robbins
http://mikefrobbins.com