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) "
    }

    # Filter the output to get the most recent update for each recipient
    #Write-Host -ForegroundColor Cyan "Receiver [$Identity] has accepted copies of the shared calendar from the following recipients:"
    #$csvObject | Sort-Object { [DateTime]$_.Timestamp } -Descending | Sort-Object SharedCalendarOwner -Unique | Sort-Object { [DateTime]$_.Timestamp } -Descending | Format-Table -a Timestamp, SharedCalendarOwner, FolderName

    # Output the results to the console
    #Write-Host "Receiver [$Identity] has accepted copies of the shared calendar from the following recipients in the last 180 days:"
    #$csvObject | Where-Object { [DateTime]$_.Timestamp -gt (Get-Date).AddDays(-180) } | Format-Table -a Timestamp, SharedCalendarOwner, 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")
    }
    $csvObject | Where-Object { [DateTime]::Parse($_.Timestamp, $culture) -gt (Get-Date).AddDays(-180) } | 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

Review comment (suggestion):
We might want to include this in a `try` `catch` block to handle if we don't properly parse the `DateTime`.  #Resolved

Code after:
    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
    } catch {
        Write-Error "Ignore above error, No InternetCalendar logs found."
    }

    # check if the output is empty
    if ($null -eq $logOutput.MailboxLog) {