Repository: microsoft/CSS-Exchange
File: Calendar/Check-SharingStatus.ps1 (PowerShell)

Code before:
    $csvString = $csvString.Replace("calendar name ", "")

    # Convert the CSV string to an object
    $csvObject = $csvString | ConvertFrom-Csv

    # Access the values as properties of the object
    foreach ($row in $csvObject) {
        Write-Debug "$($row.Timestamp) - $($row.SharedCalendarOwner) - $($row.FolderName) "
    }

    Write-Host "Receiver [$Identity] has accepted copies of the shared calendar from the following recipients in the last 180 days:"
    if ($csvObject.Timestamp.Substring(0, 2) -gt 12) {
        Write-Verbose "Trying European DateTime Format - dd/MM/yyyy HH:mm:ss"
        $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-GB")
    } else {
        $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
    }

    try {
        $csvObject | Where-Object { [DateTime]::Parse($_.Timestamp, $culture) -gt (Get-Date).AddDays(-180) } | Format-Table -a Timestamp, SharedCalendarOwner, FolderName
    } catch {
        Write-Error "Error parsing dates in the log entries.  Outputting all entries without date filtering."
        $csvObject |  Format-Table -a Timestamp, SharedCalendarOwner, FolderName
    }
}

<#
.SYNOPSIS
    Formats the InternetCalendar logs from Export-MailboxDiagnosticLogs for a given identity.
.DESCRIPTION
    This function processes calendar sharing invite logs.
.PARAMETER Identity
    The SMTP Address for which to process calendar sharing accept logs.
#>
function ProcessInternetCalendarLogs {
    param (
        [string]$Identity
    )

    # Define the header row
    $header = "Timestamp", "Mailbox", "SyncDetails", "PublishingUrl", "RemoteFolderName", "LocalFolderId", "Folder"

    $csvString = @()
    $csvString = $header -join ","
    $csvString += "`n"

    try {
        # Call the Export-MailboxDiagnosticLogs cmdlet and store the output in a variable
        # -ErrorAction is not supported on Export-MailboxDiagnosticLogs
        # $logOutput = Export-MailboxDiagnosticLogs $Identity -ComponentName AcceptCalendarSharingInvite -ErrorAction SilentlyContinue
        $logOutput = Export-MailboxDiagnosticLogs $Identity -ComponentName InternetCalendar

Review comment (suggestion):
The problem with this logic is it doesn't account for dates at the beginning of the of month. 

Example: Feb 11th, 2024 -> 11/02/2024 in European format. 

Would these time stamps be determined by the mailbox's current settings at the time of the entry created by what is set at `Get-MailboxRegionalConfiguration`? Would it be better to get that information and try to parse the events this way?  #WontFix

Code after:
    $csvString = $csvString.Replace("calendar name ", "")

    # Convert the CSV string to an object
    $csvObject = $csvString | ConvertFrom-Csv

    # Access the values as properties of the object
    foreach ($row in $csvObject) {
        Write-Debug "$($row.Timestamp) - $($row.SharedCalendarOwner) - $($row.FolderName) "
    }

    Write-Host "Receiver [$Identity] has accepted copies of the shared calendar from the following recipients in the last 180 days:"
    # Try to determine date format by examining timestamps (updated to deal with 2/6/2026 5:20:07 PM)
    $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-US")
    foreach ($entry in $csvObject) {
        $timestamp = $entry.Timestamp
        if ([string]::IsNullOrEmpty($timestamp)) { continue }

        $monthOrDay = $timestamp.Split(" ")[0]
        if ([string]::IsNullOrEmpty($monthOrDay)) { continue }

        $firstValue = $monthOrDay.Split("/")[0]
        if ([string]::IsNullOrEmpty($firstValue)) { continue }

        $valueToTest = 0
        if ([int]::TryParse($firstValue, [ref]$valueToTest)) {
            if ($valueToTest -gt 12) {
                Write-Verbose "Looks like European DateTime Format - dd/MM/yyyy HH:mm:ss"
                $culture = [System.Globalization.CultureInfo]::CreateSpecificCulture("en-GB")
                break
            }
        }
    }

    try {
        $csvObject | Where-Object { [DateTime]::Parse($_.Timestamp, $culture) -gt (Get-Date).AddDays(-180) } | Format-Table -a Timestamp, SharedCalendarOwner, FolderName
    } catch {
        Write-Error "Error parsing dates in the log entries.  Outputting all entries without date filtering."
        $csvObject |  Format-Table -a Timestamp, SharedCalendarOwner, FolderName
    }
}

<#
.SYNOPSIS
    Formats the InternetCalendar logs from Export-MailboxDiagnosticLogs for a given identity.
.DESCRIPTION
    This function processes calendar sharing invite logs.
.PARAMETER Identity
    The SMTP Address for which to process calendar sharing accept logs.
#>
function ProcessInternetCalendarLogs {
    param (