Author Posts

March 2, 2015 at 7:18 am

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?

March 2, 2015 at 7:44 am

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

March 10, 2015 at 7:30 am

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.