Created
February 26, 2015 12:58
-
-
Save cdhunt/30297c23f49f93864b7b to your computer and use it in GitHub Desktop.
February 2015 NoVA PSUG Mini-Scripting Games Solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$WebRequest = Invoke-WebRequest http://www.presidentsusa.net/presvplist.html | |
$tables = @($WebRequest.ParsedHtml.getElementsByTagName("TABLE")) | |
$table = $tables[0] | |
$titles = @() | |
$rows = @($table.Rows) | |
$arrPresidents = @() | |
foreach($row in $rows){ | |
$cells = @($row.Cells) | |
if($cells[0].tagName -eq "TH"){ | |
$titles = @($cells | % { ("" + $_.InnerText).Trim() }) | |
continue | |
} | |
$resultObject = [Ordered] @{} | |
for($counter = 0; $counter -lt $cells.Count; $counter++){ | |
$title = $titles[$counter] | |
$resultObject[$title] = ("" + $cells[$counter].InnerText).Trim() | |
} | |
$a = $resultObject.President -split " " | |
$CustomObject = New-Object psobject | |
if ($a[2].Length -gt 3){ | |
$CustomObject | Add-Member -MemberType NoteProperty -Name "LastName" -Value $a[2] | |
} | |
else{ | |
$CustomObject | Add-Member -MemberType NoteProperty -Name "LastName" -Value $a[3] | |
} | |
$CustomObject | Add-Member -MemberType NoteProperty -Name "FirstName" -Value $a[1] | |
$arrPresidents += $CustomObject | |
} | |
$arrPresidents | Sort-Object LastName |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$contents = iwr 'http://www.presidentsusa.net/presvplist.html' | |
$rawNames = $contents.AllElements | Where-Object {$_.tagName -eq "TD" -and $_.innerText -match "^\d+"} | Select-Object -ExpandProperty innerText | |
$list = foreach ($item in $rawNames) | |
{ | |
$splitArray = $item.split(' ') | |
$president = [pscustomobject]@{Number = $splitArray[0].trim('.') | |
Firstname = $splitArray[1] | |
Lastname = $splitArray[-2] | |
Term = $splitArray[-1]} | |
Write-Output $president | |
} | |
$list | Sort-Object -Property Lastname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The President of the United States is both an honor and considered one of the world's most powerful people. The US Presidents List responsibilities include being the commander-in-chief of the military and leading the nation with the largest economy.