How to Import a Custom PowerShell Module Into a Dockerfile πŸ‘¨β€πŸ³

If you want to use a custom PowerShell module in a Docker container, the command to import it correctly is simple when you know it! πŸ˜‹

2 min read
How to Import a Custom PowerShell Module Into a Dockerfile πŸ‘¨β€πŸ³
Photo by Magda Ehlers on Pexels.
πŸ₯°
This post wouldn't have been possible without Jesse Chisholm, who last month gave me the solution below. Thanks Jesse! ❀

In one of my previous posts on linting PowerShell scripts, I wasn't impressed with how GitLab CI displays PSScriptAnalyzer logs. I ended my post by asking my readers if they know how we could improve the readability of logs and one reader, Jesse Chisholm, suggested I try the script below.

function Show-LintMessages()
{
  $output = ""
  $input|ForEach-Object -Process {
    $I = $_;
    $output += ($I | Select RuleName,Severity,ScriptName,Line | Format-Table | Out-String);
    $output += ($I | Select Message | Format-Table | Out-String);
    }
  $output | Out-Host;
}

# Demonstration of function
Invoke-ScriptAnalyzer -ScriptDefinition '"b" = "b"; function eliminate-file (){}' | Show-LintMessages
The script that Jesse gave me to display the results in a prettier way.

After trying it, it was exactly what I was looking for! 🀩

But, since I was using PSScriptAnalyzer with Docker, I didn't want to declare this function every time I called the analyzer... So I started looking for how to make it available in my Docker container. πŸ€”

After a while, I finally managed to install the function and make it available in the terminal with a PowerShell module. By adding it to the /usr/local/share/powershell/Modules folder, the module is automatically imported and every user who connects to the Docker container has access to the Show-LintMessages function! πŸ€—

COPY Show-LintMessages.psm1 /usr/local/share/powershell/Modules/Show-LintMessages/Show-LintMessages.psm1
The Docker instruction to add a custom PowerShell module into a Docker container.

In GitLab CI, I also had to pipe the output of PSScriptAnalyser to the new command.

 - pwsh -Command "Invoke-ScriptAnalyzer -EnableExit -Recurse -Path . | Show-LintMessages"
The Show-LintMessages function will format the output of the previous command.

Unfortunately, the message was truncated in the GitLab CI job output... πŸ˜“

'Select' is an alias of 'Select-Object'. Alias can introduce possible problems and make scripts hard to maintain. Pleas…
RuleName                  Severity ScriptName             Line
--------                  -------- ----------             ----
PSAvoidUsingCmdletAliases  Warning Show-LintMessages.psm1    6
Message
-------
'Select' is an alias of 'Select-Object'. Alias can introduce possible problems and make scripts hard to maintain. Pleas…
RuleName              Severity ScriptName Line
--------              -------- ---------- ----
PSAvoidUsingWriteHost  Warning test.ps1      1
Message
-------
File 'test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when ther…
The results in GitLab CI were unfortunately truncated and didn't contain blank lines...

So I tweaked the function a bit, and after a few tries, I finally ended with the script below.

function Show-LintMessages() {
  $input|ForEach-Object -Process {
    Write-Output "--------------------------------------------------------------------------------";
    $PSItem | Select-Object RuleName,Severity,ScriptName,Line | Format-Table -Wrap | Out-String -Width 80;
    Write-Output " ";
    $PSItem | Select-Object Message | Format-Table -Wrap | Out-String -Width 80;
    Write-Output "--------------------------------------------------------------------------------";
    Write-Output " ";
  }
}
The final content of the Show-LintMessages.psm1 file.

And the output is now much better in GitLab CI! πŸ˜„

--------------------------------------------------------------------------------
RuleName              Severity ScriptName Line
--------              -------- ---------- ----
PSAvoidUsingWriteHost  Warning test.ps1      1

Message
-------
File 'test.ps1' uses Write-Host. Avoid using Write-Host because it might not
work in all hosts, does not work when there is no host, and (prior to PS 5.0)
cannot be suppressed, captured, or redirected. Instead, use Write-Output,
Write-Verbose, or Write-Information.
--------------------------------------------------------------------------------
The same message, but displayed with a custom script to look better in GitLab CI.

It's more readable, isn't it? πŸ˜‰