OmadaSqlTroubleShooter.psm1

#######################################################################################################################################################
# WARNING: DO NOT EDIT THIS FILE AS IT IS GENERATED AND WILL BE OVERWRITTEN ON THE NEXT UPDATE! #
# #
# Generated via psake on: 2026-09-17T06:11:17.796Z #
# Version: 2026.9.17.94 #
# Copyright Fortigi (C) 2024-2026 #
#######################################################################################################################################################


#requires -Version 7.0

[CmdLetBinding()]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'WebViewInstalled', Justification = 'The variable is used, but script analyzer does not recognize it')]
param()

try {
    $Script:Tracer = [System.Diagnostics.Trace]
    $Script:Tracer::AutoFlush = $true

    $ModuleName = "OmadaSqlTroubleshooter"
    "Loading {0} Module" -f $ModuleName | Write-Verbose

    $Script:ModuleVersion = "2026.09.17.94"

    if (-not (Test-Path "$PSScriptRoot\Lib\Functions\Public" -PathType Container)) {
        $Public = @(Get-ChildItem "$PsscriptRoot\Lib\Functions\Functions.ps1")
    }
    else {
        $Public = @(Get-ChildItem -Path $PSScriptRoot\Lib\Functions\Public\*.ps1 -Recurse | Where-Object { $_.Name -notlike "_*.ps1" })
    }
    if (-not(Test-Path "$PSScriptRoot\Lib\Functions\Private" -PathType Container)) {
        $Private = @()
    }
    else {
        $Private = @(Get-ChildItem -Path $PSScriptRoot\Lib\Functions\Private\*.ps1 -Recurse | Where-Object { $_.Name -notlike "_*.ps1" })
    }
    foreach ($Import in @($Public + $Private)) {
        try {
            . $Import.FullName
        }
        catch {
            "Failed to import function {0}: {1}" -f $($Import.FullName), $_ | Write-Error -ErrorAction "Stop"
        }
    }


    $MinimumOmadaWebPSVersion = "2026.07.09.9"
    if ($MinimumOmadaWebPSVersion -ne "0.0") {
        Import-OmadaWebPSModule -MinimumVersion $MinimumOmadaWebPSVersion
    }

    $Script:DependencyLockPath = Join-Path $PSScriptRoot -ChildPath "DependencyLock.psd1"

    $LocalAppDataPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)
    $Script:ModuleAppDataPath = (New-Item (Join-Path $LocalAppDataPath -ChildPath $ModuleName) -ItemType Directory -Force).FullName
    $BinPath = (New-Item (Join-Path $Script:ModuleAppDataPath -ChildPath "Bin") -ItemType Directory -Force).FullName

    try {
        $WebBinBasePath = New-Item  $BinPath -ItemType Directory -Force
    }
    catch {}

    "{0} - Set paths" -f $MyInvocation.MyCommand | Write-Verbose

    $ResolvedWebView2Path = Resolve-WebView2AssemblyPath -ModuleRoot $PSScriptRoot -BinPath $WebBinBasePath

    $Script:WebView2CorePath = $ResolvedWebView2Path.Core
    $Script:WebView2WinFormsPath = $ResolvedWebView2Path.WinForms
    $Script:WebView2WpfPath = $ResolvedWebView2Path.Wpf
    $Script:WebView2LoaderPath = $ResolvedWebView2Path.Loader

    $Script:WebView2Source = $ResolvedWebView2Path.Source

    $Script:WebView2BasePath = $ResolvedWebView2Path.BasePath

    "{0} - WebView2 assemblies resolved from the {1} at '{2}'" -f $MyInvocation.MyCommand, $ResolvedWebView2Path.Source.ToLowerInvariant(), $ResolvedWebView2Path.BasePath | Write-Verbose

    if ($Env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
        $WebView2DownloadPath = [System.IO.Path]::Combine($WebBinBasePath, "win-x64")
    }
    else {
        $WebView2DownloadPath = [System.IO.Path]::Combine($WebBinBasePath, "win-x86")
    }
    $Script:WebView2StampPath = [System.IO.Path]::Combine($WebView2DownloadPath, "WebView2.pin")

    if ($ResolvedWebView2Path.RequiresInstall) {
        New-Item -ItemType Directory -Path $WebView2DownloadPath -Force | Out-Null
    }

    $Script:ScriptDomPath = [System.IO.Path]::Combine($WebBinBasePath, "Microsoft.SqlServer.TransactSql.ScriptDom.dll")
    "{0} - {1}" -f $MyInvocation.MyCommand, $Script:ScriptDomPath | Write-Verbose

    $Script:ScriptDomStampPath = [System.IO.Path]::Combine($WebBinBasePath, "ScriptDom.pin")
    "{0} - {1}" -f $MyInvocation.MyCommand, $Script:ScriptDomStampPath | Write-Verbose

    $Script:WebView2UserProfileBasePath = [System.IO.Path]::Combine($Script:ModuleAppDataPath, "Edge User Data")

    $Script:WebView2UserProfilePath = [System.IO.Path]::Combine($Script:ModuleAppDataPath, "Edge User Data\OmadaWebView2Profile")
    "{0} - {1}" -f $MyInvocation.MyCommand, $Script:WebView2UserProfilePath | Write-Verbose

    if ($ResolvedWebView2Path.RequiresInstall) {
        $WebViewInstalled = Install-WebView2 -IncludeWpf
    }
    else {
        $WebViewInstalled = $true
    }


    if (!$WebViewInstalled) {
        "Cannot start module because the Microsoft Edge Webview2 RunTime was not found. You can download it from: https://developer.microsoft.com/en-us/microsoft-edge/webview2?form=MA13LH#download-section. When you are not able to install it, you can also add the Webview2 Fixed Version binaries to folder {0}" -f (Join-Path $WebBinBasePath -ChildPath "$ModuleName\bin\Webview2Runtime") | Write-Error -ErrorAction "Stop"
    }

    "Validate version" | Write-Verbose
    Show-ModuleUpdateNotification -ModuleName $ModuleName

    Test-Shortcut


}
catch {
    throw
}