Posting to Bluesky via PowerShell on macOS
The week of PASS Data Community Summit 2024 (November 4-8), Bluesky seemed to reach critical mass with the data community as well as the tech community writ large. Over the week, I saw a few posts (“skeets” I guess?) from Jeff Hicks saying that he was working on a PowerShell module for Bluesky. I said to myself “interesting, I’ll keep an eye on that.”
On the evening of November 11th, Jeff posted that he needed help troubleshooting an apparent issue on macOS.
Is anyone using the PSBlueSky #PowerShell module on a Mac and able to post a message from a PowerShell prompt without error? I’m troubleshooting an issue.
I’ve got a Mac. I’ve got PowerShell. Let’s go! After a bit of tinkering (mostly getting the credentials right), I was able to post successfully. Yay!
But a note in Jeff’s included help PDF got me thinking about the next step.
For automation purposes, you can use the Secrets management module to store your credential. Write your own code to retrieve the credential and pass it to the module commands.
Secrets management is something that I’ve been meaning to get my arms around and, where possible, make use of. Haven’t needed it much at work oddly enough, and I’m not 100% certain that our access management platform has everything enabled that I’d need. At home, I use 1Password. It turns out that there is a 1Password module for PowerShell, so I installed it and tried to get to work.
PS /Users/andy> Invoke-1PasswordExpression "get item psbsky"
Invoke-1PasswordExpression: The 'Invoke-1PasswordExpression' command was found in the module '1Pwd', but the module could not be loaded. For more information, run 'Import-Module 1Pwd'.
PS /Users/andy> import-module 1pwd
Write-Error: 1Password CLI not found in the current path. Download the 1Password CLI executable 'op.exe' and put it in the same directory as your script(s).
Huh. OK, let’s get that installed via Homebrew:
andy@Andys-MacBook-Air flxsql % brew install 1password-cli
Same result. Take a closer look at that Import-Module
error - it’s looking for a Windows executable. I checked the GitHub repository and even cloned the repository - yep, it’s only going to work in Windows, and it’s expecting to find the 1Password executable in the path you’re currently working in. I’d like to see about addressing both of those, but not today. For now, I’ll just roll my own code to pull the app password I created for PowerShell (called psbsky
in my vault), then get on with setting up the credential so I can talk to Bluesky.
$BlueSkyOnePassword = op item get psbsky --cache --format=json --fields label=username,label=password | ConvertFrom-Json|Sort-Object -Property id -Descending;
$BlueSkyUsername = $BlueSkyOnePassword[0].value;
$BlueSkyPassword = $BlueSkyOnePassword[1].value | ConvertTo-SecureString -AsPlainText -Force;
# Null out the plaintext password now that we have a SecureString
$BlueSkyOnePassword = $null;
$BskyCredential = [PSCredential]::New($BlueSkyUsername, $BlueSkyPassword);
Excellent! Now I can go ahead and use Jeff’s module.
$PSDefaultParameterValues['*-Bsky*:Credential'] = $BskyCredential
New-BskyPost -Message "Testing posts from @jdhitsolutions.com PSBsky module" -Verbose
Jeff’s module has a number of other functions you can use to explore and interact with your Bluesky account. Check it out!