How can you configure to remote connect to PS 7?

Hi all,
I have a Windows 10 system that I installed PS 7 on. I am connecting to that system remotely using enter-pssession. I have a script that runs gets system uptime using Get-Uptime.

The problem is Get-Uptime wasnt in PS5, and everytime I connect with enter-pssession or invoke-command, i get the ps5 console, and it doesnt know Get-Uptime. Is there a way to connect to the PS 7 console ?

Thanks!

What is Get-Uptime? If I’m not wrong it’s not a default function or cmdlet of either version 5.1 or version 7. I’d assume it is a local function you want to call remotely, right? :wink:
This has nothing to do with connecting remotely to Windows PowerShell or PowerShell core.
I’d recommend to use Get-CimInstance to query remote mashines. If you want to query more than just one CIM clas you may use a common CIM session for all queries to reduce the connections and speed up your code.

In quick n’ dirty it would look something like this

$OS = Get-CimInstance -ClassName CIM_OperatingSystem  -ComputerName '<RemoteComputerName>'
$Uptime = (Get-Date) - $OS.LastBootUpTime
$Uptime

Thanks!

This was where I saw get uptime:

Get-Uptime (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

I use it in a script I wrote like this:

#Get uptime of local or remote
function Get-MKUptime {
    param(
        [Parameter(valuefrompipeline)]
        $computername
    )

    if ($null -eq $computername) {
        $timeup = (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property LastBootUpTime )
        $hostname = hostname
        $up = Get-Uptime
        Write-Host 'Last boot time: '$timeup.LastBootUpTime -ForegroundColor Red
        Write-Host $hostname 'has been up for'$up.days 'days, '$up.hours'hours, '$up.Minutes'minutes'$up.Seconds'seconds' -ForegroundColor Yellow


    }
    else {
        Invoke-Command -ComputerName $computername {
            $timeup = (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property LastBootUpTime )
            $hostname = hostname
            $up = Get-Uptime
            Write-Host 'Last boot time: '$timeup.LastBootUpTime -ForegroundColor Red
            Write-Host $hostname 'has been up for'$up.days 'days, '$up.hours'hours, '$up.Minutes'minutes'$up.Seconds'seconds' -ForegroundColor Yellow
        } 
    }
}

so I am using Get-CimInstance, but I want to use Get-Uptime also :slight_smile:

One more thing,
When I Enter-Pssession on the remote computer, and check $PSVersionTable, its 5.1. I can run Windows Terminal on that system using PS7, and Get-Uptime runs then, so I was hoping to be able to somehow connect like that.

Thanks.

I updated my answer above with a snippet to show how to get the uptime when you already have the LastBootupTime. So you don’t need another function. :man_shrugging:t3:

You actually don’t need to use Invoke-Command for CIM cmdlets. They’re all capable of implicit remoting using either the parameter -ComputerName or -CimSession

You connect to a remote computer and start PWSH inside this remote connection? Really?
But anyway … you don’t need that.

How did you define that function on the remote system?

While I’m sure that’s possible I never tried that and don’t know how that works, sorry. :man_shrugging:t3:

Who ever said I did that? What I actually said was I can run Windows Terminal on that system using PS7…implying LOCALLY??? Sorry if that was over your head!

so what we are saying is…even though that system has Get-Uptime as I can run it when i am sitting at the console, you cant run it against that system remotely? Interesting.

Thanks!

PS Olaf,
Your code works great in my script, thanks again.

But Im really curious about the running of PS7 commandlets like that Get-Uptime remotely. I find it weird that you cant do that?

You have to specify the configuration you want to connect to. If it’s not available, run Enable-PSRemoting in the version of PowerShell you want to connect to on the remote machine first.

PS E:\Temp> $session = New-PSSession -ComputerName localhost -ConfigurationName PowerShell.7
PS E:\Temp> Enter-PSSession $session

[localhost]: PS E:\> $PSVersionTable | Select PSVersion

PSVersion
---------
7.3.9

[localhost]: PS E:\> Get-Uptime | Select-Object Days

Days
----
   2

1 Like

So, I misunderstood this:

Sorry. :man_shrugging:t3: :love_you_gesture:t3:

I actually was not aware of it until now that Get-Uptime is a built in cmdlet of PowerShell version 7.x. Since I use a self written function with the same name for about 10 years I never noticed that MSFT actually included that in version 7. :face_with_hand_over_mouth::wink::man_shrugging:t3:

Thanks Guys.

Maybe I have a different issue, but when I enter-pssession on that machine and run Get-PSSessionConfiguration, I get this:

Get-PSSessionConfiguration


Name          : microsoft.powershell
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : microsoft.powershell.workflow
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : microsoft.powershell32
PSVersion     : 5.1
StartupScript :
RunAsUser     :
Permission    : NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed

Name          : PowerShell7
PSVersion     : 5.1
startupscript : import-module C:\Program Files\PowerShell\7\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1
RunAsUser     :
Permission    : NT AUTHORITY\INTERACTIVE AccessAllowed, BUILTIN\Administrators AccessAllowed, BUILTIN\Remote Management Users AccessAllowed



And trying to connect:

$session = New-PSSession -ComputerName sk-1st -ConfigurationName PowerShell.7
New-PSSession: [sk-1st] Connecting to remote server sk-1st failed with the following error message : The WS-Management service cannot process the request. Cannot find the PowerShell.7 session configuration in the WSMan: drive on the sk-1st computer. For more information, see the about_Remote_Troubleshooting Help topic.

any ideas on why that system doesn’t show the PSVersion as 7 for the PowerShell7?

Did you run Enable-PSRemoting in version 7?

That was it matt-bloomfield, great, Thanks much!

I had to run it as administrator in the Windows Terminal window. and then using -ConfigurationName PowerShell.7.3.9 to connect works.

Good times! :slight_smile: