Start-IntuneAI.ps1

<#
.SYNOPSIS
    Start script for AI Enterprise Intune Compliance Module
     
.DESCRIPTION
    This script imports the module and starts the interactive menu.
    Can be run from the module directory or from the parent directory.
     
.EXAMPLE
    .\Start-IntuneAI.ps1
     
.EXAMPLE
    cd D:\Github-NL-Baseline\ai-intune-master\ai-enterprise-intune-compliance
    .\Start-IntuneAI.ps1
#>


# Get the script directory
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path

# Try to find the module in different locations
$modulePath = $null

# Option 1: Module in same directory as script (development mode)
$localModulePath = Join-Path $scriptPath "ai-enterprise-intune-compliance"
if (Test-Path $localModulePath) {
    $modulePath = $localModulePath
}

# Option 2: Module installed in PowerShell modules directory
if (-not $modulePath) {
    $installedModule = Get-Module -ListAvailable -Name "ai-enterprise-intune-compliance" | Select-Object -First 1
    if ($installedModule) {
        $modulePath = $installedModule.ModuleBase
    }
}

# Option 3: Try standard PowerShell module paths
if (-not $modulePath) {
    $modulePaths = @(
        "$env:USERPROFILE\Documents\WindowsPowerShell\Modules\ai-enterprise-intune-compliance",
        "$env:USERPROFILE\Documents\PowerShell\Modules\ai-enterprise-intune-compliance",
        "$env:ProgramFiles\WindowsPowerShell\Modules\ai-enterprise-intune-compliance",
        "$env:ProgramFiles\PowerShell\Modules\ai-enterprise-intune-compliance"
    )
    
    foreach ($path in $modulePaths) {
        if (Test-Path $path) {
            $modulePath = $path
            break
        }
    }
}

# If still not found, show error
if (-not $modulePath) {
    Write-Error "Module 'ai-enterprise-intune-compliance' not found."
    Write-Host ""
    Write-Host "Please install the module first:" -ForegroundColor Yellow
    Write-Host "1. Copy the 'ai-enterprise-intune-compliance' folder to one of these locations:" -ForegroundColor Gray
    Write-Host " - $env:USERPROFILE\Documents\WindowsPowerShell\Modules\" -ForegroundColor White
    Write-Host " - $env:USERPROFILE\Documents\PowerShell\Modules\" -ForegroundColor White
    Write-Host ""
    Write-Host "2. Or run this script from the module directory." -ForegroundColor Gray
    exit 1
}

# Check and load required modules first
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " AI Enterprise Intune Compliance" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Check for required modules
$requiredModules = @(
    "Microsoft.Graph.Authentication",
    "Microsoft.Graph.DeviceManagement",
    "Microsoft.Graph.Identity.SignIns"
)

$missingModules = @()
foreach ($moduleName in $requiredModules) {
    $module = Get-Module -ListAvailable -Name $moduleName | Select-Object -First 1
    if (-not $module) {
        $missingModules += $moduleName
    }
}

if ($missingModules.Count -gt 0) {
    Write-Host "Missing required modules:" -ForegroundColor Red
    foreach ($module in $missingModules) {
        Write-Host " - $module" -ForegroundColor Yellow
    }
    Write-Host ""
    Write-Host "Install missing modules with:" -ForegroundColor Yellow
    foreach ($module in $missingModules) {
        Write-Host " Install-Module -Name $module -Force -AllowClobber" -ForegroundColor White
    }
    exit 1
}

# Remove any existing loaded versions to avoid conflicts
Write-Host "Preparing module environment..." -ForegroundColor Gray
foreach ($moduleName in $requiredModules) {
    $loaded = Get-Module -Name $moduleName -ErrorAction SilentlyContinue
    if ($loaded) {
        Write-Host " Removing existing $moduleName module..." -ForegroundColor Gray
        Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
    }
}

# Import required modules
Write-Host "Loading required modules..." -ForegroundColor Gray
try {
    foreach ($moduleName in $requiredModules) {
        Write-Host " Loading $moduleName..." -ForegroundColor Gray
        Import-Module -Name $moduleName -Force -ErrorAction Stop
    }
    Write-Host "Required modules loaded successfully." -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Error "Failed to load required modules: $_"
    Write-Host ""
    Write-Host "Try removing and reinstalling the modules:" -ForegroundColor Yellow
    Write-Host " Get-Module -Name Microsoft.Graph.* | Remove-Module -Force" -ForegroundColor White
    Write-Host " Install-Module -Name Microsoft.Graph.Authentication -Force -AllowClobber" -ForegroundColor White
    Write-Host " Install-Module -Name Microsoft.Graph.DeviceManagement -Force -AllowClobber" -ForegroundColor White
    Write-Host " Install-Module -Name Microsoft.Graph.Identity.SignIns -Force -AllowClobber" -ForegroundColor White
    exit 1
}

# Import the main module
try {
    Write-Host "Loading module from: $modulePath" -ForegroundColor Gray
    Write-Host ""
    
    # Remove module if already loaded
    $loadedModule = Get-Module -Name "ai-enterprise-intune-compliance" -ErrorAction SilentlyContinue
    if ($loadedModule) {
        Remove-Module -Name "ai-enterprise-intune-compliance" -Force -ErrorAction SilentlyContinue
    }
    
    Import-Module $modulePath -Force -ErrorAction Stop
    Write-Host "Module loaded successfully." -ForegroundColor Green
    Write-Host ""
} catch {
    Write-Error "Failed to import module: $_"
    Write-Host ""
    Write-Host "Troubleshooting:" -ForegroundColor Yellow
    Write-Host "1. Check that the module files are complete." -ForegroundColor Gray
    Write-Host " Required files:" -ForegroundColor Gray
    Write-Host " - ai-enterprise-intune-compliance.psd1" -ForegroundColor White
    Write-Host " - ai-enterprise-intune-compliance.psm1" -ForegroundColor White
    Write-Host ""
    Write-Host "2. If assembly conflicts persist, restart PowerShell and try again." -ForegroundColor Gray
    exit 1
}

# Start the interactive menu
try {
    Start-IntuneAIMenu
} catch {
    Write-Error "Failed to start menu: $_"
    Write-Host ""
    Write-Host "If this is the first time running, initialize the module first:" -ForegroundColor Yellow
    Write-Host " Initialize-IntuneAIModule" -ForegroundColor White
    exit 1
}