INTRODUCTION :
Hey Everyone, hope you guys are good!
Today’s blog post is to data harvest / Web scrape Quotations from the internet in your Powershell host.
But, why the hell I’d need Quotes in PowerShell console?
C’mon! because I’m a “Reading Quotes every morning” type of the guy 🙂 and I know many of you too like reading them. I daily spent some time reading quotes and I feel motivated, so thought it won’t be a bad idea to get Quotes directly in your PowerShell console
SCRIPT :
<# | |
.Synopsis | |
Get a Quote for any topc | |
.DESCRIPTION | |
Get-Quote cmdlet data harvests a/multiple quote(s) from Web outputs into your powershell console | |
.EXAMPLE | |
PS > Quote -Topic "success" | |
For me success was always going to be a Lamborghini. But now I've got it, it just sits on my drive. | |
Curtis Jackson [50 Cent], American Rapper. From his interview with Louis Gannon for Live magazine, The Mail on Sunday (UK) newspaper, (25 October 2009). | |
.EXAMPLE | |
PS > "love", "genius"| Quote | |
To be able to say how much you love is to love but little. | |
Petrarch, To Laura in Life (c. 1327-1350), Canzone 37 | |
Doing easily what others find it difficult is talent; doing what is impossible for talent is genius. | |
Henri-Frédéric Amiel, Journal | |
.EXAMPLE | |
PS > Get-Quote -Topic "Genius" -Count 2 | |
No age is shut against great genius. | |
Seneca the Younger, Epistolæ Ad Lucilium, CII | |
Genius is a capacity for taking trouble. | |
Leslie Stephen, reported in Bartlett's Familiar Quotations, 10th ed. (1919) | |
.INPUTS | |
Inputs to this cmdlet (if any) | |
.OUTPUTS | |
Output from this cmdlet (if any) | |
.NOTES | |
This cmdlet uses "https://en.wikiquote.org" to pull the information | |
.COMPONENT | |
The component this cmdlet belongs to | |
.ROLE | |
The role this cmdlet belongs to | |
.FUNCTIONALITY | |
The functionality that best describes this cmdlet | |
#> | |
function Get-Quote { | |
[CmdletBinding()] | |
[Alias("Quote")] | |
[OutputType([String])] | |
Param( | |
# Topic of the Quote | |
[Parameter(Mandatory = $true, | |
ValueFromPipeline = $true, | |
ValueFromPipelineByPropertyName = $true, | |
Position = 0)] | |
[ValidateNotNullOrEmpty()][String[]]$Topic, | |
[Parameter(Position = 1)][Int]$Count = 1 , | |
[Parameter(Position = 2)][Int]$Length = 150 | |
) | |
Process { | |
Foreach ($Item in $Topic) { | |
$URL = "https://en.wikiquote.org/wiki/$Item" | |
Try { | |
$WebRequest = Invoke-WebRequest $URL | |
$WebRequest.ParsedHtml.getElementsByTagName('ul') | | |
Where-Object { $_.parentElement.classname -eq "mw-parser-output" -and $_.innertext.length -lt $Length } | | |
Get-Random -Count $Count | | |
ForEach-Object { | |
$_.innertext | |
[Environment]::NewLine | |
} | |
} | |
catch { | |
$_.exception | |
} | |
} | |
} | |
} |
HOW TO RUN :
Run the function like I did in the animation below and it pulls Quotes from the internet depending on the parameter you choose and value you provide.
You could also, put this function in your $Profile , randomized with Keywords like Motivation, Success, Genius, Love and get Quotes every time you launch an instance of Powershell host.
Write-Host "Quote of the Day" -ForegroundColor Yellow | |
Get-Random "Genius","Love","Success","Failure","Intelligence" | Get-Quote |
Have fun, stay motivated and keep your game going 🙂 Signing off.
[…] on March 7, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
LikeLike
[…] Get-Quote using Powershell […]
LikeLike
[…] Get-Quote using Powershell […]
LikeLike