AutoPilotModule.psm1

# Definieer standaardvariabelen
$script:DefaultOutputFilePath = "C:\AutoPilotHWID.csv"
$script:DefaultEmailFrom = "nas@onne.nl"
$script:DefaultEmailTo   = "info@onne.nl"
$script:DefaultSubject   = "Windows Autopilot Hardware Hash"
$script:DefaultBody      = "Zie bijlage voor de hardware-hash."
$script:DefaultMailServer       = "smtp.office365.com"
$script:DefaultMailServerPort   = "587"

function Get-AutoPilotHashAndSendMail {
    [CmdletBinding()]
    param(
        [string]$OutputFilePath = "$DefaultOutputFilePath",
        [string]$EmailFrom      = "$DefaultEmailFrom",
        [string]$EmailTo        = "$DefaultEmailTo",
        [string]$Subject        = "$DefaultSubject",
        [string]$Body           = "$DefaultBody",
        [string]$SMTPServer     = "$DefaultMailServer",
        [int]$SMTPPort          = "$DefaultMailServerPort"
    )

    # Controleer of het script Get-WindowsAutoPilotInfo.ps1 is geïnstalleerd
    if (-not (Get-Command Get-WindowsAutoPilotInfo.ps1 -ErrorAction SilentlyContinue)) {
        Write-Host "Get-WindowsAutoPilotInfo.ps1 wordt geïnstalleerd..."
        Install-Script -Name Get-WindowsAutoPilotInfo -Force
    }

    Write-Host "Het ophalen van de hardware-hash begint..."
    Get-WindowsAutoPilotInfo.ps1 -OutputFile $OutputFilePath

    # Controleer of het CSV-bestand is aangemaakt
    if (Test-Path $OutputFilePath) {
        Write-Host "Het CSV-bestand is succesvol aangemaakt: $OutputFilePath"
    }
    else {
        Write-Error "Er is iets misgegaan bij het aanmaken van het CSV-bestand."
        return
    }

    # E-mail versturen
    $Attachment = $OutputFilePath
    Write-Host "Versturen van e-mail met de hardware-hash..."
    
    # Vraag om Office 365-inloggegevens (bijv. nas@onne.nl)
    $Credential = Get-Credential

    Send-MailMessage -From $EmailFrom `
                     -To $EmailTo `
                     -Subject $Subject `
                     -Body $Body `
                     -SmtpServer $SMTPServer `
                     -Port $SMTPPort `
                     -UseSsl `
                     -Credential $Credential `
                     -Attachments $Attachment

    Write-Host "E-mail succesvol verzonden!"
}

Export-ModuleMember -Function Get-AutoPilotHashAndSendMail