Powershell: Get-Package: A parameter cannot be found that matches parameter name 'Scope'

Viewed 63

I am building a Nanoserver-based docker image in Docker Desktop (Windows ).

My Dockerfile looks like this:

# Nano Server - IIS

FROM mcr.microsoft.com/powershell:lts-nanoserver-20h2-20220318

SHELL ["pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

USER ContainerAdministrator

RUN [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; \
    Install-PackageProvider -Name NanoServerPackage -Force -Scope AllUsers ; \
    Get-Package -Name Microsoft-NanoServer-IIS-Package -ProviderName NanoServerPackage -Scope AllUsers -Force -Verbose \
       | Install-Package -Name Microsoft-NanoServer-IIS-Package -ProviderName NanoServerPackage -Scope AllUsers -Force -Verbose ; \
    Write-Output "done"

USER ContainerUser 

When I run docker build the RUN command fails with the error: "Get-Package: A parameter cannot be found that matches parameter name 'Scope'."

According to the Powershell docs, Get-Package does take a -Scope argument. What am I missing here?

$PSVersionTable output:

Name                           Value
----                           -----
PSVersion                      7.2.2
PSEdition                      Core
GitCommitId                    7.2.2
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0 
1 Answers

I was just struggling with the same, or at least similar (for me it's Install-Package), issue right now.

Workaround / Solution

Our workaround is to simply call the command within the powershell environment:

powershell Install-Package -Name My.Package -Source MySource -Scope CurrentUser -Force -AllowPrereleaseVersions

Elaborations & Observations

I run a few tests on our build server with our desired call (stripped to the essentials for the sake of the elaboration below, including screenshot):

Install-Package -Name My.Package -Source MySource -Scope CurrentUser -Force -AllowPrereleaseVersions
  • The call failed with pwsh (7.2.5) with the error you describe.
  • However, when I call with powershell (5.1.19041.1682), it succeeds.

Even stranger: When I start pwsh, the call fails as mentioned above. However when I switch to powershell, back to pwsh and execute the command again, it succeeds as you can verify in the screenshot: switch to powershell and back to pwsh

It is still a mystery to me and I'd be glad if someone can elaborate on this behavior.

Related