Search and download scripts from TechNet Script Center repository

by ravikanthchaganti on July 7, 2010
220 Views

If you have ever used PoshCode, you can search & download scripts from PoshCode using PowerShell. They even have a module for that. PowerGUI provides features to search PoshCode and download/upload scripts to PoshCode from PowerGUI Script Editor. A similar script or an option does not exist for TechNet Script Center. So, this is my first attempt at that. The following module (not really, this is just version 0.001), provides the ability to search TechNet script center and download a specific script to disk.

This uses TechNet Script Center search RSS feed for searching the repository and hence returns only top 20 results.So, version 0.1 will have the “real” search capabilities. In fact, there are many things not there in this version. There is no comment based help, there is no proper error handling, and etc. All this will get fixed soon. This is just proof-of-concept and leave your feedback here on what you want to see.

This script is also available on the TechNet Script center

$searchURL = "http://gallery.technet.microsoft.com/ScriptCenter/en-us/site/feeds/searchRss?f%5B0%5D.Type=SearchText&f%5B0%5D.Value"
$webClient = New-Object System.Net.WebClient            

function Test-Proxy {
    return $webClient.Proxy.IsBypassed($searchURL)
}            

Function Search-ScriptCenter {
    Param([String]$keyword)
    $url = $("$searchURL=$keyword")            

    if (!(Test-Proxy)) {
        if (!($global:ProxyCred)) {
            $global:ProxyCred = Get-Credential
        }
        $webClient.Proxy.Credentials = $global:ProxyCred
    }            

    try {
        $results = ([xml]$webClient.DownloadString($url)).rss.channel.item
    }
    catch {
        Write-Error "Error starting a script center search"
        return
    }            

    $resultsObject = @()
    if ($results.Count -ne 0) {
        foreach ($result in $results) {
            $tmpObject = New-Object PSCustomObject
            $tmpObject | Add-Member -MemberType NoteProperty -Name "Title" -Value $result.Title
            $tmpObject | Add-Member -MemberType NoteProperty -Name "Description" -Value $result.Description
            $tmpObject | Add-Member -MemberType NoteProperty -Name "Link" -Value $result.Link
            $tmpObject | Add-Member -MemberType NoteProperty -Name "Author" -Value $result.Author
            $tmpObject | Add-Member -MemberType NoteProperty -Name "PublishedDate" -Value $([DateTime]$result.PubDate)
            $resultsObject+=$tmpObject
        }
    }
    return $resultsObject
}            

Function Get-ScriptCenterScript {
    Param([String]$uri,[String]$saveAs)
    Write-Verbose ""
    $objIExplore = New-Object -ComObject InternetExplorer.Application
    $objIExplore.Navigate($uri)            

    While ($objIExplore.Busy) {
        Start-Sleep -Seconds 5
    }            

    try {
        $docHtml = $objIExplore.Document
        if ($docHtml -eq $null) {
            Write-Error "Error"
            return
        }
        $scriptText = $docHtml.GetElementByID("ScriptCode").lastChild.InnerText
    }
    catch {
        Write-Error "There was error reading the HTML"
        $objIExplore.Quit()
        return
    }            

    if ($saveAs) {
        Try {
            Set-Content -Value $scriptText -Path $saveAs
        }
        Catch {
            Write-Error "Error copying the script to local disk"
            return $scriptText
        }
    }
}                

Export-ModuleMember -Function Search-ScriptCenter,Get-ScriptCenterScript -Variable SearchURL,WebClient

Using this is quite simple.

  • Copy the above content to a .psm1 file
  • Use Import-Module to load the .psm1 file
  • This loads two script cmdlets: Search-ScriptCenter and Get-ScriptCenterScript
#Search TechNet Script Center Repository
Search-ScriptCenter -keyword "PowerShell" | Out-GridView

Note: You will be prompted for the proxy credentials, if required.

This will show a pretty dialog as shown here

Search-ScriptCenter Output in GridView
Search-ScriptCenter Output in GridView
#Search TechNet Script Center Repository
$results = Search-ScriptCenter -keyword "PowerShell"            

$results | % {
    Write-Host $_.Link
}

The above snippet will retrieve all 20 links and display then on the console. You can now use one of the links as a value to Get-ScriptCenterScript and save the contents of the script to local disk. This can be done by

#Download a specific script
Get-ScriptCenterScript -uri "http://gallery.technet.microsoft.com/ScriptCenter/en-us/7f9e7e32-8b98-412f-a1b4-0c25512670f1" -saveAs C:\Scripts\test.ps1

This will save the script contents to C:\scripts\test.ps1. -SaveAs parameter is optional. If you don’t specify that, the script will just return the script contents to console or you can choose to assign to another variable.

This is it for starters. There is more coming. May be a good looking WPF addon for ISE/PGSE.

Related Posts

{ 2 trackbacks }

Leave a Comment

Previous post:

Next post: