简体   繁体   中英

Run remote Powershell session as version 2

I'm on a server that is running Powershell Version 2:

PS C:\> $PSVersionTable

Name      Value
----      -----
...
PSVersion 2.0

I then create a new remote session to a different computer and connect to it:

$sess = New-PSSession -ComputerName {ComputerName} -Credential $credential

It returns me the result:

PS C:\> Invoke-Command -Session $sess -ScriptBlock { $PSVersionTable }

Name      Value
----      -----
...
PSVersion 3.0

However, I need Powershell to be in Version 2 for my script so I enter a session (to make it easier). I then try to get Powershell to be Version 2:

C:\> Enter-PSSession -Session $sess
[{ComputerName}]: PS C:\> Powershell -Version 2
Windows Powershell
Copyright (C) 2009 Microsoft Corporation. All rights reserverd

And then it just hangs (or at least never lets me enter anything else into the console until I Ctrl-C).

I've also tried going through the Invoke-Command:

PS C:\> Invoke-Command -Session $sess -ScriptBlock { Powershell -version 2 }

and it does the same.

I've also tried to register a PSSessionConfiguration as per here: https://technet.microsoft.com/en-us/library/hh847899.aspx

PS C:\> Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0

But I get:

Register-PSSessionConfiguration: a parameter cannot be found that matches parameter name 'PSVersion'.

Does anyone have any ideas of what I can try next?! Thanks

On what machine did you run Register-PSSessionConfiguration ?. Your computer or the "server"?

You need to make the configuration on the target server . That is what you will be running the hosted PSSessionConfiguration.

I just tried the steps in the technet article and it worked perfectly. My 2008 server remoted to my windows 7 machine running a 2.0 PSSessionConfiguration.

On target server/host:

Register-PSSessionConfiguration -Name PS2 -PSVersion 2.0

Then, on the client machine, reference the 'PS2' configuration.

$s = New-PSSession -ComputerName Server01 -ConfigurationName PS2

I take it that the following doesn't work either:

 #Requires -version 2.0

Another kluge you could try is to create a scheduled task on the target and have the task fire off your script with Powershell.exe -version 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM