Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
August 30, 2024

August 2024 PowerShell Potluck

Here we are again at the end of another month. I hope you found this month's content helpful. I am always open to suggestions on topics that you'd like to know more about. In the meantime, let's get to our monthly potluck for PowerShell snacks.

PowerShell Saturday RTP

You know I am always encouraging you to attend the annual PowerShell Summit in Bellevue, WA. I know that isn't a realistic option for some of you. In lieu of that event, I encourage you to find user group meetings to attend. Many of them are virtual these days. But even better is an in-person event and there is a great one scheduled for this fall.

The Research Triangle PowerShell User Group is one of the largest and most active groups in the US. They are hosting a PowerShell Saturday event in the Raleigh, NC, area on October 5, 2024.

The Saturday event includes 14 sessions over two tracks. But it gets even better. There is a bonus deep-dive day on Sunday. This will be a well-run event that offers a lot of bang for your buck. Registration fees are $75 for Saturday and $125 for the weekend. Breakfast and lunch are included. If you can get to the Raleigh/Durham area, I encourage you to do so. Learn more and get your tickets at https://powershellsaturdaync.com/.

5 Reasons to Use PSReadLine

I have accepted an invitation from the people at ScriptRunner to write an occasional blog post for them. My first post is up and it is about PSReadLine and reasons you should be using it every day. You can read it here.

PSReminderLite

I've mentioned this before but spend my day working from a PowerShell prompt. I have built many tools to help me be more productive. For years, I used a module I wrote called MyTickle. This module used a SQL Server instance, usually SQL Server Express, to maintain a reminder or tickle system for upcoming events and appointments. I always felt using SQL Server was overkill for this task. It also made it challenging to manage reminders from my travel laptop.

Since I wrote that module, I have started doing more with SQLite and wrote the MySQLite to provide a framework for scripting with SQLite and PowerShell. The big advantage is that now I only need a single file.

I started revising the MyTickle module to use SQLite or SQLServer, but the project quickly became cumbersome. I decided to write a completely new module with the same functionality but relying on SQLite. You can install the PSReminderLite module on PowerShell 7 from the PowerShell Gallery.

Get Reminders
figure 1
Get Reminders by Tag
figure 2

I encourage you to look at the project's README file for more information and what to expect.

Git Helpers

Another area in which I spend a lot of time is working with Git and GitHub repositories. I know enough git commands to get by and be dangerous. For tasks I do frequently, I like to write PowerShell functions to help me. Here are a few recent additions.

Open GitHub Repository

Often, when I am working on a module, I want to open the GitHub repository in my browser. This is often a multi-step process. I wrote a function to streamline this.

Function Open-GHRepo {
    [CmdletBinding()]
    [Alias('or')]
    Param()
    #Verify the folder is a git repository
    If (Test-Path .git) {
        #verify there is a remote repository
        $remote = git remote
        if ($remote) {
            $u = git remote get-url --push $remote
            Start-Process $u
        }
        else {
            Write-Host "`e[3;38;5;204mThis repository has no remote GitHub repository`e[0m"
        }
    }
    else {
        Write-Warning 'This is not a git repository'
    }
}

I need to perform a few checks because the module or folder may not be initialized as a git repository.

Not a git repository
figure 3

Or I may be using git to manage the folder but there is no online repository. This example uses the function's alias because I like to type as little as possible.

No GitHub remote
figure 4

Get Repository Default Branch

Another pesky task is determining the repository's default branch. This is especially important if you are working with a repository that has a different default branch than the traditional master. My initial function simply gave me the default branch, such as master or main. However, it is better to write a structured object to the pipeline. This function will return the repository name, default branch, and the remote URL if there is one.

Function Get-RepoDefault {
    [cmdletbinding()]
    [alias("grd")]
    Param()    if (Test-Path .git) {
        #get remote URL
        $remote = git remote show -n
        if ($Remote) {
            $u = git remote get-url --push $remote
            $Repository = $u.split('/')[-1].replace('.git', '')
            $URL = $u.replace('.git', '')
        }
        else {
            #No online repo so use the local folder name
            $Repository = Split-Path . -Leaf
            $URL = $Null
        }        #get the default branch such as main or master
        If (Test-Path refs/remotes/origin/HEAD) {
            $Default = ((git symbolic-ref --short refs/remotes/origin/HEAD) -split '/')[1]
        }
        elseif ($Remote) {
            #if HEAD not found, then resort to using gh.exe
            $Default = gh repo view --json defaultBranchRef --jq .defaultBranchRef.name
        }
        else {
            #a private git repo with no online repository
            $Default = 'Unknown'
        }        #parse the repository name
        [PSCustomObject]@{
            Repository = $Repository
            Default    = $Default
            Path       = Convert-Path .
            URL        = $URL
        }
    }
    else {
        Write-Warning 'This is not a git repository'
    }
}
Get-RepoDefault
figure 5

The function relies on the gh command-line tool to get the default branch from the remote repository. This function also tests for a git repository if there is a remote repository. If there is no remote repository, then I can't find any way to use the local git configuration to determine the default branch. I could test for either master or main. But what if the code is run in a repo with both branches? Or what if the default is something else? In this situation, the best I can do is return Unknown which is better than guessing. I can check branches manually.

Unknown default Git branch
figure 6

VSCode Pandoc Extension

Finally, the last few articles have been about using Pandoc with PowerShell. If your requirements are simple, you might find the VSCode Pandoc extension useful. Make sure you install the active extension.

vscode-pandoc
figure 7

Once installed, you can configure a few settings.

Configure the extension
figure 8

To use, open the command palette and select Pandoc Render. Then, select the output format.

Pandoc render options
figure 9

I quickly converted a Markdown document to an HTML page.

Convert Markdown to HTML
figure 10

I prefer to write custom PowerShell tooling around Pandoc, but this extension might be a good starting point for you.

Thank You

As always, thank you for supporting this newsletter. If you are a free subscriber, I hope you'll consider at least trying a premium subscription for a month or two. Premium subscribers have full access to the archive. Think about how much you can learn in a month or two. You can cancel at any time.

Subscribe now

For my US readers, enjoy the holiday weekend. Otherwise, do good things with PowerShell. I'll be back in your inbox next month.

(c) 2022-2025 JDH Information Technology Solutions, Inc. - all rights reserved
Don't miss what's next. Subscribe to Behind the PowerShell Pipeline:
Start the conversation:
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.