In my previous post, Fun With PowerShell – Opening Websites with Start-Process, I showed how to use the Start-Process cmdlet to open a website. This is part of my ongong ArcaneBooks Project, in which I created a new function to display the webpage for a book at the OpenLibrary website by using the ISBN.
I wanted to create a similar function to work with the Library of Congress website, and so let me present the Show-LCCNBookData function.
Show-LCCNBookData
The function I created, Show-LCCNBookData is almost identical to the Show-ISBNBookData function I covered in the previous post, so I won’t go into a lot of depth in this post.
As with the ISBN version, I made this an advanced function so users could pipe data into it.
function Show-LCCNBookData
{
[CmdletBinding(HelpURI="https://github.com/arcanecode/ArcaneBooks/blob/1ebe781951f1a7fdf19bb6731487a74fa12ad08b/ArcaneBooks/Help/Get-ISBNBookData.md")]
[alias("slccn")]
param (
[Parameter( Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = 'Please enter the LCCN (Library of Congress Control Number).'
)]
[string] $LCCN
)
Note I still need to update the help URL to the correct one, but the rest of the function opening is complete, with the sole parameter being the $LCCN.
Now we fall into the process block.
process
{
foreach($number in $LCCN)
{
Write-Verbose "Beginning Show-LCCNBookData for $ISBN at $(Get-Date).ToString('yyyy-MM-dd hh:mm:ss tt')"
$lccnCleaned = $LCCN.Replace('-', '').Replace(' ', '')
$lccnPrefix = $lccnCleaned.Substring(0,2)
$lccnPadded = $lccnCleaned.Substring(2).PadLeft(6, '0')
# Now combine the reformatted LCCN and save it as a property
$lccnFormatted ="$($lccnPrefix)$($lccnPadded)"
$baseURL = "https://lccn.loc.gov/"
$url = "$($baseURL)$($lccnFormatted)"
Write-Verbose 'Opening the Book on Library of Congress Number'
Start-Process $url
Write-Verbose "Finished Getting Data for $($LCCN)"
}
Write-Verbose "Done opening the web pages at Library of Congress"
}
When we fall into the process loop we first need to clean up the LCCN that was passed in. As was documented in my LCCN overview post the LCCN is the two digit year at front, then six digits. If the number of digits after the first two isn’t six in length we have to zero pad it to become six, which will make the entire LCCN string eight digits.
We then append the formatted LCCN to the base URL for the LOC website. Then we use the Start-Process cmdlet to open the webpage.
Calling Show-LCCNBookData
Calling the function is pretty easy, you can either pass in a Library of Congress Control Number as a parameter or via the pipeline. All these examples should open the Library of Congress website, in your default browser, with the book associated with the LCCN you passed in.
# Pass in a single LCCN as a parameter
$LCCN = '54009698'
Show-LCCNBookData -LCCN $LCCN -Verbose
# Alias
$LCCN = '54009698'
slccn -LCCN $LCCN -Verbose
# Pipe in a single ISBN
$LCCN = '54-9698'
$LCCN | Show-LCCNBookData
.EXAMPLE
# Pipe in an array of LCCNs
$LCCNs = @( '54-9698'
, '40-33904'
, '41-3345'
, '64-20875'
, '74-75450'
, '76-190590'
, '71-120473'
)
$LCCNs | Show-LCCNBookData -Verbose
In the final example we can actually pipe in an array of LCCNs, it should open up a page for each one.
Note the Library of Congress isn’t perfect, sometimes it will bring up a page with multiple items for the number passed in as it may have multiple entries. It’s still faster though than having to do manual searches on the LoC website.
See Also
You may find more helpful information at the links below.
ArcaneBooks Project Introduction
ArcaneBooks – Library of Congress Control Number (LCCN) – An Overview
Fun With PowerShell – Advanced Functions
Fun With PowerShell – Opening Websites with Start-Process
Fun With PowerShell – Write-Verbose
Conclusion
This post and the previous one demonstrates how easy it can be to create helper functions for your modules. My two show functions are designed to let users quickly bring up the webpage for the books they are working with.
If you like PowerShell, you might enjoy some of my Pluralsight courses. PowerShell 7 Quick Start for Developers on Linux, macOS and Windows is one of many PowerShell courses I have on Pluralsight. All of my courses are linked on my About Me page.
If you don’t have a Pluralsight subscription, just go to my list of courses on Pluralsight . At the top is a Try For Free button you can use to get a free 10 day subscription to Pluralsight, with which you can watch my courses, or any other course on the site.