Send-MDMDiagLog.psm1
function Send-MDMDiagLog { <# .SYNOPSIS Collects MDM Diagnostic logs and emails them. .DESCRIPTION Runs mdmdiagnosticstool.exe, generates a ZIP log file (named with serial number + timestamp), attaches it to an email, and sends it along with system info like disk space & power status. .PARAMETER To Destination email address. .PARAMETER From Sender mailbox (SMTP relay enabled). .PARAMETER SmtpServer SMTP relay server address. .PARAMETER Cc Additional recipients (CC list). .EXAMPLE Send-MDMDiagLog -To "user@domain.com" -From "noreply@domain.com" ` -SmtpServer "smtp.domain.com" -Cc "admin@domain.com" #> [CmdletBinding(SupportsShouldProcess=$true)] param( [Parameter(Mandatory=$true)] [ValidatePattern('^[\w\.\-]+@[\w\-]+\.[A-Za-z]{2,}$')] [string]$To, [Parameter(Mandatory=$true)] [ValidatePattern('^[\w\.\-]+@[\w\-]+\.[A-Za-z]{2,}$')] [string]$From, [Parameter(Mandatory=$true)] [string]$SmtpServer, [string[]]$Cc ) # region Ensure Folder Exists $OutputPath = "C:\Temp\Autopilot" if (-not (Test-Path $OutputPath)) { New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null Write-Host "[$(Get-Date -Format 'HH:mm:ss')] Created folder: $OutputPath" -ForegroundColor Yellow } # endregion Ensure Folder Exists # region Variables $Serial = (Get-CimInstance Win32_BIOS).SerialNumber $Date = Get-Date -Format "yyyyMMdd_HHmm" $ZipFile = "C:\Temp\Autopilot\${Serial}_$Date.zip" $Subject = "Windows Endpoint Logs - $Serial" # endregion Variables # region System Info $Drive = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" $FreeGB = [math]::Round($Drive.FreeSpace / 1GB, 2) $DiskStatus = if ($FreeGB -lt 5) { "Low Disk Space ($FreeGB GB Free)" } else { "$FreeGB GB Free" } $Battery = Get-CimInstance Win32_Battery -ErrorAction SilentlyContinue $PowerStatus = if ($Battery -and $Battery.BatteryStatus -eq 2) { "Connected to Power" } else { "Not Connected to Power" } $SystemInfo = [PSCustomObject]@{ ComputerName = $env:COMPUTERNAME Make = (Get-CimInstance Win32_BIOS).Manufacturer Model = (Get-CimInstance Win32_ComputerSystem).Model SerialNumber = $Serial BIOS_GUID = (Get-CimInstance Win32_ComputerSystemProduct).UUID IPAddress = (Get-CimInstance Win32_Networkadapterconfiguration | Where-Object { $_.IPAddress -ne $null }).IPAddress | Select-Object -First 1 MACAddress = (Get-CimInstance Win32_Networkadapterconfiguration | Where-Object { $_.MACAddress -ne $null }).MACAddress | Select-Object -First 1 DiskSpace = $DiskStatus PowerStatus = $PowerStatus } $Table = $SystemInfo | ConvertTo-Html -Fragment -Property ComputerName, Make, Model, SerialNumber, BIOS_GUID, IPAddress, MACAddress, DiskSpace, PowerStatus $Body = @" <html> <head> <style> body { font-family: Segoe UI, Arial; font-size: 10pt; background-color: #f9f9f9; } table { border-collapse: collapse; width: 100%; margin-top: 10px; } th, td { border: 2px solid #2b5797; padding: 8px; text-align: left; } th { background-color: #2b5797; color: white; font-weight: bold; } tr:nth-child(even) { background-color: #f2f2f2; } </style> </head> <body> <h2>MDM Diagnostic Logs</h2> $Table <br/><i>Generated: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")</i> </body> </html> "@ # endregion System Info Write-Host "[$(Get-Date -Format "HH:mm:ss")] Starting MDM Diagnostic collection..." -ForegroundColor Cyan # region Run MDM Diagnostic Tool try { if (-not (Test-Path "C:\Temp\Autopilot")) { New-Item -Path "C:\Temp\Autopilot" -ItemType Directory -Force | Out-Null } Start-Process -FilePath "mdmdiagnosticstool.exe" ` -ArgumentList "-area DeviceEnrollment;DeviceProvisioning;Autopilot -zip $ZipFile" ` -Wait -NoNewWindow Write-Host "[$(Get-Date -Format "HH:mm:ss")] Diagnostics completed. File: $ZipFile" -ForegroundColor Green } catch { throw "Error running MDM Diagnostic Tool: $_" } # endregion Run MDM Diagnostic Tool # region Send Email $params = @{ To = $To From = $From Subject = $Subject Body = $Body SmtpServer = $SmtpServer BodyAsHtml = $true Attachments = @($ZipFile) | Where-Object { Test-Path $_ } } if ($Cc) { $params.Cc = $Cc } try { Send-MailMessage @params Write-Host "[$(Get-Date -Format "HH:mm:ss")] Email sent to $To" -ForegroundColor Yellow } catch { Write-Warning "Failed to send email: $_" } # endregion Send Email Write-Host "[$(Get-Date -Format "HH:mm:ss")] Completed successfully." -ForegroundColor Cyan } |