Script Module and existing PS remote sessions

Welcome Forums General PowerShell Q&A Script Module and existing PS remote sessions

This topic contains 2 replies, has 2 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #22924

    Points: 35
    Rank: Member

    I am finalizing the creation of a module for disabling employees across AD, Exchange and Lync. In the script I am loading the AD module and setting up 2 remote sessions, one for Lync and one for Exchange. Since this module will need to be used by anyone who has the proper rights, I would like to be able to check for the existing Lync and Exchange remote sessions before loading them in the script. I know that some people may have already loaded them in their PowerShell profile.

    I have seen several different methods for doing this, such as... if ($RemoteExchange_LocalizedStrings) or get-pssession or even if (-not(Get-PsSession)).

    What is the recommended method of finding existing sessions in PowerShell 4?

  • #22925

    Points: 131
    Helping Hand
    Rank: Participant


    $ComputerName = 'veeambackup2' 
    $Session = Get-PSSession | where { $_.ComputerName -eq $ComputerName -and $_.State -eq 'Opened' }
    if (-not $Session) {
        try { $Session = New-PSSession -ComputerName $ComputerName -EA 1 } catch { throw }

    Replace $ComputerName value on top..
    then use the session:

    Invoke-Command -Session $Session -ScriptBlock { 
        # some commands here 

    finally, remove the session when done:

    Remove-PSSession -Session $Session
  • #23192

    Points: 35
    Rank: Member

    I am having difficulty making this work. We are using the -ConnectionUri parameter with an http://xxxxxx/powershell to open the session.

    All i need to do is verify whether an Exchange or Lync remote session exists as some people call them via their Windows Powershell profile. If they don't exist, I already have the piece in place to load them.

The topic ‘Script Module and existing PS remote sessions’ is closed to new replies.