From 9fbc2edb4e65fa70c0b201b71bfab397b6e8a43d Mon Sep 17 00:00:00 2001 From: Gioxx Date: Tue, 19 Nov 2024 09:11:27 +0100 Subject: [PATCH] 241031(1) --- Gioxx.ToyBox/Calendar/GTB.Calendar.ps1 | 23 +- Gioxx.ToyBox/Gioxx.ToyBox.psd1 | 3 +- Gioxx.ToyBox/Groups/GTB.Groups.ps1 | 25 +- Gioxx.ToyBox/Mailboxes/GTB.Mboxes.ps1 | 375 +++++++++++++-------- Gioxx.ToyBox/Main/GTB.Tools.ps1 | 88 +++-- Gioxx.ToyBox/Protection/GTB.Protection.ps1 | 13 +- Gioxx.ToyBox/Protection/GTB.Quarantine.ps1 | 8 +- Gioxx.ToyBox/Rooms/GTB.Room.ps1 | 6 +- Gioxx.ToyBox/Statistics/GTB.Stats.ps1 | 12 +- 9 files changed, 356 insertions(+), 197 deletions(-) diff --git a/Gioxx.ToyBox/Calendar/GTB.Calendar.ps1 b/Gioxx.ToyBox/Calendar/GTB.Calendar.ps1 index 9ae9371..0b709dd 100644 --- a/Gioxx.ToyBox/Calendar/GTB.Calendar.ps1 +++ b/Gioxx.ToyBox/Calendar/GTB.Calendar.ps1 @@ -27,6 +27,8 @@ function Export-CalendarPermission { param( [Parameter(Mandatory=$False, ValueFromPipeline=$True, HelpMessage="Mailbox to analyze (e.g. info@contoso.com)")] [string[]] $SourceMailbox, + [Parameter(Mandatory=$False, ValueFromPipeline=$True, HelpMessage="Domain to analyze (e.g. contoso.com)")] + [string] $SourceDomain, [Parameter(Mandatory=$False, HelpMessage="Folder where export CSV file (e.g. C:\Temp)")] [string] $folderCSV, [Parameter(Mandatory=$False, ValueFromPipeline=$True, HelpMessage="Export all mailboxes calendar permissions")] @@ -34,19 +36,22 @@ function Export-CalendarPermission { ) begin { - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue - + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $mboxCounter = 0 $arr_CalPerm = @() - $eolConnectedCheck = priv_CheckEOLConnection - if ( $eolConnectedCheck -eq $true ) { - if ([string]::IsNullOrEmpty($SourceMailbox)) { - Write-Warning "WARNING: no mailbox(es) specified, I scan all the mailboxes, please be patient." + if ( $eolConnectedCheck -eq $true ) { + if ([string]::IsNullOrEmpty($SourceMailbox) -And [string]::IsNullOrEmpty($SourceDomain)) { + Write-Warning "WARNING: no mailbox(es) or domain(s) specified, I scan all the mailboxes, please be patient." $All = $True } + if (-not([string]::IsNullOrEmpty($SourceDomain))) { + Write-InformationColored "Analyzing mailboxes in $($SourceDomain) ..." -ForegroundColor Cyan + $SourceMailbox = Get-Mailbox -ResultSize Unlimited -WarningAction SilentlyContinue | Where { $_.RecipientTypeDetails -ne "GuestMailUser" -And $_.EmailAddresses -like "*@" + $SourceDomain } + } + if ($All) { $SourceMailbox = Get-Mailbox -ResultSize Unlimited -WarningAction SilentlyContinue } @@ -86,7 +91,7 @@ function Export-CalendarPermission { $arr_CalPerm | Out-Host } - Set-Variable ProgressPreference $previousProgressPreference + priv_RestorePreferences } } @@ -140,7 +145,7 @@ function Set-OoO { $previousMessage = Get-MailboxAutoReplyConfiguration -Identity $SourceMailbox | Select -ExpandProperty ExternalMessage if ( [string]::IsNullOrEmpty($previousMessage) ) { $proposedText = "I'm out of office and will have limited access to my mailbox.
- I will reply to your email as soon as possible. + I will reply to your e-mail as soon as possible.

Have a nice day." } else { $proposedText = $previousMessage } diff --git a/Gioxx.ToyBox/Gioxx.ToyBox.psd1 b/Gioxx.ToyBox/Gioxx.ToyBox.psd1 index 78cce94..9f30eec 100644 --- a/Gioxx.ToyBox/Gioxx.ToyBox.psd1 +++ b/Gioxx.ToyBox/Gioxx.ToyBox.psd1 @@ -34,6 +34,7 @@ "Add-MboxPermission", "Change-MboxLanguage", "Change-MFAStatus", + "Check-SharedMailboxCompliance", "Clone-OoOMessage", "Connect-EOL", "Connect-MSOnline", @@ -86,7 +87,7 @@ # ReleaseNotes of this module # ReleaseNotes = '' - Prerelease = '20240924(3)' + Prerelease = '241031(1)' RequireLicenseAcceptance = $False } diff --git a/Gioxx.ToyBox/Groups/GTB.Groups.ps1 b/Gioxx.ToyBox/Groups/GTB.Groups.ps1 index 9f31f63..6391ec8 100644 --- a/Gioxx.ToyBox/Groups/GTB.Groups.ps1 +++ b/Gioxx.ToyBox/Groups/GTB.Groups.ps1 @@ -14,10 +14,10 @@ function Export-DG { [switch] $GridView ) - Set-Variable ProgressPreference Continue + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $DGsCounter = 0 $arr_ExportedDG = @() - $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { if ( [string]::IsNullOrEmpty($DG) ) { $All = $True } else { $DGs = Get-DistributionGroup $DG } @@ -78,6 +78,8 @@ function Export-DG { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } function Export-DDG { @@ -94,10 +96,10 @@ function Export-DDG { [switch] $GridView ) - Set-Variable ProgressPreference Continue + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $DDGsCounter = 0 $arr_ExportedDDG = @() - $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { if ( [string]::IsNullOrEmpty($DDG) ) { $All = $True } else { $DDGs = Get-DynamicDistributionGroup $DDG } @@ -158,6 +160,8 @@ function Export-DDG { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } function Export-M365Group { @@ -174,10 +178,10 @@ function Export-M365Group { [switch] $GridView ) - Set-Variable ProgressPreference Continue + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $M365GsCounter = 0 $arr_ExportedM365Groups = @() - $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { if ( [string]::IsNullOrEmpty($M365G) ) { $All = $True } else { $M365Gs = Get-UnifiedGroup $M365G } @@ -238,10 +242,13 @@ function Export-M365Group { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } function Get-RoleGroupsMembers { - Set-Variable ProgressPreference Continue + priv_SetPreferences + $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { @@ -288,14 +295,14 @@ function Get-UserGroups { Write-Host "Recipient not available or not found ($($UserPrincipalName))." -f "Red" break } elseif ($emailAddresses.Count -gt 1) { - Write-Host "Complete e-mail address not specified, multiple email addresses found:" -f "Cyan" + Write-Host "Complete e-mail address not specified, multiple e-mail addresses found:" -f "Cyan" $emailAddresses | ForEach-Object { Write-Host $_.Replace('SMTP:', ' ') } Write-Host "Run the command again but specify the full address to perform a more accurate search." -f "Cyan" $UserPrincipalName = (Get-Recipient $UserPrincipalName).PrimarySmtpAddress | Select-Object -First 1 Write-Host "First user selected: $($UserPrincipalName)" -f "Cyan" } else { $UserPrincipalName = (Get-Recipient $UserPrincipalName).PrimarySmtpAddress - Write-Host "Complete e-mail address not specified, user found: $($UserPrincipalName)" -f "Cyan" + Write-Host "Complete e-mail address not specified, first match found: $($UserPrincipalName)" -f "Cyan" } } catch { Write-Error $_.Exception.Message diff --git a/Gioxx.ToyBox/Mailboxes/GTB.Mboxes.ps1 b/Gioxx.ToyBox/Mailboxes/GTB.Mboxes.ps1 index 4022a57..d01b220 100644 --- a/Gioxx.ToyBox/Mailboxes/GTB.Mboxes.ps1 +++ b/Gioxx.ToyBox/Mailboxes/GTB.Mboxes.ps1 @@ -13,10 +13,19 @@ function Add-MboxAlias { if ( $eolConnectedCheck -eq $true ) { try { - $GRRTD = (Get-Recipient $SourceMailbox -ErrorAction Stop).RecipientTypeDetails + $recipient = Get-Recipient $SourceMailbox -ErrorAction Stop + $GRRTD = $recipient.RecipientTypeDetails + $existingAliases = $recipient.EmailAddresses } catch { Write-Warning "`nUsage: Add-MboxAlias -SourceMailbox mailbox@contoso.com -MailboxAlias alias@contoso.com`n" Write-Error $_.Exception.Message + return + } + + # Check if the alias already exists + if ($existingAliases -contains $MailboxAlias) { + Write-InformationColored "`nAlias '$MailboxAlias' already exists for mailbox '$SourceMailbox'. No action taken." -ForegroundColor "Yellow" + return } Switch ($GRRTD) { @@ -49,8 +58,7 @@ function Add-MboxPermission { begin { if ([string]::IsNullOrEmpty($AccessRights)) { $AccessRights = "All" } - $previousInformationPreference = $InformationPreference - Set-Variable InformationPreference Continue + priv_SetPreferences -Verbose } process { @@ -61,11 +69,26 @@ function Add-MboxPermission { if ( $eolConnectedCheck -eq $true ) { $UserMailbox | ForEach { $CurrentUser = $_ + + # Checks if the user exists + $userExists = Get-User -Identity $CurrentUser -ErrorAction SilentlyContinue + if (-not $userExists) { + Write-Error "`nThe mailbox $($CurrentUser) does not exist. Please check the provided e-mail address." + return + } + + # Continue with assigning permissions only if the user exists Switch ($AccessRights) { "FullAccess" { - if ($AutoMapping) { - Write-Information "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) ..." - $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$True -Confirm:$False + $existingPermission = Get-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -ErrorAction SilentlyContinue | Where-Object { $_.AccessRights -contains 'FullAccess' -and $_.IsInherited -eq $false } + if (-not $existingPermission) { + if ($AutoMapping) { + Write-Output "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) ..." + $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$True -Confirm:$False + } else { + Write-Output "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) without AutoMapping ..." + $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$False -Confirm:$False + } $addMboxPermDN = (Get-User -Identity $addMboxPerm.User).DisplayName [PSCustomObject]@{ Identity = $addMboxPerm.Identity @@ -76,51 +99,13 @@ function Add-MboxPermission { Deny = $addMboxPerm.Deny } | Out-Host } else { - Write-Information "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) without AutoMapping ..." - $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$False -Confirm:$False - $addMboxPermDN = (Get-User -Identity $addMboxPerm.User).DisplayName - [PSCustomObject]@{ - Identity = $addMboxPerm.Identity - User = $addMboxPerm.User - DisplayName = $addMboxPermDN - AccessRights = $addMboxPerm.AccessRights - IsInherited = $addMboxPerm.IsInherited - Deny = $addMboxPerm.Deny - } | Out-Host + Write-InformationColored "`n$($CurrentUser) already has FullAccess permission to $($SourceMailbox)" -ForegroundColor "Yellow" } } "SendAs" { - Write-Information "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." - $addMboxPerm = Add-RecipientPermission $SourceMailbox -Trustee $CurrentUser -AccessRights SendAs -Confirm:$False - $addMboxPermDN = (Get-User -Identity $addMboxPerm.Trustee).DisplayName - [PSCustomObject]@{ - Identity = $addMboxPerm.Identity - Trustee = $addMboxPerm.Trustee - DisplayName = $addMboxPermDN - AccessControlType = $addMboxPerm.AccessControlType - AccessRights = $addMboxPerm.AccessRights - } | Out-Host - } - "SendOnBehalfTo" { - Write-Information "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." - Set-Mailbox $SourceMailbox -GrantSendOnBehalfTo @{add="$($CurrentUser)"} -Confirm:$False | Out-Host - } - "All" { - if ($AutoMapping) { - - Write-Information "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) ..." - $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$True -Confirm:$False - $addMboxPermDN = (Get-User -Identity $addMboxPerm.User).DisplayName - [PSCustomObject]@{ - Identity = $addMboxPerm.Identity - User = $addMboxPerm.User - DisplayName = $addMboxPermDN - AccessRights = $addMboxPerm.AccessRights - IsInherited = $addMboxPerm.IsInherited - Deny = $addMboxPerm.Deny - } | Out-Host - - Write-Information "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." + $existingPermission = Get-RecipientPermission -Identity $SourceMailbox -Trustee $CurrentUser -ErrorAction SilentlyContinue | Where-Object { $_.AccessRights -contains 'SendAs' } + if (-not $existingPermission) { + Write-Output "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." $addMboxPerm = Add-RecipientPermission $SourceMailbox -Trustee $CurrentUser -AccessRights SendAs -Confirm:$False $addMboxPermDN = (Get-User -Identity $addMboxPerm.Trustee).DisplayName [PSCustomObject]@{ @@ -130,11 +115,30 @@ function Add-MboxPermission { AccessControlType = $addMboxPerm.AccessControlType AccessRights = $addMboxPerm.AccessRights } | Out-Host - } else { - - Write-Information "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) without AutoMapping ..." - $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$False -Confirm:$False + Write-InformationColored "`n$($CurrentUser) already has SendAs permission to $($SourceMailbox)" -ForegroundColor "Yellow" + } + } + "SendOnBehalfTo" { + $existingPermission = (Get-Mailbox -Identity $SourceMailbox).GrantSendOnBehalfTo | Where-Object { $_ -eq $CurrentUser } + if (-not $existingPermission) { + Write-Output "`nAdd $($CurrentUser) (SendOnBehalfTo) to $($SourceMailbox) ..." + Set-Mailbox $SourceMailbox -GrantSendOnBehalfTo @{add="$($CurrentUser)"} -Confirm:$False | Out-Host + } else { + Write-InformationColored "`n$($CurrentUser) already has SendOnBehalfTo permission to $($SourceMailbox)" -ForegroundColor "Yellow" + } + } + "All" { + # FullAccess + $existingFullAccess = Get-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -ErrorAction SilentlyContinue | Where-Object { $_.AccessRights -contains 'FullAccess' -and $_.IsInherited -eq $false } + if (-not $existingFullAccess) { + if ($AutoMapping) { + Write-Output "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) ..." + $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$True -Confirm:$False + } else { + Write-Output "`nAdd $($CurrentUser) (FullAccess) to $($SourceMailbox) without AutoMapping ..." + $addMboxPerm = Add-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -AutoMapping:$False -Confirm:$False + } $addMboxPermDN = (Get-User -Identity $addMboxPerm.User).DisplayName [PSCustomObject]@{ Identity = $addMboxPerm.Identity @@ -144,8 +148,14 @@ function Add-MboxPermission { IsInherited = $addMboxPerm.IsInherited Deny = $addMboxPerm.Deny } | Out-Host + } else { + Write-InformationColored "`n$($CurrentUser) already has FullAccess permission to $($SourceMailbox)" -ForegroundColor "Yellow" + } - Write-Information "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." + # SendAs + $existingSendAs = Get-RecipientPermission -Identity $SourceMailbox -Trustee $CurrentUser -ErrorAction SilentlyContinue | Where-Object { $_.AccessRights -contains 'SendAs' } + if (-not $existingSendAs) { + Write-Output "`nAdd $($CurrentUser) (SendAs) to $($SourceMailbox) ..." $addMboxPerm = Add-RecipientPermission $SourceMailbox -Trustee $CurrentUser -AccessRights SendAs -Confirm:$False $addMboxPermDN = (Get-User -Identity $addMboxPerm.Trustee).DisplayName [PSCustomObject]@{ @@ -155,7 +165,8 @@ function Add-MboxPermission { AccessControlType = $addMboxPerm.AccessControlType AccessRights = $addMboxPerm.AccessRights } | Out-Host - + } else { + Write-InformationColored "`n$($CurrentUser) already has SendAs permission to $($SourceMailbox)" -ForegroundColor "Yellow" } } } @@ -164,7 +175,7 @@ function Add-MboxPermission { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } - Set-Variable InformationPreference $previousInformationPreference + priv_RestorePreferences } } @@ -178,7 +189,7 @@ function Change-MboxLanguage { [string] $CSV ) - Set-Variable ProgressPreference Continue + priv_SetPreferences -Verbose $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { @@ -201,6 +212,96 @@ function Change-MboxLanguage { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences +} + +function Check-SharedMailboxCompliance { + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection + + if ( $eolConnectedCheck -eq $true ) { + + $mggConnectedCheck = priv_CheckMGGraphModule + if ( $mggConnectedCheck -eq $true ) { + Write-InformationColored "`nFinding shared mailboxes... " -NoNewLine + $Mbx = Get-ExoMailbox -RecipientTypeDetails SharedMailbox -ResultSize Unlimited | Sort-Object DisplayName + If ($Mbx) { + Write-InformationColored ("{0} shared mailboxes found." -f $Mbx.Count) -ForegroundColor "Cyan" + $mboxCounter = 0 + } Else { + Write-InformationColored "No shared mailboxes found." -ForegroundColor "Red" + Break + } + # Define the service plan IDs for Exchange Online (Plan 1) and Exchange Online (Plan 2) + $ExoServicePlan1 = "9aaf7827-d63c-4b61-89c3-182f06f82e5c" + $ExoServicePlan2 = "efb87545-963c-4e0d-99df-69c6916d9eb0" + $Report = [System.Collections.Generic.List[Object]]::new() + + ForEach ($M in $Mbx) { + # $mboxCounter++ + # $PercentComplete = (($mboxCounter / $Mbx.Count) * 100) + # Write-Progress -Activity "Processing $((Get-Recipient $M).DisplayName)" -Status "$mboxCounter out of $($Mbx.Count) ($($PercentComplete.ToString('0.00'))%)" -PercentComplete $PercentComplete + + $ExoPlan1Found = $false; $ExoPlan2Found = $false; $LogsFound = "No" + Write-Output ("Checking sign-in records for {0}" -f $M.DisplayName) + $UserId = $M.ExternalDirectoryObjectId + [array]$Logs = Get-MgAuditLogSignIn -Filter "userid eq '$UserId'" -All -Top 1 + If ($Logs) { + + # Check if there are successful sign-in records (at least one), otherwise, don't keep track of them (these could be attack attempts) + Write-Output ("Checking for successful sign-in records for {0}" -f $M.DisplayName) + [array]$search4SuccessfulLogins = Get-MgAuditLogSignIn -Filter "userid eq '$UserId'" -All + $search4SuccessfulLogins | ForEach-Object { + If ($_.Status.ErrorCode -eq "0") { + $LogsFound = "Yes" + } + } + + If ($LogsFound -eq "Yes") { + Write-InformationColored ("Sign-in records found for shared mailbox {0}" -f $M.DisplayName) -ForegroundColor "Red" + # Check if the shared mailbox is licensed + $User = Get-MgUser -UserId $M.ExternalDirectoryObjectId -Property UserPrincipalName, AccountEnabled, Id, DisplayName, assignedPlans + [array]$ExoPlans = $User.AssignedPlans | Where-Object {$_.Service -eq 'exchange' -and $_.capabilityStatus -eq 'Enabled'} + If ($ExoServicePlan1 -in $ExoPlans.ServicePlanId) { + $ExoPlan1Found = $true + + } ElseIf ($ExoServicePlan2 -in $ExoPlans.ServicePlanId) { + $ExoPlan2Found = $true + } + + If ($ExoPlan1Found -eq $true) { + Write-Output ("Shared mailbox {0} has Exchange Online (Plan 1) license" -f $M.DisplayName) + } ElseIf ($ExoPlan2Found -eq $true) { + Write-Output ("Shared mailbox {0} has Exchange Online (Plan 2) license" -f $M.DisplayName) + } Else { + Write-InformationColored ("Shared mailbox {0} has no Exchange Online license" -f $M.DisplayName) -ForegroundColor "Yellow" + } + } else { + Write-InformationColored ("No successful sign-in records found for shared mailbox {0}" -f $M.DisplayName) -ForegroundColor "Green" + } + } + + $ReportLine = [PSCustomObject] @{ + DisplayName = $M.DisplayName + ExternalDirectoryObjectId = $M.ExternalDirectoryObjectId + 'Sign in Record Found' = $LogsFound + 'Exchange Online Plan 1' = $ExoPlan1Found + 'Exchange Online Plan 2' = $ExoPlan2Found + } + $Report.Add($ReportLine) + } + + $Report | Out-GridView -Title "Shared Mailbox Sign-In Records and Licensing Status" + + } else { + Write-Error "`nCan't connect or use Microsoft Graph modules. `nPlease check logs." + } + } else { + Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." + } + + priv_RestorePreferences } function Export-MboxAlias { @@ -218,11 +319,10 @@ function Export-MboxAlias { ) begin { + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $mboxCounter = 0 $arr_MboxAliases = @() - $eolConnectedCheck = priv_CheckEOLConnection - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue if ( $eolConnectedCheck -eq $true ) { @@ -282,6 +382,8 @@ function Export-MboxAlias { Write-Error $_.Exception.Message } } + + priv_RestorePreferences } end { @@ -292,7 +394,7 @@ function Export-MboxAlias { $arr_MboxAliases | Out-Host } - Set-Variable ProgressPreference $previousProgressPreference + priv_RestorePreferences } } @@ -304,9 +406,9 @@ function Export-MboxPermission { [string] $folderCSV ) + priv_SetPreferences -Verbose $arr_MboxPerms = @() $mboxCounter = 0 - Set-Variable ProgressPreference Continue # $warningPrefBackup = priv_HideWarning $eolConnectedCheck = priv_CheckEOLConnection @@ -404,71 +506,72 @@ function Get-MboxPermission { $arr_MbxPerms = @() $eolConnectedCheck = priv_CheckEOLConnection + priv_SetPreferences -Verbose if ( $eolConnectedCheck -eq $true ) { - $MboxPermFullAccess = Get-MailboxPermission $(Get-Mailbox $SourceMailbox).PrimarySmtpAddress | - Where-Object { $_.AccessRights -eq "FullAccess" -and !$_.IsInherited } | - ForEach-Object { - $UserMailbox = $_.User.ToString() - $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress - $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName - - $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } - if ($existingUserObject) { - $existingUserObject.AccessRights += ", FullAccess" - } else { - $arr_MbxPerms += [PSCustomObject]@{ - User = $DisplayName - UserMailbox = $PrimarySmtpAddress - AccessRights = "FullAccess" - } - } - } - - $MboxPermSendAs = Get-RecipientPermission $(Get-Mailbox $SourceMailbox).PrimarySmtpAddress -AccessRights SendAs | - Where-Object { $_.Trustee.ToString() -ne "NT AUTHORITY\SELF" -And $_.Trustee.ToString() -notlike "S-1-5*" } | - ForEach-Object { - $UserMailbox = $_.Trustee.ToString() - $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress - $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName - - $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } - if ($existingUserObject) { - $existingUserObject.AccessRights += ", SendAs" - } else { - $arr_MbxPerms += [PSCustomObject]@{ - User = $DisplayName - UserMailbox = $PrimarySmtpAddress - AccessRights = "SendAs" - } - } + $MboxPermFullAccess = Get-MailboxPermission $(Get-Mailbox $SourceMailbox).PrimarySmtpAddress | Where-Object { $_.AccessRights -eq "FullAccess" -and !$_.IsInherited } | ForEach-Object { + $UserMailbox = $_.User.ToString() + $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress + $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName + + $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } + if ($existingUserObject) { + $existingUserObject.AccessRights += ", FullAccess" + } else { + $arr_MbxPerms += [PSCustomObject]@{ + User = $DisplayName + UserMailbox = $PrimarySmtpAddress + AccessRights = "FullAccess" + } + } + } + Write-Progress -Activity "Gathered FullAccess permissions for $($SourceMailbox) ..." -Status "35% Complete" -PercentComplete 35 + + $MboxPermSendAs = Get-RecipientPermission $(Get-Mailbox $SourceMailbox).PrimarySmtpAddress -AccessRights SendAs | Where-Object { $_.Trustee.ToString() -ne "NT AUTHORITY\SELF" -And $_.Trustee.ToString() -notlike "S-1-5*" } | ForEach-Object { + $UserMailbox = $_.Trustee.ToString() + $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress + $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName + + $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } + if ($existingUserObject) { + $existingUserObject.AccessRights += ", SendAs" + } else { + $arr_MbxPerms += [PSCustomObject]@{ + User = $DisplayName + UserMailbox = $PrimarySmtpAddress + AccessRights = "SendAs" } - - $MboxPermSendOnBehalfTo = $(Get-Mailbox $SourceMailbox).GrantSendOnBehalfTo | - ForEach-Object { - $UserMailbox = $_ - $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress - $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName - - $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } - if ($existingUserObject) { - $existingUserObject.AccessRights += ", SendOnBehalfTo" - } else { - $arr_MbxPerms += [PSCustomObject]@{ - User = $DisplayName - UserMailbox = $PrimarySmtpAddress - AccessRights = "SendOnBehalfTo" - } - } + } + } + Write-Progress -Activity "Gathered SendAs permissions for $($SourceMailbox) ..." -Status "50% Complete" -PercentComplete 50 + + $MboxPermSendOnBehalfTo = $(Get-Mailbox $SourceMailbox).GrantSendOnBehalfTo | ForEach-Object { + $UserMailbox = $_ + $PrimarySmtpAddress = $(Get-Mailbox $UserMailbox -ErrorAction SilentlyContinue).PrimarySmtpAddress + $DisplayName = $(Get-User -Identity $UserMailbox).DisplayName + + $existingUserObject = $arr_MbxPerms | Where-Object { $_.UserMailbox -eq $PrimarySmtpAddress } + if ($existingUserObject) { + $existingUserObject.AccessRights += ", SendOnBehalfTo" + } else { + $arr_MbxPerms += [PSCustomObject]@{ + User = $DisplayName + UserMailbox = $PrimarySmtpAddress + AccessRights = "SendOnBehalfTo" } + } + } + Write-Progress -Activity "Gathered SendOnBehalfTo permissions for $($SourceMailbox) ..." -Status "90% Complete" -PercentComplete 90 - Write-Host "`nAccess Rights on $((Get-Mailbox $SourceMailbox).DisplayName) ($((Get-Mailbox $SourceMailbox).PrimarySmtpAddress))" -f "Yellow" + Write-InformationColored "`nAccess Rights on $((Get-Mailbox $SourceMailbox).DisplayName) ($((Get-Mailbox $SourceMailbox).PrimarySmtpAddress))" -ForegroundColor "Yellow" $arr_MbxPerms | Out-Host } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } function New-SharedMailbox { @@ -542,8 +645,7 @@ function Remove-MboxPermission { ) begin { - $previousInformationPreference = $InformationPreference - Set-Variable InformationPreference Continue + priv_SetPreferences -Verbose if ([string]::IsNullOrEmpty($AccessRights)) { $AccessRights = "All" } } @@ -555,25 +657,34 @@ function Remove-MboxPermission { if ( $eolConnectedCheck -eq $true ) { $UserMailbox | ForEach { $CurrentUser = $_ + + # Checks if the user exists + $userExists = Get-User -Identity $CurrentUser -ErrorAction SilentlyContinue + if (-not $userExists) { + Write-Error "`nThe mailbox $($CurrentUser) does not exist. Please check the provided e-mail address." + return + } + + # Continue with removing permissions only if the user exists Switch ($AccessRights) { "FullAccess" { - Write-Information "Removing full access for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing full access for $($CurrentUser) from $($SourceMailbox) ..." Remove-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -Confirm:$False } "SendAs" { - Write-Information "Removing SendAs for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing SendAs for $($CurrentUser) from $($SourceMailbox) ..." Remove-RecipientPermission $SourceMailbox -Trustee $CurrentUser -AccessRights SendAs -Confirm:$False } "SendOnBehalfTo" { - Write-Information "Removing SendOnBehalfTo for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing SendOnBehalfTo for $($CurrentUser) from $($SourceMailbox) ..." Set-Mailbox $SourceMailbox -GrantSendOnBehalfTo @{remove="$($CurrentUser)"} -Confirm:$False } "All" { - Write-Information "Removing full access for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing full access for $($CurrentUser) from $($SourceMailbox) ..." Remove-MailboxPermission -Identity $SourceMailbox -User $CurrentUser -AccessRights FullAccess -Confirm:$False - Write-Information "Removing SendAs for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing SendAs for $($CurrentUser) from $($SourceMailbox) ..." Remove-RecipientPermission $SourceMailbox -Trustee $CurrentUser -AccessRights SendAs -Confirm:$False - Write-Information "Removing SendOnBehalfTo for $($CurrentUser) from $($SourceMailbox) ..." + Write-Output "Removing SendOnBehalfTo for $($CurrentUser) from $($SourceMailbox) ..." Set-Mailbox $SourceMailbox -GrantSendOnBehalfTo @{remove="$($CurrentUser)"} -Confirm:$False } } @@ -583,7 +694,7 @@ function Remove-MboxPermission { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } - Set-Variable InformationPreference $previousInformationPreference + priv_RestorePreferences } } @@ -594,10 +705,9 @@ function Set-MboxRulesQuota { ) begin { + priv_SetPreferences -Verbose $mboxCounter = 0 $arr_MboxRulesQuota = @() - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue } process { @@ -630,28 +740,26 @@ function Set-MboxRulesQuota { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } - Set-Variable ProgressPreference $previousProgressPreference + priv_RestorePreferences } } function Set-SharedMboxCopyForSent { # Credits: https://stackoverflow.com/q/51680709 param( - [Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="Shared mailbox address to which to activate the copy of sent emails (e.g. info@contoso.com)")] + [Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="Shared mailbox address to which to activate the copy of sent e-mails (e.g. info@contoso.com)")] [string[]] $SourceMailbox ) begin { + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $mboxCounter = 0 $arr_SharedMboxCFS = @() $arr_SharedMboxCFSError = @() - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue } process { - $eolConnectedCheck = priv_CheckEOLConnection - if ( $eolConnectedCheck -eq $true ) { $SourceMailbox | ForEach { try { @@ -684,7 +792,7 @@ function Set-SharedMboxCopyForSent { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } - Set-Variable ProgressPreference $previousProgressPreference + priv_RestorePreferences } } @@ -695,6 +803,7 @@ Export-ModuleMember -Alias * Export-ModuleMember -Function "Add-MboxAlias" Export-ModuleMember -Function "Add-MboxPermission" Export-ModuleMember -Function "Change-MboxLanguage" +Export-ModuleMember -Function "Check-SharedMailboxCompliance" Export-ModuleMember -Function "Export-MboxAlias" Export-ModuleMember -Function "Export-MboxPermission" Export-ModuleMember -Function "Get-MboxAlias" diff --git a/Gioxx.ToyBox/Main/GTB.Tools.ps1 b/Gioxx.ToyBox/Main/GTB.Tools.ps1 index 893eb0b..e20e29f 100644 --- a/Gioxx.ToyBox/Main/GTB.Tools.ps1 +++ b/Gioxx.ToyBox/Main/GTB.Tools.ps1 @@ -2,42 +2,42 @@ function priv_CheckEOLConnection { $eolConnected = $false - $userConnected = priv_MailSearcher if ( (Get-Module -Name ExchangeOnlineManagement -ListAvailable).count -gt 0 ) { try { Get-EXOMailbox -ResultSize 1 -ErrorAction Stop $eolConnected = $true } catch { + $userConnected = priv_MailSearcher if ( $userConnected -ne "notfound" ) { - Write-Host "Please wait until I load Microsoft Exchange Online Management.`nConnecting using $($userConnected) ..." -f "Yellow" - Connect-EOL -UserPrincipalName $userConnected + Write-InformationColored "Please wait until I load Microsoft Exchange Online Management.`nConnecting using $($userConnected) ..." -ForegroundColor Yellow + Connect-EOL -UserPrincipalName $userConnected } else { - Write-Host "Please wait until I load Microsoft Exchange Online Management." -f "Yellow" - Connect-EOL + Write-InformationColored "Please wait until I load Microsoft Exchange Online Management." -ForegroundColor Yellow + Connect-EOL } $eolConnected = $true } } else { - Write-Host "Microsoft Exchange Online Management module is not available." -f "Yellow" + Write-Warning "Microsoft Exchange Online Management module is not available." $Confirm = Read-Host "Are you sure you want to install module? [Y] Yes [N] No " if ( $Confirm -match "[yY]" ) { try { - Write-host "Installing Microsoft Exchange Online Management PowerShell module ..." + Write-InformationColored "Installing Microsoft Exchange Online Management PowerShell module ..." -ForegroundColor Yellow Install-Module ExchangeOnlineManagement -Scope CurrentUser -AllowClobber -Force if ( $userConnected -ne "notfound" ) { - Write-Host "Please wait until I load Microsoft Exchange Online Management.`nConnecting using $($userConnected) ..." -f "Yellow" + Write-InformationColored "Please wait until I load Microsoft Exchange Online Management.`nConnecting using $($userConnected) ..." -ForegroundColor Yellow Connect-EOL -UserPrincipalName $userConnected } else { Connect-EOL } $eolConnected = $true } catch { - Write-Host "`nCan't install Exchange Online Management modules. `nPlease check logs." -f "Red" + Write-Error "`nCan't install Exchange Online Management modules. `nPlease check logs." exit } } else { - Write-Host "`nMicrosoft Exchange Online Management module is required to run this script. `nPlease install module using Install-Module ExchangeOnlineManagement cmdlet." -f "Red" + Write-Error "`nMicrosoft Exchange Online Management module is required to run this script. `nPlease install module using Install-Module ExchangeOnlineManagement cmdlet." exit } } @@ -60,7 +60,7 @@ function priv_CheckMsolEmbeddedService { } } else { Write-Error "MSOnline module is not available on the system." - Write-InformationColored "Please use embedded Windows PowerShell and execute this command: `nInstall-Module MSOnline" -ForegroundColor "Yellow" + Write-InformationColored "Please use embedded Windows PowerShell and execute this command: `nInstall-Module MSOnline" -ForegroundColor Yellow } return $msolServiceConnected @@ -84,29 +84,29 @@ function priv_CheckMGGraphModule { Get-MgUser -ErrorAction Stop $mggConnected = $true } catch { - Write-Host "Please wait until I load Microsoft Graph, the operation may take a minute or more." -f "Yellow" + Write-InformationColored "Please wait until I load Microsoft Graph, the operation may take a minute or more." -ForegroundColor Yellow # Import-Module Microsoft.Graph -ErrorAction SilentlyContinue # Import-Module Microsoft.Graph.Users -ErrorAction SilentlyContinue Connect-MgGraph $mggConnected = $true } } else { - Write-Host "Microsoft Graph PowerShell module is not available." -f "Yellow" + Write-Warning "Microsoft Graph PowerShell module is not available." $Confirm = Read-Host "Are you sure you want to install module? [Y] Yes [N] No " if ( $Confirm -match "[yY]" ) { try { - Write-host "Installing Microsoft Graph PowerShell module ..." + Write-InformationColored "Installing Microsoft Graph PowerShell module ..." -ForegroundColor Yellow Install-Module Microsoft.Graph -Repository PSGallery -Scope CurrentUser -AllowClobber -Force # Import-Module Microsoft.Graph -ErrorAction SilentlyContinue # Import-Module Microsoft.Graph.Users -ErrorAction SilentlyContinue Connect-MgGraph $mggConnected = $true } catch { - Write-Host "`nCan't install and import Graph modules. `nPlease check logs." -f "Red" + Write-Error "`nCan't install and import Graph modules. `nPlease check logs." exit } } else { - Write-Host "`nMicrosoft Graph PowerShell module is required to run this script. `nPlease install module using Install-Module Microsoft.Graph cmdlet." -f "Red" + Write-Error "`nMicrosoft Graph PowerShell module is required to run this script. `nPlease install module using Install-Module Microsoft.Graph cmdlet." exit } } @@ -162,7 +162,7 @@ function priv_GUI_TextBox ($headerMessage, $defaultText) { if ($textBox.Text -eq '') { # Empty TextBox - Write-Host "Message can't be empty, operation canceled." -f "Yellow" + Write-Error "Message can't be empty, operation canceled." break } else { if ( $ShowDialogResult -eq [System.Windows.Forms.DialogResult]::OK ) { @@ -175,7 +175,7 @@ function priv_GUI_TextBox ($headerMessage, $defaultText) { } if ( $ShowDialogResult -eq [System.Windows.Forms.DialogResult]::Cancel ) { - Write-Host "Operation canceled (Aborted by user)." -f "Yellow" + Write-Error "Operation canceled (Aborted by user)." break } } @@ -189,13 +189,32 @@ function priv_HideWarning { function priv_MailSearcher { # Credits: https://powershellmagazine.com/2012/11/14/pstip-how-to-get-the-email-address-of-the-currently-logged-on-user/ - $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)" - if ( $searcher -ne $null ) { - $mailAddress = $searcher.FindOne().Properties.mail + # $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)" + # if ( $searcher -ne $null ) { + # $mailAddress = $searcher.FindOne().Properties.mail + # return $mailAddress + # } else { + # return "notfound" + # } + + try { + $searcher = [adsisearcher]"(samaccountname=$env:USERNAME)" + if ($searcher -ne $null) { + $mailAddress = $searcher.FindOne().Properties.mail + if ($mailAddress) { + return $mailAddress + } else { + return "notfound" + } + } else { + return "notfound" + } + } catch { + Write-Warning "Unable to automatically find e-mail address for current user." + $mailAddress = Read-Host "Specify your e-mail address" return $mailAddress - } else { - return "notfound" } + } function priv_MaxLenghtSubString($string, $maxchars) { @@ -206,6 +225,16 @@ function priv_MaxLenghtSubString($string, $maxchars) { } } +function priv_RestorePreferences { + if ($global:PreviousInformationPreference -ne $null) { + Set-Variable -Name InformationPreference -Value $global:PreviousInformationPreference -Scope Global + } + + if ($global:PreviousProgressPreference -ne $null) { + Set-Variable -Name ProgressPreference -Value $global:PreviousProgressPreference -Scope Global + } +} + function priv_SaveFileWithProgressiveNumber($path) { $baseName = [System.IO.Path]::GetFileNameWithoutExtension($path) $extension = [System.IO.Path]::GetExtension($path) @@ -220,6 +249,19 @@ function priv_SaveFileWithProgressiveNumber($path) { return $path } +function priv_SetPreferences { + param ( + [switch]$Verbose + ) + $global:PreviousInformationPreference = $InformationPreference + $global:PreviousProgressPreference = $ProgressPreference + + if ($Verbose) { + Set-Variable -Name InformationPreference -Value Continue -Scope Global + Set-Variable -Name ProgressPreference -Value Continue -Scope Global + } +} + function priv_TakeDecisionOptions($message, $yes, $no, $yesHint, $noHint, $defaultOption=0) { $option_1 = New-Object System.Management.Automation.Host.ChoiceDescription "$($yes)", "$($yesHint)" $option_2 = New-Object System.Management.Automation.Host.ChoiceDescription "$($no)", "$($noHint)" diff --git a/Gioxx.ToyBox/Protection/GTB.Protection.ps1 b/Gioxx.ToyBox/Protection/GTB.Protection.ps1 index 40e30b5..4195557 100644 --- a/Gioxx.ToyBox/Protection/GTB.Protection.ps1 +++ b/Gioxx.ToyBox/Protection/GTB.Protection.ps1 @@ -42,17 +42,13 @@ function Export-MFAStatus { [string] $folderCSV ) - $previousInformationPreference = $InformationPreference - Set-Variable InformationPreference Continue - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue - - $folder = priv_CheckFolder($folderCSV) + priv_SetPreferences -Verbose $mggConnectedCheck = priv_CheckMGGraphModule + $folder = priv_CheckFolder($folderCSV) if ( $mggConnectedCheck -eq $true ) { try { - Connect-MgGraph -Scopes "Reports.Read.All,AuditLog.Read.All" + Connect-MgGraph -Scopes "Reports.Read.All,AuditLog.Read.All" -NoWelcome # Fetch user registration detail report from Microsoft Graph $Users = Get-MgReportAuthenticationMethodUserRegistrationDetail @@ -88,8 +84,7 @@ function Export-MFAStatus { Write-Error "`nCan't connect or use Microsoft Graph Modules. `nPlease check logs." } - Set-Variable InformationPreference $previousInformationPreference - Set-Variable ProgressPreference $previousProgressPreference + priv_RestorePreferences } function User-DisableDevices { diff --git a/Gioxx.ToyBox/Protection/GTB.Quarantine.ps1 b/Gioxx.ToyBox/Protection/GTB.Quarantine.ps1 index 3d86a16..5f35c8b 100644 --- a/Gioxx.ToyBox/Protection/GTB.Quarantine.ps1 +++ b/Gioxx.ToyBox/Protection/GTB.Quarantine.ps1 @@ -119,10 +119,7 @@ function Get-QuarantineToRelease { return } - $previousProgressPreference = $ProgressPreference - Set-Variable ProgressPreference Continue - $previousInformationPreference = $InformationPreference - Set-Variable InformationPreference Continue + priv_SetPreferences -Verbose if ($ChooseDayFromCalendar) { [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") @@ -352,8 +349,7 @@ function Get-QuarantineToRelease { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } - Set-Variable ProgressPreference $previousProgressPreference - Set-Variable InformationPreference $previousInformationPreference + priv_RestorePreferences } function Release-QuarantineFrom { diff --git a/Gioxx.ToyBox/Rooms/GTB.Room.ps1 b/Gioxx.ToyBox/Rooms/GTB.Room.ps1 index cb4cc4f..b9e0a45 100644 --- a/Gioxx.ToyBox/Rooms/GTB.Room.ps1 +++ b/Gioxx.ToyBox/Rooms/GTB.Room.ps1 @@ -10,10 +10,10 @@ function Get-RoomsDetails { [switch] $GridView ) + priv_SetPreferences -Verbose + $eolConnectedCheck = priv_CheckEOLConnection $mboxCounter = 0 $arr_RoomDetails = @() - $eolConnectedCheck = priv_CheckEOLConnection - Set-Variable ProgressPreference Continue if (-not([string]::IsNullOrEmpty($folderCSV))) { $CSV = $True } if ($CSV) { $folder = priv_CheckFolder($folderCSV) } @@ -50,6 +50,8 @@ function Get-RoomsDetails { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } diff --git a/Gioxx.ToyBox/Statistics/GTB.Stats.ps1 b/Gioxx.ToyBox/Statistics/GTB.Stats.ps1 index f10552d..8532c9a 100644 --- a/Gioxx.ToyBox/Statistics/GTB.Stats.ps1 +++ b/Gioxx.ToyBox/Statistics/GTB.Stats.ps1 @@ -10,7 +10,7 @@ function Export-MboxStatistics { [switch] $Round ) - Set-Variable ProgressPreference Continue + priv_SetPreferences -Verbose $eolConnectedCheck = priv_CheckEOLConnection if ( $eolConnectedCheck -eq $true ) { @@ -76,6 +76,8 @@ function Export-MboxStatistics { } else { Write-Error "`nCan't connect or use Microsoft Exchange Online Management module. `nPlease check logs." } + + priv_RestorePreferences } function Export-MsolAccountSku { @@ -84,9 +86,9 @@ function Export-MsolAccountSku { [string] $folderCSV ) - Set-Variable ProgressPreference Continue - $folder = priv_CheckFolder($folderCSV) + priv_SetPreferences -Verbose $mggConnectedCheck = priv_CheckMGGraphModule + $folder = priv_CheckFolder($folderCSV) if ( $mggConnectedCheck -eq $true ) { $arr_MsolAccountSku = @() @@ -139,10 +141,10 @@ function Export-MsolAccountSku { $arr_MsolAccountSku | Export-CSV $CSV -NoTypeInformation -Encoding UTF8 -Delimiter ";" } else { - Write-Host "`nCan't connect or use Microsoft Graph modules. `nPlease check logs." -f "Red" + Write-Error "`nCan't connect or use Microsoft Graph modules. `nPlease check logs." } - + priv_RestorePreferences } # Export Modules and Aliases =======================================================================================================================================