Public/Initialize-HydrationLogging.ps1

function Initialize-HydrationLogging {
    <#
    .SYNOPSIS
        Initializes logging for the hydration session
    .DESCRIPTION
        Sets up the logging infrastructure for a hydration run, creating the log directory
        and session log file. Configures both console and file logging with timestamps.
    .PARAMETER LogPath
        Path to write log files. Defaults to OS temp directory under IntuneHydrationKit/Logs
    .PARAMETER EnableVerbose
        Enable verbose logging
    .EXAMPLE
        Initialize-HydrationLogging
        # Uses default temp path: $env:TEMP/IntuneHydrationKit/Logs (Windows) or /tmp/IntuneHydrationKit/Logs (macOS/Linux)
    .EXAMPLE
        Initialize-HydrationLogging -LogPath "./MyLogs"
        # Uses custom path
    #>

    [CmdletBinding()]
    param(
        [Parameter()]
        [string]$LogPath,

        [Parameter()]
        [switch]$EnableVerbose
    )

    # Set default log path to OS-appropriate temp directory if not specified
    if (-not $LogPath) {
        $tempBase = [System.IO.Path]::GetTempPath()
        $LogPath = Join-Path -Path $tempBase -ChildPath 'IntuneHydrationKit/Logs'
    }

    if (-not (Test-Path -Path $LogPath)) {
        # Always create log directory regardless of -WhatIf (logging is observational, not a tenant change)
        New-Item -Path $LogPath -ItemType Directory -Force -WhatIf:$false | Out-Null
    }

    $script:LogPath = $LogPath
    $script:VerboseLogging = $EnableVerbose

    $timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
    $script:CurrentLogFile = Join-Path -Path $LogPath -ChildPath "hydration-$timestamp.log"

    # Clear existing log file
    if (Test-Path -Path $script:CurrentLogFile) {
        Clear-Content -Path $script:CurrentLogFile
    }

    Write-HydrationLog -Message "Logging initialized at: $($script:CurrentLogFile)" -Level Info
}