Remediate-IMEHealth.ps1

<#PSScriptInfo
.VERSION 1.0
.GUID 5a1e9d44-7c2b-4f0a-9e33-1b8f4c2d7a90
.AUTHOR Mert Efe Kanlikilic
.DESCRIPTION A remediation script that resolves common Intune Management Extension (IME) health issues on Windows devices.
#>


$logPath = "C:\ProgramData\IntuneRemediations\IMEHealth.log"

function Write-Log {
    param ($msg)
    $time = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $entry = "$time - $msg"
    Add-Content -Path $logPath -Value $entry
    Write-Output $msg
}

try {
    Write-Log "Starting IME remediation"

    # 1. Service restart (primary fix)
    try {
        Write-Log "Restarting IME service"

        Restart-Service IntuneManagementExtension -Force -ErrorAction Stop
        Start-Sleep -Seconds 5

        $svc = Get-Service IntuneManagementExtension
        if ($svc.Status -eq "Running") {
            Write-Log "IME restarted successfully"
            exit 0
        }
    }
    catch {
        Write-Log "Restart failed: $_"
    }

    # 2. Fallback: kill process + restart
    Write-Log "Attempting process-level recovery"

    Get-Process -Name "IntuneManagementExtension" -ErrorAction SilentlyContinue | Stop-Process -Force
    Start-Sleep 3

    Start-Service IntuneManagementExtension
    Start-Sleep 5

    $svc = Get-Service IntuneManagementExtension
    if ($svc.Status -eq "Running") {
        Write-Log "IME recovered after process restart"
        exit 0
    }

    Write-Log "IME still not fully recovered (may require reinstall or policy sync)"
    exit 0   # fail etme (çok önemli)
}
catch {
    Write-Log "Remediation failed: $_"
    exit 1
}