Get-Quotation.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID 6c1560ae-2a1d-4033-a85a-1c2942b6be8a
 
.AUTHOR PrateekSingh
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS Powershell WebScraping Automation
 
.LICENSEURI
 
.PROJECTURI https://geekeefy.wordpress.com/2017/03/07/get-quote-using-powershell/
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 Get-Quote cmdlet data harvests a/multiple quote(s) from Web outputs into your powershell console
 
#>
 

param()

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


    )

    Begin
    {

    }
    Process
    {
        Foreach($Item in $Topic)
        {
            $URL = "https://en.wikiquote.org/wiki/$Item"
            Try
            {
                $WebRequest = Invoke-WebRequest $URL
                $WebRequest.ParsedHtml.getElementsByTagName('ul')  |`
                Where-Object{$_.parentElement.id -eq "mw-content-text" -and $_.innertext.length -lt $Length} |`
                Get-Random -Count $Count |`
                ForEach-Object{ 
                                [Environment]::NewLine            
                                $_.innertext
                }
            }
            catch
            {
                $_.exception
            }
        }
    }
    End
    {
    }
}

"love", "genius"| Quote