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.