Skip to content

Instantly share code, notes, and snippets.

@cdhunt
Created February 26, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdhunt/30297c23f49f93864b7b to your computer and use it in GitHub Desktop.
Save cdhunt/30297c23f49f93864b7b to your computer and use it in GitHub Desktop.
February 2015 NoVA PSUG Mini-Scripting Games Solutions
$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
$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
@MyCreditUnions
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment