Skip to content

Commit 2ab1936

Browse files
Daniele CatanesiDaniele Catanesi
authored andcommitted
Released version 2.2.0
Updated Winscp assembly and exe to version upstream version 5.15.9 Added new function New-ExchangeConnection Added new function Close-ExchangeSession
1 parent f0d7aeb commit 2ab1936

File tree

8 files changed

+350
-277
lines changed

8 files changed

+350
-277
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# IT-ToolBox - Change History
22

3+
## Version 2.2.0.0 - 05.12.2019
4+
5+
- **New** cmdlet *New-ExchangeSession* to create a remote PowerShell session to an on-premise Exchange server [2](https://github.com/PsCustomObject/IT-ToolBox/issues/2)
6+
- **New** cmdlet *Close-ExchangeSession* to close a remote PowerShell session to an on-premise or online Exchange server
7+
38
## Version 2.1.0.0 - 02.11.2019
49

5-
- **New** cmdlet *New-ApiRequest* to create OAuth2 api requests
10+
- **New** cmdlet *New-ApiRequest* to create OAuth2 API requests
611
- **Fixed** comment based help for *New-StringEncryption* cmdlet
712
- **Fixed** comment based help for *New-StringDecryption* cmdlet
813
- **Fixed** wrong return data type for *Test-IsEmail* cmdlet

IT-ToolBox.psd1

100755100644
File mode changed.

Staging/Close-ExchangeSession.ps1 renamed to Public/Close-ExchangeSession.ps1

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,43 @@ function Close-ExchangeSession
88
Function is used to close an open session to a remote Exchange server.
99
1010
Sessions can be specified by ID or Name, by default all sessions with a ConfigurationName of Microsoft.Exchange will be returned and removed.
11+
12+
Office 365 sessions will not be removed
1113
1214
.PARAMETER SessionId
1315
An integer value representing the Sesison ID to remove.
1416
1517
.PARAMETER SessionName
1618
A string value representing the Session Name to remove.
1719
20+
.PARAMETER Online
21+
When parameter is specified function will only close any open Exchange Online session.
22+
1823
.EXAMPLE
1924
PS C:\> Close-ExchangeSession
2025
2126
.OUTPUTS
2227
System.Boolean
2328
2429
.NOTES
25-
If no SessionName or SessionId parameter is used function will close any remote PowerShell session (Skype Online, Exchange Online, Sharepoint Online etc.)
26-
#>
30+
If no SessionName or SessionId parameter is used function will close any Exchange remote session.
31+
#>
2732

2833
[CmdletBinding(DefaultParameterSetName = 'ReturnAllSessions')]
2934
param
3035
(
3136
[Parameter(ParameterSetName = 'SessionById')]
3237
[ValidateNotNullOrEmpty()]
33-
[int]$SessionId,
38+
[int]
39+
$SessionId,
3440
[Parameter(ParameterSetName = 'SessionByName')]
3541
[ValidateNotNullOrEmpty()]
36-
[string]$SessionName
42+
[string]
43+
$SessionName,
44+
[Parameter(ParameterSetName = 'ReturnAllSessions')]
45+
[Parameter(ParameterSetName = 'SessionById')]
46+
[switch]
47+
$Online
3748
)
3849

3950
Begin
@@ -92,26 +103,52 @@ function Close-ExchangeSession
92103
# Get session by name
93104
[System.Management.Automation.Runspaces.PSSession]$sessionObject = Get-PSSession -Name $SessionName
94105
[string]$sessionConfiguration = $sessionObject.ConfigurationName
106+
[string]$sessionComputerName = $sessionObject.ComputerName
95107

96-
# Check session is the correct type
97-
if ($sessionConfiguration -like '*Exchange*')
108+
if ($PSBoundParameters.ContainsKey('Online'))
98109
{
99-
$paramRemovePSSession = @{
100-
Session = $sessionObject
101-
Confirm = $false
110+
if ($sessionConfiguration -like '*Exchange*')
111+
{
112+
$paramRemovePSSession = @{
113+
Session = $sessionObject
114+
Confirm = $false
115+
}
116+
117+
Remove-PSSession @paramRemovePSSession
118+
119+
return $true
120+
}
121+
else
122+
{
123+
Write-Warning -Message "Session $SessionId is not a valid Exchange session!"
124+
125+
return $false
102126
}
103-
104-
Remove-PSSession @paramRemovePSSession
105-
106-
return $true
107127
}
108128
else
109129
{
110-
Write-Warning -Message "Session $SessionId is not an Exchange session"
111-
112-
return $false
130+
# Check session is the correct type
131+
if (($sessionConfiguration -like '*Exchange*') -and
132+
($sessionComputerName -notlike '*Office365*'))
133+
{
134+
$paramRemovePSSession = @{
135+
Session = $sessionObject
136+
Confirm = $false
137+
}
138+
139+
Remove-PSSession @paramRemovePSSession
140+
141+
return $true
142+
}
143+
else
144+
{
145+
Write-Warning -Message "Session $SessionId is not a valid Exchange session!"
146+
147+
return $false
148+
}
113149
}
114150
}
151+
115152
catch
116153
{
117154
Write-Warning -Message "No PsSession with Name $SessionName found!"
@@ -121,7 +158,6 @@ function Close-ExchangeSession
121158

122159
break
123160
}
124-
125161
'ReturnAllSessions'
126162
{
127163
# Get all seesions
@@ -132,21 +168,36 @@ function Close-ExchangeSession
132168
{
133169
# Get configuration name
134170
[string]$sessionConfiguration = $session.ConfigurationName
135-
[string]$sessionId = $sessionObject.ID #TODO: review this property
171+
[string]$sessionId = $sessionObject.Id
172+
[string]$sessionComputerName = $sessionObject.ComputerName
136173

137174
try
138175
{
139-
# Check session is the correct type
140-
if ($sessionConfiguration -like '*Exchange*')
176+
if ($PSBoundParameters.ContainsKey('Online'))
141177
{
142-
$paramRemovePSSession = @{
143-
Session = $session
144-
Confirm = $false
178+
if ($sessionConfiguration -like '*Exchange*')
179+
{
180+
$paramRemovePSSession = @{
181+
Session = $sessionObject
182+
Confirm = $false
183+
}
184+
185+
Remove-PSSession @paramRemovePSSession
186+
}
187+
else
188+
{
189+
# Check session is the correct type
190+
if (($sessionConfiguration -like '*Exchange*') -and
191+
($sessionComputerName -notlike '*Office365*'))
192+
{
193+
$paramRemovePSSession = @{
194+
Session = $session
195+
Confirm = $false
196+
}
197+
198+
Remove-PSSession @paramRemovePSSession
199+
}
145200
}
146-
147-
Remove-PSSession @paramRemovePSSession
148-
149-
return $true
150201
}
151202
}
152203
catch
@@ -155,16 +206,16 @@ function Close-ExchangeSession
155206

156207
return $false
157208
}
209+
210+
break
158211
}
159-
160-
break
161212
}
162213
}
163-
}
164-
165-
End
166-
{
167-
# Revert back setting
168-
$ErrorActionPreference = $currentConfig
214+
215+
End
216+
{
217+
# Revert back setting
218+
$ErrorActionPreference = $currentConfig
219+
}
169220
}
170221
}

0 commit comments

Comments
 (0)