pondělí 1. března 2010

How to check (find) the newest online documentation

Last week I was playing with online documentation for PowerShell cmdlets. If you are not aware - if you specify -Online parameter for Get-Help cmdlet you are redirected to the most recent version located at Microsoft TechNet site. For example:

PS C:\> Get-Help Add-Computer -Online


redirects you to http://technet.microsoft.com/en-us/library/dd347556.aspx where you can check latest version of help.

I wanted to check latest version for cmdlets provided with PowerShell but I didn't want to go cmdlet by cmdlet and manually checked date of last update. As you know - everything in PowerShell is an object (incl. documentation) so I switched to the console and was looking for property storing link to online help. It was pretty easy and shortly I found right way. So after first check I received following results:

PS C:\> Get-Help Add-* |% {$_.relatedLinks.navigationLink} |? {$_.uri}

linkText uri
-------- ---
Online version: http://go.microsoft.com/fwlink/?LinkID=113279
Online version: http://go.microsoft.com/fwlink/?LinkID=113281
Online version: http://go.microsoft.com/fwlink/?LinkID=113280
Online version: http://go.microsoft.com/fwlink/?LinkID=135195
Online version: http://go.microsoft.com/fwlink/?LinkID=113278
Online version: http://go.microsoft.com/fwlink/?LinkID=135194


Then I was able to do quick check of date just by using my Get-WebPage function which uses DownloadString method of Net.WebClient class.

PS C:\> ... | Get-WebPage |% {$_ -match "</p>Updated:\s(?<date>.*?)</p>" | Out-Null ; $Matches.date }

May 20, 2009
October 21, 2009
October 21, 2009
February 17, 2010
February 17, 2010
February 17, 2010
February 17, 2010
February 17, 2010


Cool. But ... as you can see I have dates but no idea which help file to check :( So I decided to create short function (filter) which creates object for info I needed. I decided to store name of the cmdlet, web link and date of update. You can check Get-WebPage and New-TempObj (strange name - I know - but I wanted results quickly).

function Get-WebPage {

[Cmdletbinding()]

param (
[Parameter(Mandatory
=$true,
Position
=0,
ValueFromPipeline
=$true,
HelpMessage
="Enter URL to download")]
[
string]
$Uri
)

BEGIN {
$wc = New-Object System.Net.WebClient
$proxy = [System.Net.WebProxy]::GetDefaultProxy()
$proxy.UseDefaultCredentials = $true
$wc.proxy = $proxy
}

PROCESS {
$wc.DownloadString($Uri)
}

END { }

}

filter New-TempObj {

$hash = @{
name
= $($_.Name)
uri
= $($_.relatedLinks.navigationLink |? {$_.linkText -eq 'Online version:'}).uri
}
[
datetime]$hash.date = Get-WebPage $hash.uri |% {$_ -match "<p>Updated:\s(?<date>.*?)</p>" | Out-Null; $Matches.date}

$myObj = New-Object PSObject -Property $hash
$myObj
}

Now I am able to do following:

PS C:\> Get-Help Add-* | New-TempObj | ft -AutoSize

date name uri
---- ---- ---
20.5.2009 0:00:00 Add-History http://go.microsoft.com/fwlink/?LinkID=113279
20.5.2009 0:00:00 Add-PSSnapin http://go.microsoft.com/fwlink/?LinkID=113281
16.12.2009 0:00:00 Add-Member http://go.microsoft.com/fwlink/?LinkID=113280
20.5.2009 0:00:00 Add-Type http://go.microsoft.com/fwlink/?LinkID=135195
20.5.2009 0:00:00 Add-Content http://go.microsoft.com/fwlink/?LinkID=113278
17.6.2009 0:00:00 Add-Computer http://go.microsoft.com/fwlink/?LinkID=135194


Or - even better - all help files changed in 2010:

PS C:\> get-help *-* | New-TempObj |? { $_.date.Year -eq 2010 } | Select date, name

date name
---- ----
17.2.2010 0:00:00 Get-Counter
17.2.2010 0:00:00 Import-Counter
17.2.2010 0:00:00 Export-Counter
17.2.2010 0:00:00 Start-Job
20.1.2010 0:00:00 New-Module
17.2.2010 0:00:00 Get-Module
17.2.2010 0:00:00 Out-File
17.2.2010 0:00:00 Read-Host
17.2.2010 0:00:00 Export-Clixml
17.2.2010 0:00:00 Select-XML
17.2.2010 0:00:00 Join-Path
20.1.2010 0:00:00 Get-Location
20.1.2010 0:00:00 Set-Location
20.1.2010 0:00:00 Push-Location
20.1.2010 0:00:00 Pop-Location
20.1.2010 0:00:00 New-Item


So now I am able to read latest documentation everytime without any special manual work.

Note: As I said - whole "script" was created in a hurry and therefore it's not the best one I created. You can receive exceptions for some help files but it is not the case for standard cmdlets.

2 komentáře:

stej řekl(a)...

Pěkné. Skoro mám pokušení dát si to do schedulovaných tasků a dělat si diffy a mrkat, kde se co změnilo :) Pěkněs to vymyslel.

$hay@Israel řekl(a)...

Another way to get the help url :)

Get-Command -Verb Add | Where-Object {$_.HelpUri} | Select-Object Name,HelpUri


And if you don't want to mess with string parsing:

$request = [System.Net.WebRequest]::Create($url)
$LastModified = $request.GetResponse().LastModified
$LastModified