Monday, August 19, 2019

Powershell 7 on Ubuntu, the Weird Way?

So, I work in Powershell for most everything, in a 99.999% Windows environment, but recently gained access to spin up development servers. Lo and behold, there were Linux servers available. I am a Debian/Ubuntu guy, so I was able to get a Run Level 3 Ubuntu server up in no time. My intention is to test Powershell 7 with my Windows DEV environment, but naturally, there were some hurdles.

First, I try to get Powershell 7, per the instruction at Microsoft.

# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# nothing happens

# Update the list of products
sudo apt-get update

#dpkg: error processing archive packages-microsoft-prod.deb (--install):
# cannot access archive: No such file or directory
#Errors were encountered while processing:
# packages-microsoft-prod.deb

# Install PowerShell
sudo apt-get install -y powershell

#Reading package lists... Done
#Building dependency tree
#Reading state information... Done
#E: Unable to locate package powershell

# Start PowerShell
pwsh

#No command 'pwsh' found, did you mean:
# Command 'posh' from package 'posh' (universe)
# Command 'push' from package 'heimdal-clients' (universe)
# Command 'pdsh' from package 'pdsh' (universe)
# Command 'ppsh' from package 'ppsh' (universe)

#pwsh: command not found

Clearly that did not work. Sadly, wget and/or packages.microsoft.com are blocked from my new server. wget exists, but there is some conflict. Hmm.

wget -S --spider https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
Spider mode enabled. Check if remote file exists.
--2019-08-19 14:40:34--  https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
Resolving packages.microsoft.com (packages.microsoft.com)... 40.76.35.62
Connecting to packages.microsoft.com (packages.microsoft.com)|40.76.35.62|:443... failed: Connection refused.


But wait.. I was able to download Powershell 7 for Windows, so why not just download the .deb file and do it manually? One download and an sftp session later, and here I go:

#Use the actual package name
sudo dpkg -i powershell_6.2.0-1.ubuntu.16.04_amd64.deb
sudo apt-get install -f


Well, that was easy. Hey, how come...?

$ which powershell
$ which powershell-preview
$ which pwsh


I guess I need to find it? I am a bit lazy, so:

sudo find / -name pwsh
/opt/microsoft/powershell/7-preview/pwsh


So, add that to your PATH variable:

$ PATH=$PATH:/opt/microsoft/powershell/7-preview
$ echo $PATH
/home/vdev2/bin:/home/vdev2/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/microsoft/powershell/7-preview
$ which pwsh
/opt/microsoft/powershell/7-preview/pwsh


So...

$ pwsh
PowerShell 7.0.0-preview.2
Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/dave>


That whole copyright/help section is a bit of a pain, though. Doing a quick info pwsh however, show me we can throw standard Powershell arguments at the Linux version.

$ pwsh -NoLogo
PS /home/vdev2>


So, we can change our .bashrc file to contain that switch and never see it again, and add an alias so we can use powershell, if we want:

alias pwsh='pwsh -NoLogo'
alias powershell='pwsh -NoLogo'

Then reload our profile, and we are done.


$ exec bash
$ pwsh
PS /home/vdev2>


Viola!

Monday, August 12, 2019

Powershell 5, 6, 7 -- Where am I?

So, I'm testing out Powershell Core (6.2.2) and Powershell 7 (7.0.0-preview.2) on a Windows system, and it occurred to me that I might want to know which version I am in while working. This seemed simple enough.

For more information, see the RTPSUG intro to 6.2/7.0 here.

I had already noticed that 6.2 and 7.0 do not use my 5.1 $PROFILE, so why not have the profile change the shell prompt?

function Global:prompt { "PS 5 | $PWD>" }

Add that to my 5.1 profile, and I get:

PS 5 | A:\>

I went to create a profile for 6.2 (basic instructions here), and added:

function Global:prompt { "PS 6 | $PWD>" }

And got:

PS 6 | A:\>

Nice. I then went to 7.0 and I see:

PS 6 | A:\>

Well... It seems that the way I installed 6.2 and 7.0 (or the default manner of the shell) has the two versions sharing a $PROFILE. So, we need to do this the way I should have done from the beginning. I change my two profiles to have the following:

function Global:prompt{ "PS $($PSVersionTable.PSVersion.Major)>" }

$PSVersionTable.PSVersion is a System.Version variable. So now I get the correct major version programmatically.

Although, there is an even better way to handle this...

Here are the two profile locations:

PS 5 | C:\Users\david\bin>$profile
C:\Users\david\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

PS 6 | C:\Users\david\bin>$PROFILE
C:\Users\david\Documents\PowerShell\Microsoft.PowerShell_profile.ps1


I have many functions I use, and variables defined in my 5.1 profile. Do I want to replicate everything, every time? No. So, instead, let's do this to my 6.2/7.0 profile:

$PROFILE = "C:\Users\david\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
. $PROFILE


This re-assigns my 6.2/7.0 $PROFILE value to match that of my 5.2 $PROFILE value. I then dot-source my profile, and my 5.1 profile is loaded into whichever version I use.

Friday, August 2, 2019

Raspberry Pi 4 and Luks Encrypted Disks

I recently got a Raspberry Pi 4, which is USB-C compatible. I have a 1TB external drive, which was encrypted on a Mint 19 system (which is sadly no longer mine). "Cool! I can use it with my new Pi!"

First, I am asked for my passphrase, then:

Error: pcmanfm bd_crypto_luks_open_blob called but not implemented

Hmm...

I installed Disk-Manager, thinking that was the issue, but nope. This is an encrypt/decrypt problem. I found my first clue here.
Below error appears in Lubuntu 18.04 LTS

bd_crypto_luks_open_blob called, but not implemented

when opening Luks Encrypted volume using PCManFM, the Lubuntu File Manager.

To overcome this error, open a terminal window and enter the commands below:

$ sudo apt install libblockdev-crypto2 <enter>
$ systemctl restart udisks2.service <enter>


Ok. Restart the disk, enter the passphrase  and...

Error: Luks Encrypted error storing passphrase in keyring (the name org.freedesktop.secrets was not provided by any service files)

So, then I found this semi-related post...
I can confirm that sudo apt install gnome-keyring solves the problem on KDE. Looks like the problem is with node-keytar.

Now I was able to decrypt the disk and access it. However, I do not have permissions for the contents. I don't want to make any real changes to the drive, so I decided to open in a GUI file manager with elevated privileges.

sudo pcmanfm

This is not ideal, but slightly more convenient to traverse the drive.