Skip to content

Get-PSSession with -ComputerName does not work #20995

Closed

Description

@c2ho

Prerequisites

Steps to reproduce

  1. launch pwsh
  2. execute command: Get-PSSession -ComputerName dc01

This has already been stated in #6321 (and #17768?) but those were closed due to inactivity and as the bot closed those for housekeeping, I now create a new one.

Expected behavior

Id    Name    ComputerName    ComputerType    State    ConfigurationName    Availability
--    ----    ------------    ------------    -----    -----------------    ------------
1     WinRM1  dc01            RemoteMachine   Opened   Microsoft.PowerShell Available



_(copied from a Windows machine executing the same command)_

Actual behavior

PS> Get-PSSession -ComputerName dc01       
                                                                                                                                                                                                                                       
Get-PSSession: The term 'New-WSManSessionOption' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Kali GNU/Linux Rolling
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Jan 4, 2024
  2. jhoneill commented on Jan 4, 2024

    @jhoneill

    Just out of curiosity - are you trying to get your sessions to [computername]
    or a list of anybody's sessions ON [computername]
    because it appears that this is trying to create an implicit WinRM session to [computerName] run get-psSession, and return the result

    On my (windows) Machine I can connect to localhost from one PowerShell session using both wsman and ssl

    #507 [|||] PS7 ~\Documents\blah> get-pssession       
    
     Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
     -- ----            --------- ------------    ------------    -----         -----------------     ------------
      1 Runspace1       WSMan     localhost       RemoteMachine   Opened        Microsoft.PowerShell     Available
      4 Runspace3       SSH       localhost       RemoteMachine   Opened        DefaultShell             Available
    

    If I ask from another powershell window

    
    #506 [|||] PS7 ~\Documents\blah> Get-PSSession -ComputerName localhost
    
     Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
     -- ----            --------- ------------    ------------    -----         -----------------     ------------
      2 Runspace1       WSMan     localhost       RemoteMachine   Disconnected  Microsoft.PowerShell          Busy
    

    It only shows me my WSMAN Session(s) even if try specifying the configuration name.
    If I try to talk via SSL to get information on the SSL ones.
    .

    #507 [|||] PS7 ~\Documents\WindowsPowerShell\Modules\Image> Get-PSSession -ComputerName localhost -UseSSL
    Get-PSSession: The remote session query failed for localhost with the following error message: The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". 
      
    

    One of the PowerShell team did imply this wasn't by design in #6321 so the bot's closing it was automatic but seems the wrong thing to have done - it does seem to be all SSL not just SSL from Linux.

  3. rhubarb-geek-nz commented on Jan 4, 2024

    @rhubarb-geek-nz

    My understanding is that WinRM is a windows only protocol and need to use SSH on MacOS and Linux.

    According to Invoke-Command

    The benefit of an SSH based PowerShell remote session is that it works across multiple platforms (Windows, Linux, macOS).
    For SSH based session you use the HostName or SSHConnection parameters to specify the remote computer and relevant connection information. 
    

    I would expect to use ComputerName ( rather like the NetBIOS/NetBEUI/LanManager computer name) for WinRM and HostName for SSH.

  4. jhoneill commented on Jan 4, 2024

    @jhoneill

    I would expect to use ComputerName ( rather like the NetBIOS/NetBEUI/LanManager computer name) for WinRM and HostName for SSL.

    Unlike (e.g.) New-PsSession, Get-PsSession doesn't take -HostName

    invoke-command -HostName localhost -ScriptBlock {get-Pssession} runs but returns nothing (even with -computername Localhost) - some ancient memory says that sessions over SSH don't know they are remote sessions which is why you can't (yet) do JEA over SSH.

  5. jborean93 commented on Jan 4, 2024

    @jborean93
    Collaborator

    This is a difficult one, Get-PSSession is very much tied into the WSMan stack and Get-PSSession -ComputerName ... is essentially hardcoded to do a WSMan enumeration POST to retrieve the information. The WSMan implementation on non-Windows is pretty restricted and cmdlets like New-WSManSessionOption are not included anymore. Even if the cmdlet existed it's going to fail on the next step when it tries to call Get-WSManInstance which is part of the same module and is also not included on non-Windows.

    While yes this could theoretically work it's very likely it's never going to be worked on as the focus on the pwsh team is on ssh transport and not WSMan anymore. You can keep this open but be prepared for no activity to be done here, it requires some work from the pwsh team which is unlikely to happen in this area.

  6. c2ho commented on Jan 5, 2024

    @c2ho
    Author

    Just out of curiosity - are you trying to get your sessions to [computername]
    or a list of anybody's sessions ON [computername]

    Both, actually. For housekeeping reasons I want to close and/or reuse sessions that already exist and are no longer needed for their initial purpose.

    I would expect to use ComputerName ( rather like the NetBIOS/NetBEUI/LanManager computer name) for WinRM and HostName for SSL.

    As James O'Neill (@jhoneill) already pointed out this does not work as HostName isn't a valid option here.

    My understanding is that WinRM is a windows only protocol and need to use SSH on MacOS and Linux.

    As of what I found WinRM is based on the open standard WSMan which has to be installed manually inside pwsh. Doesn't it imply that if PowerShell hints that it needs those exact modules to run within a Linux-based system such calls as Get-PSSession -ComputerName $ComputerName would actually work?

    [...] the pwsh team is on ssh transport and not WSMan anymore

    Unfortunately, using the -UseSSL option also throws the error that "The term 'New-WSManSessionOption' is not recognized as a name of a cmdlet, function, script file, or executable program." - are there any special modules needed that don't come with pwsh and/or pwsh -Command -Install-Module -Name PSWSMan?

  7. jborean93 commented on Jan 5, 2024

    @jborean93
    Collaborator

    Unfortunately, using the -UseSSL option also throws the e

    -UseSSL just means use https for WSMan, SSH is a completely different thing and doesn't really have the same concept as a persistent PSSession that Get-PSSession -ComputerName foo would return. There is no real concept of a long lived session with other protocols as they can only last for the lifetime of the connection. WSMan is special because you can have disconnected sessions and the state it stored on the service rather than the PowerShell process.

    are there any special modules needed that don't c

    In this case the WSMan enumeration code is part of Microsoft.WSMan.Management which is completely Windows only. Theoretcially someone could implement the New-WSManSessionOption and Get-WSManInstance so this works but I don't think that's going to happen.

    Ultimately I think the -ComputerName parameter just needs to become Windows only in pwsh as it's not going to work on Linux without any large changes to WSMan support which the pwsh team has actively moved away from in recent years.

  8. jborean93 commented on Jan 5, 2024

    @jborean93
    Collaborator

    I've opened #21009 which emits a better error message that states -ComputerName is only for non-Windows.

  9. c2ho commented on Jan 5, 2024

    @c2ho
    Author

    I don't want to be rude but displaying that it just doesn't work seems like an odd fix when the otherwise suggested solution to use option -UseSSL instead doesn't work either.

  10. jborean93 commented on Jan 5, 2024

    @jborean93
    Collaborator

    That’s not the suggested solution. SSL is not the same as SSH. What you are wanting to do requires code that does not work on non-Windows (the module is not shipped with the cmdlets needed) and getting it to work is not a trivial thing to do. If you want to remotely view PSSessions you need to run this on Windows where the WSMan client components are present.

    The PR is there to make it clearer this parameter won’t work outside of Windows rather than the missing cmdlet error you get today.

  11. microsoft-github-policy-service commented on Dec 5, 2024

    @microsoft-github-policy-service
    Contributor

    📣 Hey c2ho, how did we do? We would love to hear your feedback with the link below! 🗣️

    🔗 https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions