Private/InitializeModule.ps1

param(
    [switch] $Silent,
    [string[]] $lsMigrationToolsHelperConfigFile = @(),
    [string] $moduleName,
    [string[]] $moduleDependencies = @()
)

Set-StrictMode -Version 2.0

$verbosePreference = "SilentlyContinue"
$warningPreference = 'Continue'
$errorActionPreference = 'Stop'

if (!([environment]::Is64BitProcess)) {
    throw "ContainerHelper cannot run in Windows PowerShell (x86), need 64bit mode"
}

$isPsCore = $PSVersionTable.PSVersion -ge "6.0.0"
if ($isPsCore) {
    $byteEncodingParam = @{ "asByteStream" = $true }
    $allowUnencryptedAuthenticationParam = @{ "allowUnencryptedAuthentication" = $true }
}
else {
    $byteEncodingParam = @{ "Encoding" = "byte" }
    $allowUnencryptedAuthenticationParam = @{ }
    $isWindows = $true
    $isLinux = $false
    $IsMacOS = $false
}
$myUsername = (whoami)
if ($isWindows) {
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    $isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
else {
    $isAdministrator = ((id -u) -eq 0)
}

# $LSMigrationToolsHelperVersion = (Get-Content (Join-Path $PSScriptRoot "Version.txt")).Trim()
# if (!$silent) {
# Write-Host "$($moduleName.SubString(0,$moduleName.Length-5)) version $LSMigrationToolsHelperVersion"
# }

if ($isWindows) {
    $programDataFolder = 'C:\ProgramData\LSMigrationTools'
}
elseif ($isMacOS) {
    $programDataFolder = "/Users/$myUsername/.lsmigrationtools"
}
else {
    $programDataFolder = "/home/$myUsername/.lsmigrationtools"
}

function Get-LSMigrationToolsHelperConfig {
    if (!((Get-Variable -scope Script lsMigrationToolsHelperConfig -ErrorAction SilentlyContinue) -and $lsMigrationToolsHelperConfig)) {
        Set-Variable -scope Script -Name lsMigrationToolsHelperConfig -Value @{
            "LSRetailTelemetryConnectionString" = "InstrumentationKey=124ff8f6-2450-4092-a2f0-c985316a70a6;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/"
            "PartnerTelemetryConnectionString" = ""
            "SendExtendedTelemetryToLSRetail" = $false
        }

        Export-ModuleMember -Variable lsMigrationToolsHelperConfig
    }
    return $lsMigrationToolsHelperConfig
}


Get-LSMigrationToolsHelperConfig | Out-Null

$telemetry = @{
    "Assembly" = $null
    "LSRetailClient" = $null
    "CorrelationId" = ""
    "TopId" = ""
    "Debug" = $false
}
try {
    if (($lsMigrationToolsHelperConfig.LSRetailTelemetryConnectionString) -and !$Silent) {
        Write-Host -ForegroundColor Green "LS Migration Tools emits usage statistics telemetry to LS Retail"
    }
    # $dllPath = Join-Path $configHelperFolder 'Microsoft.ApplicationInsights.dll'
    $dllPath = Join-Path $PSScriptRoot "..\Microsoft.ApplicationInsights.dll"
    # $dllPath = Join-Path $PSScriptRoot "..\Microsoft.ApplicationInsights.2.32.0.429.dll"
    # if (-not (Test-Path $dllPath)) {
    # Copy-Item (Join-Path $PSScriptRoot "..\Microsoft.ApplicationInsights.dll") -Destination $dllPath
    # }
    $telemetry.Assembly = [System.Reflection.Assembly]::LoadFrom($dllPath)
} catch {
    if (!$Silent) {
        Write-Host -ForegroundColor Yellow "Unable to load ApplicationInsights.dll"
    }
}

. (Join-Path $PSScriptRoot "TelemetryHelper.ps1")

# Telemetry functions
Export-ModuleMember -Function RegisterTelemetryScope
Export-ModuleMember -Function InitTelemetryScope
Export-ModuleMember -Function AddTelemetryProperty
Export-ModuleMember -Function TrackTrace
Export-ModuleMember -Function TrackException

# $depVersion = "0.0"

# $moduleDependencies | ForEach-Object {
# $module = Get-Module $_
# if ($module -ne $null -and $module.Version -lt [Version]$depVersion) {
# Write-Error "Module $_ is already loaded in version $($module.Version). This module requires version $depVersion. Please reinstall BC modules."
# }
# elseif ($module -eq $null) {
# if ($isInsider) {
# Import-Module (Join-Path $PSScriptRoot "$_.psm1") -DisableNameChecking -Global -ArgumentList $silent,$lsMigrationToolsHelperConfigFile
# }
# else {
# Import-Module $_ -MinimumVersion $depVersion -DisableNameChecking -Global -ArgumentList $silent,$lsMigrationToolsHelperConfigFile
# }
# }
# }