Quantcast
Channel: background jobs – Learn Powershell | Achieve More
Viewing all articles
Browse latest Browse all 6

Turn Your PowerShell Console Title Bar Into A RSS/Weather Ticker

0
0

Something fun that I have been working on was a way to utilize the PowerShell console title bar for something other than just displaying a static message and then it hit me that the title bar would make an excellent automatically updating ticker for RSS feeds or even display the current weather conditions outside. After a little bit of time of figuring out the best way to accomplish this task, I was able to come up with a way to display a title ticker without preventing you from using the console and to also self update itself with new information to display like the image below.

image

[Update 29Jan2012] Modified some code to allow use in the ISE. Replaced [console]::title with $host.ui.rawui.windowtitle

RSS Ticker

First thing I need to do is get the RSS data from a news source, in this case, I am using Google News as my source. Using the following code, I will create a Webclient object and download the site and cast the type to XML. I won’t show every piece of code that I use in my modules but will try to show the important pieces that make it work.

$Rssurl='http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss'
$Webclient = new-object net.webclient 
$Webclient.UseDefaultCredentials = $True
$rss = [xml]$Webclient.DownloadString($Rssurl)

Now that I have the data as an XML object, I can then drill down to the actual data I need to look at the RSS feeds.

$rss.rss.channel.item | Select Title,PubDate

image

 

Now this is fine, but I want to make this into a function so I can re-use it later on. So I make a function called Get-RSSFeed that I will reference later on in this article.

Continuing on I still need to configure a timer that will kick off a specific action such as calling a function and updating the title bar in  PowerShell console.

First, I will create a timer object and set it to go off every 20 seconds.

    $timer = new-object timers.timer
    $timer.Interval = 20000 #20 seconds
    $Timer.Enabled=$True

I now need to configure an action script block that I can supply to run each time the Interval is reached using Register-ObjectEvent.

    $action = {
            $i++    
            If ($i -lt ($Rss.count -1)) {
                $host.ui.rawui.windowtitle = "{0} -- via PowerShell RSS" -f ($Rss[$i].Title)
            } Else {
                $Global:RSS = Receive-Job -Name RSS | Sort PublicationDate -Descending | Get-Unique -AsString | 
                Select PublicationDate,Title,Link
                $i=0            
                $host.ui.rawui.windowtitle = "{0} -- via PowerShell RSS" -f ($Rss[$i].Title)
            }
    } 

   Register-ObjectEvent -InputObject $timer -EventName elapsed `
   –SourceIdentifier  RSSTimer -Action $action | Out-Null
    
   $Timer.Start()

I have now defined my action block and registered the object event to go off each time the interval is reached. And I have also started the timer using the Start() method. When the timer object reaches the interval, the object event I created will kick off and run the action block in the background without causing any issues with my continued use of the PowerShell console.

 

You have undoubtedly noticed the Receive-Job that I have nested in the script block. The background that job that I use consists of just a few lines of code, but allow me pull the data I need without getting in the way of what I am doing on the console. Here is the code I use to set up the background job:

    Start-Job -Name RSS -ScriptBlock {
        Param ($Rssurl='http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss')
        While ($True) {
            $feed = Get-RSSFeed -Rssurl $Rssurl            
            Write-Output $feed
            #Wait
            Start-Sleep -seconds 180
        }
    } -ArgumentList $Rssurl -InitializationScript $GetRSSFeedsb | Out-Null

The  key takeaways from this code are that I only poll for RSS feeds every 3 minutes and you will also see that I am calling my Get-RSSFeed function from the job. But how is this possible when the function is existing only in memory outside the scope of the background job? Take note of the –InitializationScript parameter for the Start-Job cmdlet as that is the key to loading my function into the background job.

As shown in the code below, I pull the code from the function and then re-add it into a script block making sure to name the function.

$GetRSSFeed = (Get-Command Get-RSSFeed).definition
$GetRSSFeedsb = [scriptblock]::Create(
    "Function Get-RSSFeed {$GetRSSFeed}"
)

The –InitializationScript  parameter is used to configure my job session and by adding the new script block containing the function, it then loads that function into memory for the background job that I can now use. It is a pretty handy little trick I found while working on another script.

Module In Action

Lets see this in action now and start showing some Google News RSS feeds on the title bar!

Import-Module .\RSSTickerModule.psm1
Start-RSSTitleTicker -Rssurl 'http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss'

 

image

Now lets wait the 20 seconds that I defined and watch as the title bar updates automatically.

image

Weather Title Ticker

I also wrote a similar module for displaying the current weather. While the idea is exactly the same as the RSS ticker, there are some differences in how I pull the weather and how often it updates. Not really enough to go into here, but if you review the code in the module, you will see what I mean. But lets see it in action:

Import-Module .\WeatherTicker.psm1
Start-WeatherTitleTicker -ZipCode 68123

image

Yea, it’s a little chilly.  This will auto-update every 5 minutes and pull down updated weather information (if it has been updated by Yahoo Weather) and display it on the console title bar.

Downloads

You can download both of these modules by clicking on each link.

WeatherConsoleTickerModule

RSSConsoleTickerModule

In Closing

I think this is really just the beginning of some cool things that you can use the console title bar for to display automatically updating data such as the RSS feeds and Weather forecast. I could see displaying sports scores, twitter feeds among other things. I am curious as to what everyone else can come up with using this or a similar technique.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images