PS C:\Users\bob> function Get-PipelineBeginEnd {
>> param (
>> [string]$SomeInput
>> )
>> begin {
>> "Begin: The input is $SomeInput"
>> }
>> process {
>> "The value is: $_"
>> }
>> end {
>> "End: The input is $SomeInput"
>> }
>> }#Get-PipelineBeginEnd
PS C:\Users\bob> 1, 2, 3 | Get-PipelineBeginEnd -SomeInput 'Test'
Begin: The input is Test
The value is: 1
The value is: 2
The value is: 3
End: The input is Test
PS C:\Users\bob> function Show-Pics {
>> [CmdletBinding()]
>> param (
>> [Parameter(Mandatory = $true,
>> ValueFromPipeline = $true,
>> HelpMessage = 'i.redd.it or v.redd.it or imgur URL')]
>> [ValidatePattern('i.redd.it|v.redd.it|imgur')]
>> [string]$URL
>> )
>>
>> begin {
>> Write-Verbose "Starting Show-Pics function."
>> }
>> process {
>> try {
>> Start-Process $URL -ErrorAction Stop
>> Write-Verbose "Browser launch successful."
>> }
>> catch {
>> Write-Error $_
>> }
>> }
>> end {
>> Write-Verbose "All done."
>> }
>> }#Show-Pics
PS C:\Users\bob> function Get-Reddit {
>> [CmdletBinding()]
>> param
>> (
>> [Parameter(Mandatory = $true,
>> ValueFromPipeline = $false,
>> Position = 1,
>> HelpMessage = 'The name of the desired subreddit')]
>> [string]$Subreddit,
>>
>> [Parameter(Mandatory = $false,
>> ValueFromPipeline = $false,
>> Position = 2,
>> HelpMessage = 'The number of threads that will be pulled down')]
>> [ValidateRange(1, 25)]
>> [int]$Threads = 3,
>>
>> [Parameter(Mandatory = $false,
>> ValueFromPipeline = $false,
>> Position = 3,
>> HelpMessage = 'Determines if pics will be shown (if available)')]
>> [switch]$ShowPics
>> )
>>
>> Write-Verbose "Specified subreddit: $Subreddit"
>> Write-Verbose "Specified # of threads: $Threads"
>>
>> $results = [System.Collections.ArrayList]@()
>>
>> Write-Verbose "Initiating Download"
>> $uri = "http://www.reddit.com/r/$Subreddit/.json"
>> Write-Verbose "URI: $uri"
>>
>> try {
>> $invokeWebRequestSplat = @{
>> Uri = $uri
>> ErrorAction = 'Stop'
>> }
>> $rawReddit = Invoke-WebRequest @invokeWebRequestSplat
>> Write-Verbose "Download successful."
>> }
>> catch {
>> Write-Error $_
>> return $results
>> }
>>
>> if ($rawReddit) {
>>
>> Write-Verbose "Converting JSON..."
>> $redditInfo = $rawReddit | ConvertFrom-Json
>>
>> Write-Verbose "Generating output..."
>> for ($i = 0; $i -lt $Threads; $i++) {
>> $childObject = $null #reset
>> $childObject = $redditInfo.data.children.data[$i]
>>
>> $obj = [PSCustomObject]@{
>> Title = $childObject.title
>> URL = $childObject.url
>> # PermaLink = $childObject.permalink
>> Score = $childObject.score
>> # Ups = $childObject.ups
>> # Downs = $childObject.downs
>> Author = $childObject.author
>> Comments = $childObject.num_comments
>> }
>> $results.Add($obj) | Out-Null
>> if ($obj.URL -like "*i.redd.it*" -or $obj.URL -like "*imgur*" -or $obj.URL -like "*v.redd.it*" -and $ShowPics) {
>> Show-Pics -url $obj.URL
>> }
>> }
>> }#if_rawReddit
>> else {
>> Write-Warning -Message 'No information was returned from reddit.'
>> }#else_rawReddit
>>
>> return $results
>>
>> }#Get-Reddit
PS C:\Users\bob> get-help Get-Reddit -showwindow
Synopsis
Get-Reddit [-Subreddit] <string> [[-Threads] <int>] [-ShowPics] [<CommonParameters>]
Parameters
-ShowPics <>
Required? false
Position? 3
Default value
Accept pipeline input? false
Accept wildcard characters?
-Subreddit <string>
Required? true
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters?
-Threads <int>
Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters?
Syntax
Get-Reddit [-Subreddit] <string> [[-Threads] <int>] [[-ShowPics] ] [<CommonParameters>]
Inputs
None
Outputs
System.Object
Remarks
None
PS C:\Users\bob> Get-Reddit -Subreddit calgary -threads 3 -showpics
Title : Weekly /r/Calgary jobs/careers discussion
URL : https://www.reddit.com/r/Calgary/comments/i8fbwk/weekly_rcalgary_jobscareers_discussion/
Score : 8
Author : AutoModerator
Comments : 6
Title : YYC Light Show
URL : https://i.redd.it/byd3apl8plg51.jpg
Score : 850
Author : OperatorOrange
Comments : 22
Title : Calgary, can I give anyone a hand at all?
URL : https://www.reddit.com/r/Calgary/comments/i8m2ai/calgary_can_i_give_anyone_a_hand_at_all/
Score : 186
Author : Svpzk
Comments : 78
reference:
No comments:
Post a Comment