MyCorp.psm1

<#
========================================================================================
MYCORP MODULE
Rewritten from MyCorp by MyCorp Security Engineering Team
========================================================================================
DISCLAIMER
    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    PARTICULAR PURPOSE.
========================================================================================
#>


#Requires -Modules Pester, Microsoft.Graph.Authentication

#
# Initialize Module Variables
# NOTE: If you add new variables here, also update the Clear-ModuleVariable function
#
$__MyCorpSession = @{
    GraphCache       = @{ }
    GraphBaseUri     = $null
    TestResultDetail = @{ }
    Connections      = @()
    DnsCache         = @()
    ExoCache         = @{ }
    OrcaCache        = @{ }
}

# Create the MyCorp session variable in script scope
New-Variable -Name __MyCorpSession -Value $__MyCorpSession -Scope Script -Force

# -------------------------------------------------------------------
# Compatibility mapping
# -------------------------------------------------------------------
# Many existing internal scripts (the Mt* engine) reference $__MtSession.
# You requested that we do NOT change Mt* code. To keep Mt* code unchanged
# while using the MyCorp branding, create a script-scope alias variable
# $__MtSession that references the same hashtable as $__MyCorpSession.
# This preserves behavior without editing Mt* files.
# -------------------------------------------------------------------
if (-not (Get-Variable -Name __MtSession -Scope Script -ErrorAction SilentlyContinue)) {
    # Point $__MtSession to the same object as $__MyCorpSession for compatibility.
    New-Variable -Name __MtSession -Value $__MyCorpSession -Scope Script -Force
} else {
    # If $__MtSession already exists, make sure it points to the same hashtable reference
    Set-Variable -Name __MtSession -Value $__MyCorpSession -Scope Script -Force
}

#
# Load private and public scripts
#
# Keep the dynamic import but make it robust: resolve PSScriptRoot and import each script,
# writing an explicit error if a given file fails to load.
#
$scriptRoot = $PSScriptRoot
if (-not $scriptRoot) {
    $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
}

$privatePath = Join-Path -Path $scriptRoot -ChildPath 'internal'
$publicPath  = Join-Path -Path $scriptRoot -ChildPath 'public'

$privateScripts = @()
$publicScripts  = @()

if (Test-Path -Path $privatePath) {
    $privateScripts = Get-ChildItem -Path $privatePath -Recurse -Filter "*.ps1" -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer }
}

if (Test-Path -Path $publicPath) {
    $publicScripts = Get-ChildItem -Path $publicPath -Recurse -Filter "*.ps1" -ErrorAction SilentlyContinue | Where-Object { -not $_.PSIsContainer }
}

foreach ($script in ($privateScripts + $publicScripts)) {
    try {
        # Dot-source each script so functions/variables are imported into this module's script scope
        . "$($script.FullName)"
    }
    catch {
        Write-Error ("Failed to import script {0}: {1}" -f $script.FullName, ($_ | Out-String).Trim())
    }
}

#
# Load Module Manifest (rebranded)
#
$manifestPath = Join-Path -Path $scriptRoot -ChildPath 'MyCorp.psd1'
if (Test-Path -Path $manifestPath) {
    try {
        if (Get-Command -Name Import-PowerShellDataFile -ErrorAction SilentlyContinue) {
            $ModuleInfo = Import-PowerShellDataFile -Path $manifestPath
        } else {
            try {
                $ModuleInfo = Get-Content -Path $manifestPath -Raw | ConvertFrom-Json
            } catch {
                Write-Warning "Failed to read module manifest at '$manifestPath': $($_ | Out-String)"
            }
        }
    } catch {
        Write-Warning "Failed to import module manifest at '$manifestPath': $($_.Exception.Message)"
    }
} else {
    Write-Verbose "Module manifest '$manifestPath' not found. Continuing without import."
}

#
# End of Module Initialization
#