Introduction
As part of my ArcaneBooks Project I described how to use the OpenLibrary Simple API to get book data.
In that post I also showed a way to bring up the webpage for an ISBN. I had a thought, why not build a function to add to the module to do that? This way a user would have an easy way to compare the output of the web API call to what the site holds.
In this post I’ll describe how to use the Start-Process
cmdlet to open a target webpage.
Show-ISBNBookData
I created a new advanced function and named it Show-ISBNBookData
. Here is the opening of the function.
function Show-ISBNBookData
{
[CmdletBinding(HelpURI="https://github.com/arcanecode/ArcaneBooks/blob/1ebe781951f1a7fdf19bb6731487a74fa12ad08b/ArcaneBooks/Help/Get-ISBNBookData.md")]
[alias("sisbn")]
param (
[Parameter( Mandatory = $true,
ValueFromPipeline = $true,
HelpMessage = 'Please enter the ISBN.'
)]
[string] $ISBN
)
If you want to learn more about advanced functions, see my post Fun With PowerShell – Advanced Functions. Briefly, the CmdletBinding
attribute will turn this into an advanced function. Advanced functions allow you to input one or more parameters via the pipeline.
It has one parameter, the ISBN number you want to find. This can be passed in normally, or via the pipeline.
The Process Loop
In order to process multiple items from the pipeline you must enclose the heart of the function inside a process { }
block. The process
block is called once for each item passed in via the pipeline.
I then use the Replace
method of the string object to remove any dashes or spaces from the ISBN that was passed in. This is then combined with the base OpenLibrary URL to create a new string, $url
.
process
{
foreach($number in $ISBN)
{
Write-Verbose "Beginning Show-ISBNBookData for $ISBN at $(Get-Date).ToString('yyyy-MM-dd hh:mm:ss tt')"
$isbnFormatted = $ISBN.Replace('-', '').Replace(' ', '')
$baseURL = "https://openlibrary.org/isbn/"
$url = "$($baseURL)$($isbnFormatted)"
Write-Verbose 'Opening the Book on OpenLibrary'
Start-Process $url
Write-Verbose "Finished Getting Data for $($ISBN)"
}
The magic comes in the Start-Process
cmdlet. This cmdlet analyzes the string that was passed in. It then looks for the default application for it, and attempts to open the associated application for the passed in string.
As an example, if you were to pass in the name of a Microsoft Word document, Start-Process
would open Microsoft Word with the document name you passed in.
In this case, passing in a URL will attempt to open up your default web browser to the page you passed in.
If you called Show-ISBNBookData
using the pipeline, the function will attempt to open up a new tab in your browser for each URL passed in via the pipeline.
Note I also used several Write-Verbose
commands, you can learn more about it at Fun With PowerShell – Write-Verbose.
An Example
Calling the function is very simple.
$ISBN = '0-87259-481-5'
Show-ISBNBookData -ISBN $ISBN -Verbose
This should open up the following webpage in your default browser.
https://openlibrary.org/books/OL894295M/Your_HF_digital_companion
This is a reference to the book You HF Digital Companion.
See Also
You may find more helpful information at the links below.
Fun With PowerShell – Advanced Functions
Fun With PowerShell – Write-Verbose
Conclusion
As you can see, Start-Process
is extremely easy to use. Just pass in a URL or the name of a file, and PowerShell will attempt to open the item using the default application assigned in the operating system. In the ArcaneBooks project I’m using it to open a website, but you can use it for a variety of purposes.
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.