Public/Start-ssTranscript.ps1

function Start-ssTranscript {
    <#
    .SYNOPSIS
    Starts a transcript log with timestamp, script name, and version.
 
    .DESCRIPTION
    This function starts a structured enterprise transcript and ensures logs are written
    to the standardised Intune Management Extension log directory:
 
        C:\ProgramData\Microsoft\IntuneManagementExtension\Logs
 
    If the destination directory does not exist, it is automatically created.
    This guarantees idempotent and self-healing behaviour in deployment scenarios.
 
    The transcript filename is constructed using the format:
 
        ScriptName-ScriptVersion-yyyyMMdd-HHmmss.log
 
    The function then starts a transcript using Start-Transcript and returns the
    full path to the active log file.
 
    At script end, call Stop-ssTranscript to safely close the transcript.
 
    .PARAMETER ScriptName
    Name of the script for inclusion in the log filename.
 
    .PARAMETER ScriptVersion
    Version of the script for inclusion in the log filename.
 
    .OUTPUTS
    System.String
    Returns the full path to the active transcript log file.
 
    .EXAMPLE
    Start-ssTranscript -ScriptName 'MyScript' -ScriptVersion '1.2.3'
 
    # ... script body ...
    Stop-ssTranscript
 
    .NOTES
    Author: owen.heaume
    Version: 1.0 - Initial release
    #>

    param (
        [Parameter(Mandatory)]
        [string]$ScriptName,

        [Parameter(Mandatory)]
        [string]$ScriptVersion
    )

    # Define log folder
    $logFolder = 'C:\ProgramData\Microsoft\IntuneManagementExtension\Logs'

    # Ensure log folder exists
    if (-not (Test-Path -Path $logFolder -PathType Container)) {
        try {
            New-Item -Path $logFolder -ItemType Directory -Force | Out-Null
        } catch {
            throw "Failed to create log folder '$logFolder': $($_.Exception.Message)"
        }
    }

    # Create timestamp
    $timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'

    # Construct log file path
    $logFile = Join-Path -Path $logFolder -ChildPath "$($ScriptName)-$($ScriptVersion)-$timestamp.log"

    # Start transcript
    try {
        Start-Transcript -Path $logFile -Force
    } catch {
        throw "Failed to start transcript at '$logFile': $($_.Exception.Message)"
    }

    # Log header
    Write-Host "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] Starting $ScriptName v$ScriptVersion" -ForegroundColor Cyan

    return $logFile
}