Skip to content

Commit 2a95b22

Browse files
authored
Merge pull request #2502 from microsoft/Check_for_cmdlet
Check for cmdlet
2 parents b161f54 + 008773b commit 2a95b22

1 file changed

Lines changed: 41 additions & 29 deletions

File tree

Calendar/Check-SharingStatus.ps1

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,26 @@ function ProcessCalendarSharingAcceptLogs {
185185
}
186186

187187
Write-Host "Receiver [$Identity] has accepted copies of the shared calendar from the following recipients in the last 180 days:"
188-
if ($csvObject.Timestamp.Substring(0, 2) -gt 12) {
189-
Write-Verbose "Trying European DateTime Format - dd/MM/yyyy HH:mm:ss"
190-
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-GB")
191-
} else {
192-
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
188+
# Try to determine date format by examining timestamps (updated to deal with 2/6/2026 5:20:07 PM)
189+
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
190+
foreach ($entry in $csvObject) {
191+
$timestamp = $entry.Timestamp
192+
if ([string]::IsNullOrEmpty($timestamp)) { continue }
193+
194+
$monthOrDay = $timestamp.Split(" ")[0]
195+
if ([string]::IsNullOrEmpty($monthOrDay)) { continue }
196+
197+
$firstValue = $monthOrDay.Split("/")[0]
198+
if ([string]::IsNullOrEmpty($firstValue)) { continue }
199+
200+
$valueToTest = 0
201+
if ([int]::TryParse($firstValue, [ref]$valueToTest)) {
202+
if ($valueToTest -gt 12) {
203+
Write-Verbose "Looks like European DateTime Format - dd/MM/yyyy HH:mm:ss"
204+
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-GB")
205+
break
206+
}
207+
}
193208
}
194209

195210
try {
@@ -305,11 +320,6 @@ function GetOwnerInformation {
305320

306321
$script:OwnerMB | Format-List DisplayName, Database, ServerName, LitigationHoldEnabled, CalendarVersionStoreDisabled, CalendarRepairDisabled, RecipientType*
307322

308-
if ($null -eq $script:OwnerMB) {
309-
Write-Host -ForegroundColor Red "Could not find Owner Mailbox [$Owner]."
310-
exit
311-
}
312-
313323
Write-Host -ForegroundColor DarkYellow "Send on Behalf Granted to :"
314324
foreach ($del in $($script:OwnerMB.GrantSendOnBehalfTo)) {
315325
Write-Host -ForegroundColor Blue "`t$($del)"
@@ -335,7 +345,7 @@ function GetOwnerInformation {
335345
$OwnerCalendarPerms | Format-Table -a User, AccessRights, SharingPermissionFlags
336346

337347
# Warn if the size is greater than 1 GB
338-
if ([int]$OwnerCalendarStats[0].FolderSize.Split("(")[1].Replace(" bytes)", "") -gt 1000000000) {
348+
if ([int64]$OwnerCalendarStats[0].FolderSize.Split("(")[1].Replace(" bytes)", "").Replace(",", "") -gt 1000000000) {
339349
Write-Host -ForegroundColor Yellow "Warning: Owner Calendar size is greater than 1 GB. This can impact calendar performance."
340350
Write-Host -ForegroundColor Yellow "`t Consider archiving old calendar items or reducing the size of attachments in calendar items."
341351
}
@@ -383,22 +393,24 @@ function GetOwnerInformation {
383393
}
384394

385395
# cSpell:ignore Sharees
386-
Write-Host -ForegroundColor DarkYellow "`t Running 'Get-CalendarActiveSharingInformation -Identity "${Owner}:\$OwnerCalendarName"'"
387-
$OwnerActiveSharingInfo = Get-CalendarActiveSharingInformation -Identity "${Owner}:\$OwnerCalendarName"
388-
if ($OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees.count -gt 0) {
389-
Write-Host -ForegroundColor Green "`t Calendar has [$($OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees.count)] Active Receivers."
390-
$receivers = $OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees | ForEach-Object {
391-
[PSCustomObject]@{
392-
EmailAddress = $_.EmailAddress
393-
SharingPermissionFlag = ($_.SharingPermissionFlags -join ",")
394-
LastSyncTime = $_.LastSyncTime
396+
if (Get-Command -Name Get-CalendarActiveSharingInformation -ErrorAction SilentlyContinue) {
397+
Write-Host -ForegroundColor DarkYellow "`t Running 'Get-CalendarActiveSharingInformation -Identity "${Owner}:\$OwnerCalendarName"'"
398+
$OwnerActiveSharingInfo = Get-CalendarActiveSharingInformation -Identity "${Owner}:\$OwnerCalendarName"
399+
if ($OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees.count -gt 0) {
400+
Write-Host -ForegroundColor Green "`t Calendar has [$($OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees.count)] Active Receivers."
401+
$receivers = $OwnerActiveSharingInfo.ActiveShareesDataSet.Sharees | ForEach-Object {
402+
[PSCustomObject]@{
403+
EmailAddress = $_.EmailAddress
404+
SharingPermissionFlag = ($_.SharingPermissionFlags -join ",")
405+
LastSyncTime = $_.LastSyncTime
406+
}
395407
}
396-
}
397-
Write-Host -ForegroundColor DarkYellow "Look for the Receiver [$Receiver] in the list of Active Receivers."
408+
Write-Host -ForegroundColor DarkYellow "Look for the Receiver [$Receiver] in the list of Active Receivers."
398409

399-
$receivers | Format-Table -AutoSize EmailAddress, SharingPermissionFlag, LastSyncTime
400-
} else {
401-
Write-Host -ForegroundColor Yellow "`t Calendar has no Active Receivers according to Get-CalendarActiveSharingInformation."
410+
$receivers | Format-Table -AutoSize EmailAddress, SharingPermissionFlag, LastSyncTime
411+
} else {
412+
Write-Host -ForegroundColor Yellow "`t Calendar has no Active Receivers according to Get-CalendarActiveSharingInformation."
413+
}
402414
}
403415
Write-Host -ForegroundColor DarkYellow "`n`n`n------------------------------------------------"
404416
}
@@ -439,7 +451,7 @@ function GetReceiverInformation {
439451
$script:SharingType = "ExternalSharing"
440452
}
441453

442-
$OwnerCalendarName = $($OwnerMB.DisplayName)
454+
$OwnerCalendarName = $($script:OwnerMB.DisplayName)
443455
Write-Host -ForegroundColor Cyan "Receiver Calendar Folders (look for a copy of [$OwnerCalendarName] Calendar):"
444456
Write-Host -ForegroundColor Cyan "Running: 'Get-MailboxFolderStatistics -Identity $Receiver -FolderScope Calendar'"
445457
$CalStats = Get-MailboxFolderStatistics -Identity $Receiver -FolderScope Calendar
@@ -463,14 +475,14 @@ function GetReceiverInformation {
463475
}
464476

465477
# Note $Owner has a * at the end in case we have had multiple setup for the same user, they will be appended with a " 1", etc.
466-
if (($CalStats | Where-Object Name -Like $owner*) -or ($CalStats | Where-Object Name -Like "$($ownerMB.DisplayName)*" )) {
478+
if (($CalStats | Where-Object Name -Like $Owner*) -or ($CalStats | Where-Object Name -Like "$($script:OwnerMB.DisplayName)*" )) {
467479
Write-Host -ForegroundColor Green "Looks like we might have found a copy of the Owner Calendar in the Receiver Mailbox."
468480
Write-Host -ForegroundColor Green "This is a good indication the there is a Modern Sharing Relationship between these users."
469481
Write-Host -ForegroundColor Green "If the clients use the Modern Sharing or not is a up to the client."
470482
$script:ModernSharing = $true
471483

472-
$CalStats | Where-Object Name -Like $owner* | Format-Table -a FolderPath, ItemsInFolder, FolderAndSubfolderSize
473-
if (($CalStats | Where-Object Name -Like $owner*).count -gt 1) {
484+
$CalStats | Where-Object Name -Like $Owner* | Format-Table -a FolderPath, ItemsInFolder, FolderAndSubfolderSize
485+
if (($CalStats | Where-Object Name -Like $Owner*).count -gt 1) {
474486
Write-Host -ForegroundColor Yellow "Warning: Might have found more than one copy of the Owner Calendar in the Receiver Mailbox."
475487
}
476488
} else {

0 commit comments

Comments
 (0)