# BG Builders - Check and fix inbox rules on lesley shared mailbox
# Run from interactive PowerShell

$lesleyUPN = "lesley@bgbuildersllc.com"

Write-Output "Connecting to Exchange Online..."
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName "sysadmin@bgbuildersllc.com" -ShowBanner:$false
Write-Output "[OK] Connected"

# Check inbox rules
Write-Output "`n=== INBOX RULES ==="
$rules = Get-InboxRule -Mailbox $lesleyUPN -IncludeHidden
if ($rules) {
    foreach ($rule in $rules) {
        Write-Output "  Rule: $($rule.Name) | Enabled: $($rule.Enabled) | Priority: $($rule.Priority)"
        Write-Output "    Description: $($rule.Description)"
        Write-Output "    MoveToFolder: $($rule.MoveToFolder)"
        Write-Output "    DeleteMessage: $($rule.DeleteMessage)"
        Write-Output "    SoftDeleteMessage: $($rule.SoftDeleteMessage)"
        Write-Output ""
    }

    # Disable any rules that delete messages
    foreach ($rule in $rules) {
        if ($rule.DeleteMessage -or $rule.SoftDeleteMessage -or $rule.MoveToFolder -match "Deleted") {
            Write-Output "[ALERT] Removing problematic rule: $($rule.Name)"
            Remove-InboxRule -Mailbox $lesleyUPN -Identity $rule.Identity -Confirm:$false
            Write-Output "[OK] Removed"
        }
    }
} else {
    Write-Output "  [OK] No inbox rules found"
}

# Check sweep rules
Write-Output "`n=== SWEEP RULES ==="
try {
    $sweep = Get-SweepRule -Mailbox $lesleyUPN
    if ($sweep) {
        foreach ($s in $sweep) {
            Write-Output "  Rule: $($s.Name) | Enabled: $($s.Enabled)"
            Write-Output "    SourceFolder: $($s.SourceFolder)"
            Write-Output "    DestFolder: $($s.DestFolder)"
            Write-Output "    KeepLatest: $($s.KeepLatest)"
            Write-Output ""
        }
        # Remove sweep rules
        foreach ($s in $sweep) {
            Write-Output "[ALERT] Removing sweep rule: $($s.Name)"
            Remove-SweepRule -Identity $s.Identity -Mailbox $lesleyUPN -Confirm:$false
            Write-Output "[OK] Removed"
        }
    } else {
        Write-Output "  [OK] No sweep rules found"
    }
} catch {
    Write-Output "  [INFO] Sweep rules not available: $_"
}

# Check mailbox type and forwarding
Write-Output "`n=== MAILBOX STATUS ==="
$mb = Get-Mailbox -Identity $lesleyUPN
Write-Output "  Type: $($mb.RecipientTypeDetails)"
Write-Output "  Forwarding: $($mb.ForwardingAddress)"
Write-Output "  ForwardingSMTP: $($mb.ForwardingSmtpAddress)"
Write-Output "  DeliverToMailboxAndForward: $($mb.DeliverToMailboxAndForward)"
Write-Output "  HiddenFromGAL: $($mb.HiddenFromAddressListsEnabled)"
Write-Output "  LitigationHold: $($mb.LitigationHoldEnabled)"

# Check transport rules affecting this mailbox
Write-Output "`n=== TRANSPORT RULES ==="
$transport = Get-TransportRule | Where-Object { $_.State -eq "Enabled" }
if ($transport) {
    foreach ($t in $transport) {
        Write-Output "  Rule: $($t.Name) | Priority: $($t.Priority)"
    }
} else {
    Write-Output "  [OK] No transport rules"
}

Disconnect-ExchangeOnline -Confirm:$false
Write-Output "`n[OK] Done"
