# Generated by ImpTune — Printer: Imprimante Evry
# Install command: powershell.exe -NoProfile -ExecutionPolicy Bypass -File "install.ps1"
#
# This script installs the printer "Imprimante Evry" via Intune Win32 app deployment.
# It must be launched with -File (not -Command) so $PSScriptRoot is populated.

# ---------------------------------------------------------------------------
# WOW64 Guard — relaunch in 64-bit PowerShell if running under WOW64 (Intune
# runs Win32 app scripts in a 32-bit process; pnputil is 64-bit only).
# This block MUST be the first executable code in the script.
# ---------------------------------------------------------------------------
if ($env:PROCESSOR_ARCHITECTURE -eq "x86" -and $env:PROCESSOR_ARCHITEW6432) {
    $ps64 = "$env:WINDIR\SysNative\WindowsPowerShell\v1.0\powershell.exe"
    & $ps64 -NoProfile -ExecutionPolicy Bypass -File "$PSCommandPath" @args
    exit $LASTEXITCODE
}

# ---------------------------------------------------------------------------
# SYSTEM vs User context detection + UAC self-elevation
# Skip elevation when already running as SYSTEM (Intune MDM context).
# ---------------------------------------------------------------------------
$id       = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$isSystem = $id.IsSystem
$isAdmin  = ([System.Security.Principal.WindowsPrincipal]$id).IsInRole(
                [System.Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isSystem -and -not $isAdmin) {
    Start-Process powershell.exe `
        -Verb Runas `
        -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" `
        -Wait
    exit $LASTEXITCODE
}

# ---------------------------------------------------------------------------
# pnputil two-step driver staging
# Step 1: Stage INF + all associated files into Windows Driver Store
# Step 2: Install the named driver from the Driver Store
# ---------------------------------------------------------------------------
pnputil.exe /add-driver "$PSScriptRoot\drivers\Konica_PCL6/KOAWOJ__.inf" /install

Add-PrinterDriver -Name ""KONICA MINOLTA Universal PCL v3.9.13""

# ---------------------------------------------------------------------------
# Idempotent port creation
# ---------------------------------------------------------------------------
if (-not (Get-PrinterPort -Name "IP_10_124_10_200" -ErrorAction SilentlyContinue)) {
    Add-PrinterPort -Name "IP_10_124_10_200" -PrinterHostAddress "10.124.10.200"
}

# ---------------------------------------------------------------------------
# Idempotent printer creation
# ---------------------------------------------------------------------------
if (-not (Get-Printer -Name "Imprimante Evry" -ErrorAction SilentlyContinue)) {
    Add-Printer -Name "Imprimante Evry" `
                -PortName "IP_10_124_10_200" `
                -DriverName ""KONICA MINOLTA Universal PCL v3.9.13""
}

# ---------------------------------------------------------------------------
# Configure print settings
# ---------------------------------------------------------------------------
Set-PrintConfiguration -PrinterName "Imprimante Evry" `
    -DuplexingMode OneSided `
    -Color $false `
    -PaperSize A4 `
    -Collate $true
