Public/Repair-IntuneScript.ps1

function Repair-IntuneScript {
    <#
    .EXTERNALHELP IntuneScriptLab-Help.xml
    .SYNOPSIS
        Applies the mechanical fixes for findings that have one and reports the rest.
    #>

    [CmdletBinding(SupportsShouldProcess)]
    [OutputType('IntuneScriptLab.Repair')]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('FullName', 'PSPath')]
        [SupportsWildcards()]
        [string[]]$Path,

        [ValidateSet('Auto', 'Detection', 'Remediation', 'PlatformScript', 'Win32Detection', 'Win32Requirement')]
        [string]$ScriptType = 'Auto',

        [ValidateSet('Auto', 'System', 'User')]
        [string]$Context = 'Auto',

        [ValidateSet('Auto', 'x86', 'x64', 'arm64')]
        [string]$Architecture = 'Auto',

        [string[]]$IncludeRule,

        [string[]]$ExcludeRule,

        [switch]$EnforceSignatureCheck,

        [object]$Settings
    )

    begin {
        # SEC-16: checked here, not with [ValidateLength()], which would also refuse $null
        try {
            Assert-IslTextLength -Name 'IncludeRule' -Value $IncludeRule
            Assert-IslTextLength -Name 'ExcludeRule' -Value $ExcludeRule
        } catch {
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }

    process {
        # A path that is not there is reported, and the other paths are still repaired. It is tested
        # first and reported once: an error that Resolve-Path raised and this command caught would
        # also reach a caller's -ErrorVariable, three records for one path
        $files = foreach ($item in $Path) {
            if (-not (Test-Path -Path $item)) {
                $recordSplat = @{
                    Message = "Cannot find path '$item' because it does not exist."
                    ErrorId = 'PathNotFound'
                    Category = [System.Management.Automation.ErrorCategory]::ObjectNotFound
                    TargetObject = $item
                    ExceptionType = [System.Management.Automation.ItemNotFoundException]
                }
                $PSCmdlet.WriteError((New-IslErrorRecord @recordSplat))
                continue
            }
            try {
                $resolvedItems = @(Resolve-Path -Path $item -ErrorAction Stop)
            } catch {
                $PSCmdlet.WriteError($_)
                continue
            }
            foreach ($resolved in $resolvedItems) {
                if (Test-Path -LiteralPath $resolved.ProviderPath -PathType Container) {
                    Get-ChildItem -LiteralPath $resolved.ProviderPath -Recurse -Filter *.ps1 -File |
                        ForEach-Object { $_.FullName }
                } else { $resolved.ProviderPath }
            }
        }

        foreach ($file in $files) {
            # A script that cannot be analyzed or written is reported, and the next one is still
            # repaired
            try {
                # The analysis sees the script the way the caller deploys it, so the findings, their
                # fixes and the Remaining count are the ones Test-IntuneScript would give for the same
                # options
                # -ErrorAction Stop: the analysis reports a script it cannot read and goes on, and
                # with no findings this command would call the script repaired
                $testSplat = @{
                    Path = $file; ScriptType = $ScriptType; Context = $Context; Architecture = $Architecture
                    ErrorAction = 'Stop'
                }
                if ($EnforceSignatureCheck) { $testSplat.EnforceSignatureCheck = $true }
                if ($IncludeRule) { $testSplat.IncludeRule = $IncludeRule }
                if ($ExcludeRule) { $testSplat.ExcludeRule = $ExcludeRule }
                if ($PSBoundParameters.ContainsKey('Settings')) { $testSplat.Settings = $Settings }
                $findings = @(Test-IntuneScript @testSplat)
                $fixable = @($findings | Where-Object { $_.Fix })
                $fixes = [System.Collections.Generic.List[object]]::new()
                $written = $false

                if ($fixable) {
                    $bytes = [System.IO.File]::ReadAllBytes($file)
                    $encoding = if ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFF -and $bytes[1] -eq 0xFE) {
                        [System.Text.Encoding]::Unicode
                    } elseif ($bytes.Length -ge 2 -and $bytes[0] -eq 0xFE -and $bytes[1] -eq 0xFF) {
                        [System.Text.Encoding]::BigEndianUnicode
                    } elseif ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and
                        $bytes[2] -eq 0xBF) {
                        [System.Text.UTF8Encoding]::new($true)
                    } else {
                        # No BOM: UTF-8 when the bytes decode as such, otherwise the ANSI code page
                        # Windows PowerShell 5.1 reads it in (a UTF-8 decode would turn every non-ASCII
                        # character into U+FFFD and lose it for good)
                        try {
                            $null = [System.Text.UTF8Encoding]::new($false, $true).GetString($bytes)
                            [System.Text.UTF8Encoding]::new($false)
                        } catch { Get-IslOemEncoding -Kind ANSI }
                    }
                    $decoded = $encoding.GetString($bytes)
                    # A decode that does not give the same bytes back has lost characters, as an
                    # ANSI file does on a host whose system code page is UTF-8 (65001), where the
                    # fallback above is a UTF-8 decode too. Writing that text would make the loss
                    # permanent, so the file is left as it is
                    $lossless = [System.Convert]::ToBase64String($encoding.GetBytes($decoded)) -ceq
                    [System.Convert]::ToBase64String($bytes)
                    if (-not $lossless) {
                        # Only a file without a byte order mark was read in the host's fallback
                        # encoding; a file with one holds bytes its own encoding does not allow
                        $hasBom = $encoding.GetPreamble().Length -gt 0 -and
                        $bytes.Length -ge $encoding.GetPreamble().Length -and
                        [System.Convert]::ToBase64String($bytes, 0, $encoding.GetPreamble().Length) -ceq
                        [System.Convert]::ToBase64String($encoding.GetPreamble())
                        $reason = if ($hasBom) {
                            "it holds bytes that are not valid $($encoding.WebName), the encoding its byte " +
                            'order mark names, so it was not repaired. Correct those bytes, then run again'
                        } else {
                            "its bytes do not decode in $($encoding.WebName), the encoding this host reads a " +
                            'script without a byte order mark in, so it was not repaired. Save it as UTF-8 ' +
                            'with a byte order mark in an editor set to its code page, then run again'
                        }
                        Write-Warning "${file}: $reason"
                        $fixable = @()
                    }
                    $text = $decoded.TrimStart([char]0xFEFF)
                    $lineStarts = [System.Collections.Generic.List[int]]::new()
                    $lineStarts.Add(0)
                    for ($i = 0; $i -lt $text.Length; $i++) {
                        if ($text[$i] -eq "`n") { $lineStarts.Add($i + 1) }
                    }

                    # Text edits from the end of the file backwards, so earlier offsets stay valid
                    $edits = foreach ($finding in $fixable) {
                        if (-not $finding.Fix.ContainsKey('Replacement')) { continue }
                        if ($finding.Line -lt 1 -or $finding.Line -gt $lineStarts.Count) { continue }
                        $start = $lineStarts[$finding.Line - 1] + $finding.Column - 1
                        $before = "$($finding.Text)"
                        $current = if ($start + $before.Length -le $text.Length) {
                            $text.Substring($start, $before.Length)
                        } else { '' }
                        if ($current -ne $before) {
                            $place = "$file line $($finding.Line)"
                            Write-Warning "${place}: the text changed since the analysis; skipped"
                            continue
                        }
                        [pscustomobject]@{ Start = $start; Finding = $finding; Before = $before }
                    }
                    foreach ($edit in ($edits | Sort-Object Start -Descending)) {
                        $after = "$($edit.Finding.Fix.Replacement)"
                        $tail = $text.Substring($edit.Start + $edit.Before.Length)
                        $text = $text.Substring(0, $edit.Start) + $after + $tail
                        $fixes.Add([pscustomobject]@{
                                PSTypeName = 'IntuneScriptLab.Fix'
                                RuleName = $edit.Finding.RuleName
                                Line = $edit.Finding.Line
                                Before = $edit.Before
                                After = $after
                            })
                    }
                    $encodingFix = @($fixable | Where-Object { $_.Fix.ContainsKey('Encoding') }) |
                        Select-Object -First 1
                    $target = $encoding
                    if ($encodingFix) {
                        $target = [System.Text.UTF8Encoding]::new($true)
                        $fixes.Add([pscustomobject]@{
                                PSTypeName = 'IntuneScriptLab.Fix'
                                RuleName = $encodingFix.RuleName
                                Line = 0
                                Before = $encoding.WebName
                                After = 'utf-8 with BOM'
                            })
                    }

                    if ($fixes.Count -gt 0) {
                        $rules = @($fixes | ForEach-Object { $_.RuleName } | Sort-Object -Unique) -join ', '
                        if ($PSCmdlet.ShouldProcess($file, "Apply $($fixes.Count) fix(es): $rules")) {
                            [System.IO.File]::WriteAllText($file, $text, $target)
                            $written = $true
                        }
                    }
                }

                $remaining = if ($written) { @(Test-IntuneScript @testSplat).Count }
                else { $findings.Count - $fixes.Count }
                $repair = [pscustomobject]@{
                    PSTypeName = 'IntuneScriptLab.Repair'
                    Path = $file
                    Applied = $fixes.Count
                    Remaining = $remaining
                    Written = $written
                    Fixes = $fixes.ToArray()
                }
            } catch {
                $PSCmdlet.WriteError($_)
                continue
            }

            $repair
        }
    }
}