Skip to content

Commit 10be9b5

Browse files
authored
Release v1.5.1
## <a href="https://github.com/GruberMarkus/Export-RecipientPermissions/releases/tag/v1.5.1" target="_blank">v1.5.1</a> - 2022-06-27 ### Fixed - Make sure non-working Exchange Online connections are properly closed and do not remain active in the background
1 parent d289eb5 commit 10be9b5

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

docs/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
# Changelog
55

6+
## <a href="https://github.com/GruberMarkus/Export-RecipientPermissions/releases/tag/v1.5.1" target="_blank">v1.5.1</a> - 2022-06-27
7+
### Fixed
8+
- Make sure non-working Exchange Online connections are properly closed and do not remain active in the background
9+
610
## <a href="https://github.com/GruberMarkus/Export-RecipientPermissions/releases/tag/v1.5.0" target="_blank">v1.5.0</a> - 2022-06-25
711
### Changed
812
- The GrantorFilter parameter can now use the new reference variable '$Grantor' in addition to '$Recipient'. Both reference variables have identical values. '$Recipient' is now marked as obsolete and may removed in a future release.
913
### Added
10-
- New parameter 'GrantorFilter': Filter the trustees included in the export. See 'README' for details.
14+
- New parameter 'TrusteeFilter': Filter the trustees included in the export. See 'README' for details.
1115
- New sample code 'FiltersAndSidhistory.ps1' shows how to use TrusteeFilter and GrantorFilter to find permissions which may be affected by SIDHistory removal.
1216
- The connection to the cloud now uses the Exchange Online PowerShell V2 module (a preview version is used which allows traditional remote PowerShell access to all cmdlets). See '.\README' for details about the required 'ExchangeOnlineConnnectionParameters' parameter.
1317

