INTRODUCTION – Get-Quote using Powershell :
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 the real question is – 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 because most of my time is consumed in the shell.
and who doesn’t want some love and success quotes in their life?
SCRIPT :
This file contains 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
<# | |
.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.
This file contains 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
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.
Prateek Singh
Related posts
5 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,131,836 People Reached
[…] on March 7, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
[…] Get-Quote using Powershell […]
[…] Get-Quote using Powershell […]
I like this but the script isn’t showing.
You can find the script here and this page has been updated as well. Thanks.
https://gist.github.com/PrateekKumarSingh/24e21500fcbe40b3a5fd8f48494259cb