1

I'm trying to write a small PowerShell script which will use a specific PowerShell module on a remote machine to do some things. This module requires the use of a PowerShell Version >= to work.

I tried using Invoke-Command to execute the script on the remote machine, but I can't figure out how to use the latest PowerShell Version available on the remote target:

$SessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$Session = New-PSSession -ComputerName "TARGETHOST" -Authentication Negotiate -SessionOption $SessionOption
Invoke-Command -Session $Session -ScriptBlock {
    (Get-Host).Version
}

Will result in Version 1 used. After some tinkering I found a way to do what I want:

$SessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$Session = New-PSSession -ComputerName "TARGETHOST" -Authentication Negotiate -SessionOption $SessionOption
Invoke-Command -Session $Session -ScriptBlock{
    C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -Command {
        (Get-Host).Version
    }
}

But I don't think this is a nice solution, because I'm spawning another PS session inside the remote session. Now I'm wondering if there are nicer ways to accomplish the same thing.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Manuel
  • 11
  • 2

2 Answers2

2

Back when PowerShell was created, the original idea was that the version would be differentiated by script extension.

That idea was not implemented to preserve backward compatibility and to avoid confusion, so now only have .ps1 files (and not .ps2, .ps3, etc.).

As backwards compatibility was preserved, they didn't need to keep old versions of powershell around either, and so PowerShell v2 installed into the C:\Windows\System32\WindowsPowerShell\v1.0 directory... over the top of the original v1.0 and keeping the same directory name.

So even though you think you are running the PS v1 from that directory, you are actually running the latest Powershell version. You can confirm this by running powershell.exe from that location and checking version with $PSVersionTable

In my case:

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.15063.608
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.608
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • Thank's for your answer and explanation. The problem is, that the module I'm using only works, if I use the second method. Otherwise it will throw the following error: `This cmdlet requires PowerShell version 3.0 or greater. + CategoryInfo : InvalidOperation: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-SCXDiscovery + PSComputerName : TARGETHOST` – Manuel Oct 04 '17 at 06:36
0

I had the same issue with my Invoke-Command script, it would return version 1.0.0.0 when checking with (Get-Host).Version as well as $host.version. However $PSVersionTable.PSVersion successfully output 5.1. So I modified the script that is asking for the version to use $PSVersionTable.PSVersion and now it runs without complaining.

Sam Lewis
  • 25
  • 2
  • 7