5
votes

Prior to PowerShell 6.0 to elevate your session you ran the command

Start-Process powershell -Verb runAs

When trying to run the similar command in PowerShell 6.0

Start-Process pwsh -Verb runAs

You get this output:

Start-Process : The parameter '-Verb' is not supported for the cmdlet 'Start-Process' on this edition of PowerShell.
At line:1 char:1
+ Start-Process pwsh -Verb runAs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotImplemented: (:) [Start-Process], NotSupportedException
+ FullyQualifiedErrorId : NotSupportedException,Microsoft.PowerShell.Commands.StartProcessCommand

So how do I elevate to run as administrator in PowerShell 6.0?

I am running the PowerShell Windows Nano Server docker image (microsoft/powershell:nanoserver)

2
I am running a Nano Server container image, microsoft/powershell:nanoserver,built and maintained by the PowerShell team. Nano Server does not include the Full .NET Framework only .NET Core and thus only PowerShell Core will run on it.Joshua
I thought that pwsh 6 was only .NET Core. This works on 6.0.1 on Windows 10.lit
Excerpt from the PowerShell Core documentation: "PowerShell Core uses .NET Core 2.0 as its runtime. .NET Core 2.0 enables PowerShell Core to work on multiple platforms (Windows, macOS, and Linux). PowerShell Core also exposes the API set offered by .NET Core 2.0 to be used in PowerShell cmdlets and scripts."Joshua
It may be an insignificant point, but this process does not elevate "your" session. It creates a new shell session as Administrator.lit

2 Answers

5
votes

What I was wanting to accomplish when I posted my question was to add certificates to the Windows certificate store using the following command:

certoc.exe -addstore root corporaterootssl.cer

The message I was receiving when executing this command in the Windows Nano Server container was access denied. This was because I was running the container with the standard user ContainerUser using the command:

docker run -d  microsoft/powershell:nanoserver

I would connect to the container with the command:

docker exec -it  gracious_ramanujan pwsh

This would put me in the container running as ContainerUser. To enter the container as ContainerAdministrator I needed to run the command:

docker exec -it --user ContainerAdministrator  gracious_ramanujan pwsh

Then I was able to successfully run any administrative commands.

My ultimate goal was to build a container image during which I needed to run one or more commands as an administrator. To switch users in your Dockerfile you use the command USER ContainerAdministrator. I wrote a blog post detailing how to add SSL certificates to your image during your Docker build process.

-1
votes

My answer was wrong and doesn't suit OP's question. Nonetheless, I'll explain why it's wrong instead of deleting the answer.

Your answer Start-Process pwsh -Verb runAs works as intended in PowerShell 7.

However, the answer is wrong because this does not elevate the current shell, but opens a new one. I have not verified if it works too in PowerShell 6. It seems OP's problem was specific to docker and not PowerShell so this could work in PowerShell 6 too.