SHELL/1.3.3.ps1

$CheckId = "1.3.3"
$Title = "Ensure 'External sharing' of calendars is not available"
$Level = "L2"
$BenchmarkType = "Automated"
$AuditCommands = @(
    'Get-SharingPolicy -Identity "Default Sharing Policy" | Format-Table Name,Enabled'
)

try {
    if (-not (Get-Command -Name Get-SharingPolicy -ErrorAction SilentlyContinue)) {
        [pscustomobject]@{
            CheckId = $CheckId
            Title = $Title
            Level = $Level
            BenchmarkType = $BenchmarkType
            Status = "ERROR"
            Pass = $null
            Evidence = [pscustomobject]@{
                AuditCommands = $AuditCommands
                SourceDocument = "CIS_Microsoft_365_Foundations_Benchmark_v6.0.1"
            }
            Error = "Get-SharingPolicy cmdlet is unavailable in the current session."
            Timestamp = Get-Date
        }
        return
    }

    $Policy = $null
    try {
        $Policy = Get-SharingPolicy -Identity "Default Sharing Policy" -ErrorAction Stop
    }
    catch {
        $AllPolicies = @(Get-SharingPolicy -ErrorAction Stop)
        $Policy = @($AllPolicies | Where-Object {
                ([string]$_.Name -match '(?i)default')
            } | Select-Object -First 1)
        if (@($Policy).Count -gt 0) {
            $Policy = $Policy[0]
        }
        elseif ($AllPolicies.Count -gt 0) {
            $Policy = $AllPolicies[0]
        }
    }

    if ($null -eq $Policy) {
        [pscustomobject]@{
            CheckId = $CheckId
            Title = $Title
            Level = $Level
            BenchmarkType = $BenchmarkType
            Status = "FAIL"
            Pass = $false
            Evidence = [pscustomobject]@{
                AuditCommands = $AuditCommands
                SourceDocument = "CIS_Microsoft_365_Foundations_Benchmark_v6.0.1"
            }
            Error = "No sharing policy was returned."
            Timestamp = Get-Date
        }
        return
    }

    $Enabled = [bool]$Policy.Enabled
    $Pass = ($Enabled -eq $false)

    [pscustomobject]@{
        CheckId = $CheckId
        Title = $Title
        Level = $Level
        BenchmarkType = $BenchmarkType
        Status = if ($Pass) { "PASS" } else { "FAIL" }
        Pass = $Pass
        Evidence = [pscustomobject]@{
            AuditCommands = $AuditCommands
            PolicyIdentity = [string]$Policy.Identity
            Enabled = $Enabled
            RecommendedState = "Enabled = False"
            SourceDocument = "CIS_Microsoft_365_Foundations_Benchmark_v6.0.1"
        }
        Error = if ($Pass) { $null } else { "Default Sharing Policy is enabled." }
        Timestamp = Get-Date
    }
}
catch {
    [pscustomobject]@{
        CheckId = $CheckId
        Title = $Title
        Level = $Level
        BenchmarkType = $BenchmarkType
        Status = "ERROR"
        Pass = $null
        Evidence = [pscustomobject]@{
            AuditCommands = $AuditCommands
            SourceDocument = "CIS_Microsoft_365_Foundations_Benchmark_v6.0.1"
        }
        Error = $_.Exception.Message
        Timestamp = Get-Date
    }
}