Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get winrm to use powershell 7 for remote sessions by default

With the release of powershell 7, seems like it is time to move past ps 5.1 so I have installed in on a couple of servers to give it a go.

However I when I create a session to these servers from my pc with ps7 I am always running ps5.1 on the remote machine.

Invoke-Command -ComputerName name -ScriptBlock { 
        Write-Host $env:COMPUTERNAME
        $PSVersionTable.PsVersion
    }

Which outputs 5.1.17763.316. Any ideas how to get the remote session to use version 7.0.0 preferably by default?

Update making some progress with this, so though I would share.

On the remote machine in powershell 7 run the following command

Enable-PSRemoting

This will create some PsSessionConfigurations which you can see with the following command..

Get-PSSessionConfiguration

Now you can do the following to create sessions from powershell 7

Invoke-Command -ComputerName ServerName -ScriptBlock { $PsVersionTable.PSVersion } -ConfigurationName Powershell.7
$session = New-PSSession ServerName -ConfigurationName Powershell.7
Invoke-Command -Session $session -ScriptBlock { $PsVersionTable.PSVersion } 

This now uses ps 7 on the remote session, happy days. Now how to make this happen by default...? From this github issue :

set the default microsoft.powershell endpoint to any PowerShell they choose

Which I think is what I want to do so switched back to ps 5.1 and tried this command:

Get-PSSessionConfiguration -Name microsoft.powershell | Set-PSSessionConfiguration -PSVersion 7.0

Only to get the following output:

Set-PSSessionConfiguration : Cannot bind parameter 'PSVersion' to the target. Exception setting "PSVersion": "The value 7.0 is not valid for the PSVersion parameter. The available values are 2.0, 3.0, 4.0, 5.0, 5.1."

though I would try this in ps7 so switched back by running pwsh and ran the same command again to get he following...

Write-Error: No session configuration matches criteria "microsoft.powershell".

So still not quite sure how to make ps7 the default... :(

like image 694
Alexis Coles Avatar asked Mar 06 '20 10:03

Alexis Coles


1 Answers

Note:

  • It is the remoting client that determines what remoting endpoint (session configuration) to connect to on the server machine - see below.

  • Therefore, your own attempt,

# WRONG
Get-PSSessionConfiguration -Name microsoft.powershell | 
  Set-PSSessionConfiguration -PSVersion 7.0

is ineffective, because Set-PSSessionConfiguration modifies endpoint configurations on the server machine, it doesn't control the client's behavior.

Note that the fundamental prerequisite is that PowerShell remoting must be enabled on the server machine, which can be achieved either by opting to do so during installation via the MSI GUI installer, or by running Enable-PSRemoting - with admin privileges - later.Tip of the hat to Lars Fosdal.

  • Doing so from PowerShell (Core) creates the standard session configuration(s) named PowerShell.<version> that clients can opt to connect to - see below.

  • To list all configurations defined on a server, run Get-PSSessionConfiguration with admin privileges.


On a client machine, you can set a default for what session configuration defined on the server (remote machine) to connect to, via the $PSSessionConfigurationName preference variable.

E.g., to target PowerShell 7 by default:

# When remoting, default to running PowerShell Core v7.x on the
# the target machines:
$PSSessionConfigurationName = 'PowerShell.7'

If you add the above to your $PROFILE file, future sessions will target PowerShell 7 by default.

See this answer for more information, which also shows how to target a given server configuration in the context of individual commands.


Note: Changing what endpoint PowerShell [Core] targets by default - which as of 7.2 is still Window PowerShell - is being considered: see GitHub issue #11616.

like image 64
mklement0 Avatar answered Sep 27 '22 21:09

mklement0