src/Export-RecipientPermissions.ps1

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ Param(
247247

248248
# Name (and path) of the debug log file
249249
# Set to $null or '' to disable debugging
250-
[string]$DebugFile = '.\export\Export-RecipientPermissions_Debug.txt',
250+
[string]$DebugFile = '',
251251

252252

253253
# Interval to update the job progress
@@ -274,29 +274,30 @@ $ConnectExchange = {
274274

275275
while ($Stoploop -ne $true) {
276276
try {
277+
if ($Retrycount -gt 1) {
278+
Write-Host "Try $($Retrycount), via '$($connectionUri)'."
279+
}
280+
277281
if ($ExchangeSession) {
278-
if ($ExchangeSession.state -ine 'opened') {
279-
$ExchangeSessionWorking = $false
280-
} else {
281-
$ExchangeSessionWorking = (@(Invoke-Command -Session $ExchangeSession -HideComputerName -ScriptBlock { Get-SecurityPrincipal -ResultSize 1 -WarningAction SilentlyContinue } -ErrorAction SilentlyContinue).count -eq 1)
282-
}
282+
$null = Invoke-Command -Session $ExchangeSession -HideComputerName -ScriptBlock { Get-SecurityPrincipal -ResultSize 1 -WarningAction SilentlyContinue } -ErrorAction Stop
283+
284+
Write-Verbose "Exchange session established and working on try $($RetryCount)."
285+
286+
$Stoploop = $true
283287
} else {
284-
$ExchangeSessionWorking = $false
288+
throw
285289
}
290+
} catch {
291+
try {
292+
Write-Verbose "Exchange session not established or working on try $($RetryCount)."
286293

287-
if (-not $ExchangeSessionWorking) {
288-
Write-Verbose "Session 'ExchangeSession' not established or working."
289-
if ($ExchangeSession) {
290-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
291-
Disconnect-ExchangeOnline -Confirm:$false
292-
Remove-Module ExchangeOnlineManagement
293-
}
294-
295-
if ($ExchangeSession) {
296-
Remove-PSSession -Session $ExchangeSession
297-
}
294+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
295+
Disconnect-ExchangeOnline -Confirm:$false
296+
Remove-Module ExchangeOnlineManagement
297+
}
298298

299-
Start-Sleep -Seconds 70
299+
if ($ExchangeSession) {
300+
Remove-PSSession -Session $ExchangeSession
300301
}
301302

302303
if ($ExportFromOnPrem -eq $true) {
@@ -312,45 +313,45 @@ $ConnectExchange = {
312313
$ExchangeOnlineConnectionParameters['Credential'] = $ExchangeCredential
313314
}
314315

315-
if (Get-Module -Name 'ExchangeOnlineManagement') {
316-
Remove-Module ExchangeOnlineManagement
317-
}
316+
$ExchangeOnlineConnectionParameters['ConnectionUri'] = $connectionUri
317+
$ExchangeOnlineConnectionParameters['CommandName'] = ('Get-DistributionGroup', 'Get-DynamicDistributionGroup', 'Get-Mailbox', 'Get-MailboxFolderPermission', 'Get-MailboxFolderStatistics', 'Get-MailboxPermission', 'Get-Recipient', 'Get-RecipientPermission', 'Get-SecurityPrincipal', 'Get-UnifiedGroup')
318318

319319
Import-Module '.\bin\ExchangeOnlineManagement' -ErrorAction Stop
320-
Connect-ExchangeOnline @ExchangeOnlineConnectionParameters -ConnectionUri $connectionUri -CommandName ('Get-DistributionGroup', 'Get-DynamicDistributionGroup', 'Get-Mailbox', 'Get-MailboxFolderPermission', 'Get-MailboxFolderStatistics', 'Get-MailboxPermission', 'Get-Recipient', 'Get-RecipientPermission', 'Get-SecurityPrincipal', 'Get-UnifiedGroup')
320+
Connect-ExchangeOnline @ExchangeOnlineConnectionParameters
321321
$ExchangeSession = Get-PSSession | Sort-Object -Property Id | Select-Object -Last 1
322322
}
323323

324324
$null = Invoke-Command -Session $ExchangeSession -HideComputerName -ScriptBlock { Get-SecurityPrincipal -ResultSize 1 -WarningAction SilentlyContinue } -ErrorAction Stop
325-
}
326325

327-
Write-Verbose "Session 'ExchangeSession' established and working."
326+
Write-Verbose "Exchange session established and working on try $($RetryCount)."
328327

329-
$Stoploop = $true
330-
} catch {
331-
if ($Retrycount -le 3) {
332-
Write-Host "Could not connect to Exchange via '$connectionUri'."
333-
Write-Host $error[0]
334-
335-
$connectionUri = $tempConnectionUriQueue.dequeue()
336-
337-
Write-Host "Trying again in 70 seconds via '$connectionUri'."
328+
$Stoploop = $true
329+
} catch {
330+
if ($Retrycount -lt 3) {
331+
Write-Host "Exchange session could not be established in a working state to '$($connectionUri)' on try $($Retrycount)."
332+
Write-Host $error[0]
338333

339-
if ($ExchangeSession) {
340-
if ($ExportFromOnPrem -eq $false) {
334+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
341335
Disconnect-ExchangeOnline -Confirm:$false
336+
Remove-Module ExchangeOnlineManagement
342337
}
343338

344339
if ($ExchangeSession) {
345340
Remove-PSSession -Session $ExchangeSession
346341
}
347-
}
348342

349-
Start-Sleep -Seconds 70
350-
$Retrycount++
351-
} else {
352-
throw 'Could not connect to Exchange after three retries. Giving up.'
353-
$Stoploop = $true
343+
$connectionUri = $tempConnectionUriQueue.dequeue()
344+
345+
$SleepTime = (30 * $RetryCount * ($RetryCount / 2)) + 15
346+
347+
Write-Host "Trying again in $($SleepTime) seconds via '$connectionUri'."
348+
349+
Start-Sleep -Seconds $SleepTime
350+
$Retrycount++
351+
} else {
352+
throw "Exchange session could not be established in a working state on $($Retrycount) retries. Giving up."
353+
$Stoploop = $true
354+
}
354355
}
355356
}
356357
}
@@ -635,7 +636,7 @@ try {
635636
Write-Host
636637
Write-Host "Single-thread operations completed, remove connection to Exchange @$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')@"
637638

638-
if ($ExportFromOnPrem -eq $false) {
639+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
639640
Disconnect-ExchangeOnline -Confirm:$false
640641
Remove-Module ExchangeOnlineManagement
641642
}
@@ -795,7 +796,7 @@ try {
795796
} catch {
796797
"""$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')"";""Import LinkedMasterAccount"";"""";""$($_ | Out-String)""" -replace '(?<!;|^)"(?!;|$)', '""' | Add-Content -Path $ErrorFile -PassThru -Encoding $UTF8Encoding
797798
} finally {
798-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
799+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
799800
Disconnect-ExchangeOnline -Confirm:$false
800801
Remove-Module ExchangeOnlineManagement
801802
}
@@ -989,7 +990,7 @@ try {
989990
} catch {
990991
"""$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')"";""Find each recipient's UserFriendlyNames"";"""";""$($_ | Out-String)""" -replace '(?<!;|^)"(?!;|$)', '""' | Add-Content -Path $ErrorFile -PassThru -Encoding $UTF8Encoding
991992
} finally {
992-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
993+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
993994
Disconnect-ExchangeOnline -Confirm:$false
994995
Remove-Module ExchangeOnlineManagement
995996
}
@@ -1327,7 +1328,7 @@ try {
13271328
} catch {
13281329
"""$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')"";""Mailbox Access Rights"";""$($GrantorPrimarySMTP)"";""$($_ | Out-String)""" -replace '(?<!;|^)"(?!;|$)', '""' | Add-Content -Path $ErrorFile -PassThru -Encoding $UTF8Encoding
13291330
} finally {
1330-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
1331+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
13311332
Disconnect-ExchangeOnline -Confirm:$false
13321333
Remove-Module ExchangeOnlineManagement
13331334
}
@@ -1678,7 +1679,7 @@ try {
16781679
"""$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')"";""Mailbox Folder permissions"";""$($GrantorPrimarySMTP)"";""$($_ | Out-String)""" -replace '(?<!;|^)"(?!;|$)', '""' | Add-Content -Path $ErrorFile -PassThru -Encoding $UTF8Encoding
16791680

16801681
} finally {
1681-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
1682+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
16821683
Disconnect-ExchangeOnline -Confirm:$false
16831684
Remove-Module ExchangeOnlineManagement
16841685
}
@@ -2827,15 +2828,13 @@ try {
28272828
Write-Host
28282829
Write-Host "Clean-up @$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')@"
28292830

2830-
if ($ExchangeSession) {
2831-
if (($ExportFromOnPrem -eq $false) -and (Get-Module -Name 'ExchangeOnlineManagement')) {
2832-
Disconnect-ExchangeOnline -Confirm:$false
2833-
Remove-Module ExchangeOnlineManagement
2834-
}
2831+
if (($ExportFromOnPrem -eq $false) -and ((Get-Module -Name 'ExchangeOnlineManagement').count -ge 1)) {
2832+
Disconnect-ExchangeOnline -Confirm:$false
2833+
Remove-Module ExchangeOnlineManagement
2834+
}
28352835

2836-
if ($ExchangeSession) {
2837-
Remove-PSSession -Session $ExchangeSession
2838-
}
2836+
if ($ExchangeSession) {
2837+
Remove-PSSession -Session $ExchangeSession
28392838
}
28402839

28412840
Write-Host " Runspaces and RunspacePool @$(Get-Date -Format 'yyyy-MM-ddTHH:mm:sszzz')@"

0 commit comments

Comments
 (0)