Geeks With Blogs

Lance's TextBox » About Me » My Resume »Twitter

Recently Marco Shaw posted a little nntp-stats script in the microsoft.public.windows.powershell newsgroup that I thought was pretty neato, and right up my alley since we were in the middle of adding get-nntp and send-nntp cmdlets to the /n software NetCmdlets product. So as a tip of the hat to Marco, here is my version.

Poster Stats:

Get-nntp returns a collection of objects containing the subject, articleid, sender, references, etc of articles posted in a particular newsgroup. I put together a few lines of code that uses get-nntp to create a hashtable of people who have posted in the PowerShell newsgroup and how many times.


  ## 
## usage: nntpstats [-server] <string> [-newsgroup] <string> [-count] <int> ## param( [int] $count = "100", [string] $server = "msnews.microsoft.com", [string] $newsgroup = "microsoft.public.windows.powershell" )
$articles = get-nntp -server $server -newsgroup $newsgroup -list $count $myhash = @{} for($counter = 0; $counter -lt $articles.Length; $counter++) { if (! $myhash.Contains($articles[$counter].FromName)) { $myhash.add($articles[$counter].FromName, 1) } else { $myhash[$articles[$counter].FromName] = $myhash[$articles[$counter].FromName] + 1 } }
$myhash.GetEnumerator() | sort Value -descending


I'll give this a run with a count parameter value of 2 so that I can easily show the actual article data returned by get-nntp itself. Below you can see the output of my call to the nntpstats script with count = 2, just two posters, each with 1 post.

PS C:\> . .\nntpstats -count 2                         
                                                                                                                        
Name                           Value                                                                                    
----                           -----                                                                                    
Marco Shaw                     1                                                                                        
Sung M Ki                      1                                                                                        

Now here is the $articles object that was returned by get-nntp:

PS C:\> $articles                                       
                                                                                                                        
                                                                                                                        
Number       : 7840                                                                                                     
Subject      : RE: adding function return to an array                                                                   
FromName     : Sung M Ki                                                                                                
FromEmail    : SungMKim@[DELETED]                                                                       
Date         : Wed, 24 Jan 2007 11:30:01 -0800                                                                          
Size         : 1184                                                                                                     
References   : <F91D95BB-5815-4BB5-AD05-FA6EDA241816@microsoft.com>                                                     
MessageID    : <5878669D-E0CB-42C0-B12D-80793CCB97FE@microsoft.com>                                                     
Otherheaders : Xref: TK2MSFTNGP01.phx.gbl microsoft.public.windows.powershell:7840                                      
                                                                                                                        
Number       : 7841                                                                                                     
Subject      : Info: 3rd party cmdlets updated                                                                          
FromName     : Marco Shaw                                                                                               
FromEmail    : marcoDOTshaw_@[DELETED]                                                                               
Date         : Wed, 24 Jan 2007 16:09:12 -0400                                                                          
Size         : 1118                                                                                                     
References   :                                                                                                          
MessageID    : <eJB0FO$PHHA.3268@TK2MSFTNGP03.phx.gbl>                                                                  
Otherheaders : Xref: TK2MSFTNGP01.phx.gbl microsoft.public.windows.powershell:7841                                      
                                                                                                                        

Incidentally, you could create a link to any of these articles in Google Groups by appending the url-encoded messageid (without the angle brackets) to http://groups.google.com/groups?threadm=.

Now here's the output of the nntpstats script after retrieving the last 100 articles from the PowerShell newsgroup:

PS C:\> . .\nntpstats -count 100                       
                                                                                                                        
Name                           Value                                                                                    
----                           -----                                                                                    
Brandon Shell                  11                                                                                       
/\\/\\o\\/\\/ [MVP]            10                                                                                       
jhicks[at]SAPIEN.com           9                                                                                        
Keith Hill [MVP]               8                                                                                        
Oisin Grehan                   8                                                                                        
Andrew Watt [MVP]              7                                                                                        
Ric                            6                                                                                        
$hay                           6                                                                                        
Marco Shaw                     4                                                                                        
IT Staff                       3                                                                                        
Sung M Ki                      3                                                                                        
/\/\o\/\/ [M                   3                                                                                        
joergH                         2                                                                                        
Rolf                           2                                                                                        
                               2                                                                                        
Krishna Vutukuri[MSFT]         2                                                                                        
Nit                            1                                                                                        
Bomber                         1                                                                                        
cthist                         1                                                                                        
Rom                            1                                                                                        
Adam Gera                      1                                                                                        
Paul Hinsber                   1                                                                                        
Bil                            1                                                                                        
Rob Campb                      1                                                                                        
Bombergio                      1                                                                                        
red_deer_hil                   1                                                                                        
                                                                                                                        
                                                                                                                        
PS C:\>

Search:

I can tweak this script to give me a list of messages that match a particular search criteria. Unfortunately most NNTP servers don't implement the search capabilities that are specified in the NNTP RFC's, so implementing such a search requires retrieving and searching article bodies manually:


##
## usage: nntpsearch [-server] <string> [-newsgroup] <string> [-count] <int> [-searchterm] <string> ## param( [int] $count = "100", [string] $server = "msnews.microsoft.com", [string] $newsgroup = "microsoft.public.windows.powershell", [string] $searchterm = "Lance" )
$articles = get-nntp -server $server -newsgroup $newsgroup -list $count for($counter = 0; $counter -lt $articles.Length; $counter++) { $article = get-nntp -server $server -newsgroup $newsgroup -view $articles[$counter].Number if ($article.Message.Contains($searchterm) -eq "true") { Write-Host $article.MessageID } }

Posted on Wednesday, January 24, 2007 5:56 PM PowerShell | Back to top


Comments on this post: PowerShell and NNTP

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Lance Robinson | Powered by: GeeksWithBlogs.net