MECM: Client re-install

Just another Tech site

MECM: Client re-install

Re-installing the MECM client

 

<#
    MECM Reinstall Script
    - Installs MECM client cleanly after full removal
    - Logging + summary report
    - Validates installation success
#>

# ============================
# Logging + Summary Functions
# ============================

$Global:LogFile = "C:\Windows\Temp\SCCM_Reinstall_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$Global:Summary = @()

function Write-Log {
    param(
        [string]$Message,
        [string]$Level = "INFO"
    )

    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $entry = "[$timestamp] [$Level] $Message"

    Write-Host $entry
    Add-Content -Path $Global:LogFile -Value $entry
}

function Add-Summary {
    param([string]$Message)
    $Global:Summary += $Message
}

# ============================
# MECM Reinstall Function
# ============================

function Install-SCCM {
    param(
        [string]$SourcePath,
        [string]$MP = "",
        [string]$SiteCode = "",
        [switch]$ForceReinstall
    )

    Write-Log "Starting SCCM reinstall..."
    Add-Summary "SCCM reinstall started"

    if (-not (Test-Path $SourcePath)) {
        Write-Log "ERROR: Source path not found: $SourcePath" "ERROR"
        Add-Summary "Install failed (source path missing)"
        return
    }

    $ccmSetup = Join-Path $SourcePath "ccmsetup.exe"

    if (-not (Test-Path $ccmSetup)) {
        Write-Log "ERROR: ccmsetup.exe not found in: $SourcePath" "ERROR"
        Add-Summary "Install failed (ccmsetup.exe missing)"
        return
    }

    # Build argument list
    $args = @()

    if ($MP)       { $args += "MP=$MP" }
    if ($SiteCode) { $args += "SMSSITECODE=$SiteCode" }
    if ($ForceReinstall) { $args += "RESETKEYINFORMATION=TRUE" }

    $argString = $args -join " "

    Write-Log "Running SCCM installer with arguments: $argString"
    Add-Summary "Installer launched"

    Start-Process -FilePath $ccmSetup -ArgumentList $argString -Wait

    Write-Log "Waiting for ccmsetup.exe to finish..."
    Wait-Process -Name ccmsetup -ErrorAction SilentlyContinue

    # Validate installation
    Write-Log "Validating SCCM client installation..."

    $service = Get-Service -Name CcmExec -ErrorAction SilentlyContinue

    if ($service -and $service.Status -eq "Running") {
        Write-Log "SCCM client installed successfully."
        Add-Summary "SCCM client installed successfully"
    }
    else {
        Write-Log "ERROR: SCCM client installation failed." "ERROR"
        Add-Summary "Install failed (CcmExec not running)"
    }
}

# ============================
# Run Installation
# ============================

# EXAMPLE USAGE:
# Install-SCCM -SourcePath "\\SERVER\SMSClient" -MP "MP01.contoso.com" -SiteCode "ABC"

Install-SCCM `
    -SourcePath "\\YOURSERVER\SMSClient" `
    -MP "YOUR-MP-FQDN" `
    -SiteCode "YOUR-SITECODE" `
    -ForceReinstall

# ============================
# Summary Report
# ============================

Write-Log "Generating summary report..."

$summaryHeader = "===== SCCM Reinstall Summary ====="
Write-Host $summaryHeader
Add-Content -Path $Global:LogFile -Value $summaryHeader

foreach ($item in $Global:Summary) {
    Write-Host " - $item"
    Add-Content -Path $Global:LogFile -Value " - $item"
}

$summaryFooter = "===== End of Summary ====="
Write-Host $summaryFooter
Add-Content -Path $Global:LogFile -Value $summaryFooter

Write-Log "Summary report completed. Log saved to $Global:LogFile"