Script Module and existing PS remote sessions

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Juli Juli 1 year, 9 months ago.

  • Author
    Posts
  • #22924
    Profile photo of Juli
    Juli
    Participant

    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
    Profile photo of Sam Boutros
    Sam Boutros
    Participant

    try:

    $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
    Profile photo of Juli
    Juli
    Participant

    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.

You must be logged in to reply to this topic.