Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jun 27, 2016

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
You can’t perform that action at this time.