Skip to content

Instantly share code, notes, and snippets.

@kvprasoon
Last active January 2, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kvprasoon/3b03b937a4fb71b97ec3 to your computer and use it in GitHub Desktop.
Save kvprasoon/3b03b937a4fb71b97ec3 to your computer and use it in GitHub Desktop.
Get-RSSFeed.ps1
<#
.Synopsis
Shows you the RSS Feeds for given URI.
.DESCRIPTION
This cmdlet Shows you RSS Feeds for a given URI.Results for this cmdlet includes Title,Link and a Short Description for specified Link.
.EXAMPLE
PS C:\WINDOWS\system32> Get-RSSFeed -Uri http://blogs.msdn.com/powershell -OutFilePath C:\Users\PR615733\Documents\RssFeedExample1.csv -OpenAfterExecution -Verbose
Title Link
----- ----
Desired State Configuration + Puppet http://blogs.msdn.com/b/powershell/archive/2015/10/06/desired-state-configuration-puppet.aspx
How to use WMF 4 with Azure DSC Extension in Azure Resource Manager (ARM) http://blogs.msdn.com/b/powershell/archive/2015/10/02/how-to-use-wmf-4-with-azure-dsc-extension...
re: Get-ScriptDirectory to the Rescue http://blogs.msdn.com/b/powershell/archive/2007/06/19/get-scriptdirectory.aspx#10646491
re: PowerShell DSC for Linux is now available! http://blogs.msdn.com/b/powershell/archive/2015/05/06/powershell-dsc-for-linux-is-now-available...
#>
function Get-RSSFeed
{
[CmdletBinding()]
[Alias('GRSS')]
Param
(
# URI for The BLog
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[String]
$Uri,
# Destination Path for .csv file
[ValidateNotNull()]
[String]$OutCsvPath ,
[switch]
$OpenAfterExecution
)
Begin
{
[array]$Rss=@()
if($PSVersionTable.PSVersion.Major -lt 3)
{
Write-Warning -Message "This cmdlet requires Minimum PowerShell Version 3.0";break
}
}
Process
{
try
{
$Blog=Invoke-WebRequest -Uri $Uri -ErrorAction Stop
$href=$Blog.ParsedHtml.getElementsByTagName("Link")|Where-Object{$_.type -eq "application/rss+xml" -and $_.href -like "http*" }|select href
Write-Verbose -Message "Checking For RSS Feed"
if(-not $href)
{
Write-Verbose -Message "No RSS Feed availability for $Uri"
Write-Warning -Message "No RSS Feed Available for $Uri"
}
else
{
$href.href|ForEach-Object{
Write-Verbose -Message "Found RSS Feed for $_"
[scriptblock]$scriptBLoxk = {
Param($Uri)
( [xml](Invoke-WebRequest -Uri $Uri) ).rss.channel.item |
Select Link, Title -First 2 |
ForEach-Object {
New-Object -TypeName PSObject -Property @{
Link = $_.link
Title = $_.Title
Description = if ($_.link) {
(Invoke-WebRequest -Uri $_.link).ParsedHtml.GetElementsByTagName('P')[2].Outertext
}
}
}
}
Write-Verbose -Message "Starting Job for $_"
$Rss+=(Start-Job -ScriptBlock $scriptBLoxk -ArgumentList $_).Id
Write-Verbose -Message "Created Job $($Rss[-1]) for $_"
}
}
}
catch
{
Write-Warning -Message "$Uri is not accessible or not found , Please verify and try again"
}
}
End
{
$Error.Clear()
if($Rss)
{
if($PSBoundParameters.ContainsKey('OutCsvPath'))
{
try{
$CSVOutput=Wait-Job -Id $Rss|ForEach-Object{
Write-Verbose -Message "Retreiving Results from Job $($_.id)"
Receive-Job $_|select Title,Link,Description
}
$CSVOutput|Export-Csv -Path $OutCsvPath -NoTypeInformation -ErrorAction Stop
if($PSBoundParameters.ContainsKey('OpenAfterExecution'))
{
Write-Verbose -Message "Opening File $OutCsvPath "
Invoke-Item -Path $OutCsvPath
}
}
Catch
{
if($Error.exception -like "*being used by another process*")
{
Write-Verbose -Message "Skipping Redirecting output as a .csv fileas $OutCsvPath is being Used by another Process"
Write-Warning -Message "Cannot Save The Output to $OutCsvPath as it is Already opened"
Write-Verbose -Message "Showing the Output to the Host"
$CSVOutput
}
}
}
else
{
Wait-Job -Id $Rss|ForEach-Object{
Write-Verbose -Message "Retreiving Results from Job $($_.id)"
Receive-Job $_ }|Format-Table Title,Link,Description -AutoSize
}
}
}
}
@KirillPashkov
Copy link

Hey there! Nice stuff here.

Just to point out that you might want to include proxy usage switches, AND, the provided example is using, I believe, outdated parameter name "OutFile Path" instead of "Out Csv Path".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment