use std::collections::HashMap;
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use super::guid_registry::GuidRegistryEntry;
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EvidenceBundleArtifactCounts {
pub collected: u64,
pub missing: u64,
pub failed: u64,
pub skipped: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct EvidenceBundleMetadata {
pub manifest_path: String,
pub notes_path: Option<String>,
pub evidence_root: Option<String>,
#[serde(default)]
pub primary_entry_points: Vec<String>,
#[serde(default)]
pub available_primary_entry_points: Vec<String>,
pub bundle_id: Option<String>,
pub bundle_label: Option<String>,
pub created_utc: Option<String>,
pub case_reference: Option<String>,
pub summary: Option<String>,
pub collector_profile: Option<String>,
pub collector_version: Option<String>,
pub collected_utc: Option<String>,
pub device_name: Option<String>,
pub primary_user: Option<String>,
pub platform: Option<String>,
pub os_version: Option<String>,
pub tenant: Option<String>,
pub artifact_counts: Option<EvidenceBundleArtifactCounts>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneDiagnosticSeverity {
Info,
Warning,
Error,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneDiagnosticCategory {
Download,
Install,
Timeout,
Script,
Policy,
State,
General,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntuneRemediationPriority {
Monitor,
Medium,
High,
Immediate,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticInsight {
pub id: String,
pub severity: IntuneDiagnosticSeverity,
pub category: IntuneDiagnosticCategory,
pub remediation_priority: IntuneRemediationPriority,
pub title: String,
pub summary: String,
pub likely_cause: Option<String>,
pub evidence: Vec<String>,
pub next_checks: Vec<String>,
#[serde(default)]
pub suggested_fixes: Vec<String>,
#[serde(default)]
pub focus_areas: Vec<String>,
#[serde(default)]
pub affected_source_files: Vec<String>,
#[serde(default)]
pub related_error_codes: Vec<String>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum IntuneEventType {
Win32App,
WinGetApp,
PowerShellScript,
Remediation,
Esp,
SyncSession,
PolicyEvaluation,
ContentDownload,
Other,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum IntuneStatus {
Success,
Failed,
InProgress,
Pending,
Timeout,
Unknown,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneEvent {
pub id: u64,
pub event_type: IntuneEventType,
pub name: String,
pub guid: Option<String>,
pub status: IntuneStatus,
pub start_time: Option<String>,
pub end_time: Option<String>,
pub duration_secs: Option<f64>,
pub error_code: Option<String>,
pub detail: String,
pub source_file: String,
pub line_number: u32,
pub start_time_epoch: Option<i64>,
pub end_time_epoch: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub script_body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_app_guid: Option<String>,
}
impl IntuneEvent {
pub fn status_is_failed(&self) -> bool {
matches!(self.status, IntuneStatus::Failed)
}
pub fn start_time_epoch_ms(&self) -> Option<i64> {
self.start_time_epoch
}
pub fn anchor_guid(&self) -> Option<&str> {
self.guid.as_deref()
}
pub fn display_name(&self) -> Option<&str> {
if self.name.is_empty() {
None
} else {
Some(self.name.as_str())
}
}
pub fn event_kind_label(&self) -> Option<&str> {
Some(match self.event_type {
IntuneEventType::Win32App => "App Install",
IntuneEventType::WinGetApp => "WinGet Install",
IntuneEventType::PowerShellScript => "Script",
IntuneEventType::Remediation => "Remediation",
IntuneEventType::Esp => "ESP",
IntuneEventType::SyncSession => "Sync",
IntuneEventType::PolicyEvaluation => "Policy",
IntuneEventType::ContentDownload => "Download",
IntuneEventType::Other => "Operation",
})
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AppPolicyMetadata {
pub id: String,
pub name: String,
pub intent: Option<i32>,
pub target_type: Option<i32>,
pub install_command_line: Option<String>,
pub uninstall_command_line: Option<String>,
pub detection_rules: Vec<DetectionRuleMetadata>,
pub return_codes: Vec<ReturnCodeEntry>,
pub requirement_rules: Option<String>,
pub install_behavior: Option<i32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DetectionRuleMetadata {
pub detection_type: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub script_body: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub enforce_signature_check: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub run_as_32_bit: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReturnCodeEntry {
pub return_code: i32,
#[serde(rename = "type")]
pub code_type: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DownloadStat {
pub content_id: String,
pub name: String,
pub size_bytes: u64,
pub speed_bps: f64,
pub do_percentage: f64,
pub duration_secs: f64,
pub success: bool,
pub timestamp: Option<String>,
pub timestamp_epoch: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneTimestampBounds {
pub first_timestamp: Option<String>,
pub last_timestamp: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsFileCoverage {
pub file_path: String,
pub event_count: u32,
pub download_count: u32,
pub timestamp_bounds: Option<IntuneTimestampBounds>,
#[serde(default)]
pub is_rotated_segment: bool,
pub rotation_group: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsCoverage {
#[serde(default)]
pub files: Vec<IntuneDiagnosticsFileCoverage>,
pub timestamp_bounds: Option<IntuneTimestampBounds>,
#[serde(default)]
pub has_rotated_logs: bool,
pub dominant_source: Option<IntuneDominantSource>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDominantSource {
pub file_path: String,
pub event_count: u32,
pub event_share: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum IntuneDiagnosticsConfidenceLevel {
#[default]
Unknown,
Low,
Medium,
High,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct IntuneDiagnosticsConfidence {
pub level: IntuneDiagnosticsConfidenceLevel,
pub score: Option<f64>,
#[serde(default)]
pub reasons: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneRepeatedFailureGroup {
pub id: String,
pub name: String,
pub event_type: IntuneEventType,
pub error_code: Option<String>,
pub occurrences: u32,
pub timestamp_bounds: Option<IntuneTimestampBounds>,
#[serde(default)]
pub source_files: Vec<String>,
#[serde(default)]
pub sample_event_ids: Vec<u64>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum EventLogSeverity {
Critical,
Error,
Warning,
Information,
Verbose,
Unknown,
}
impl EventLogSeverity {
pub fn from_level(level: u8) -> Self {
match level {
1 => Self::Critical,
2 => Self::Error,
3 => Self::Warning,
4 => Self::Information,
5 => Self::Verbose,
_ => Self::Unknown,
}
}
pub fn is_error_or_warning(&self) -> bool {
matches!(self, Self::Critical | Self::Error | Self::Warning)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum EventLogChannel {
DeviceManagementAdmin,
DeviceManagementOperational,
Autopilot,
AadOperational,
DeliveryOptimizationOperational,
ManagementService,
ProvisioningDiagnosticsAdmin,
ShellCoreOperational,
TimeServiceOperational,
UserDeviceRegistrationAdmin,
CryptoDpapiOperational,
KerberosOperational,
SystemLog,
Other(String),
}
impl EventLogChannel {
pub fn from_channel_string(raw: &str) -> Self {
let lower = raw.to_ascii_lowercase();
if lower.contains("devicemanagement")
&& lower.contains("/admin")
&& !lower.contains("/operational")
{
Self::DeviceManagementAdmin
} else if lower.contains("devicemanagement") && lower.contains("/operational") {
Self::DeviceManagementOperational
} else if lower.contains("moderndeployment") && lower.contains("autopilot") {
Self::Autopilot
} else if lower.contains("-aad/operational") {
Self::AadOperational
} else if lower.contains("deliveryoptimization") && lower.contains("/operational") {
Self::DeliveryOptimizationOperational
} else if lower.contains("moderndeployment") && lower.contains("managementservice") {
Self::ManagementService
} else if lower.contains("provisioning-diagnostics") && lower.contains("/admin") {
Self::ProvisioningDiagnosticsAdmin
} else if lower.contains("shell-core") && lower.contains("/operational") {
Self::ShellCoreOperational
} else if lower.contains("time-service") && lower.contains("/operational") {
Self::TimeServiceOperational
} else if lower.contains("user device registration") && lower.contains("/admin") {
Self::UserDeviceRegistrationAdmin
} else if lower.contains("crypto-dpapi") && lower.contains("/operational") {
Self::CryptoDpapiOperational
} else if lower.contains("kerberos") && lower.contains("/operational") {
Self::KerberosOperational
} else if lower == "system" || lower.contains("system log") {
Self::SystemLog
} else if raw.is_empty() {
Self::Other("Unknown".to_string())
} else {
Self::Other(raw.to_string())
}
}
pub fn display_name(&self) -> &str {
match self {
Self::DeviceManagementAdmin => "MDM Admin",
Self::DeviceManagementOperational => "MDM Operational",
Self::Autopilot => "Autopilot",
Self::AadOperational => "AAD Operational",
Self::DeliveryOptimizationOperational => "DO Operational",
Self::ManagementService => "Management Service",
Self::ProvisioningDiagnosticsAdmin => "Provisioning Admin",
Self::ShellCoreOperational => "Shell Core",
Self::TimeServiceOperational => "Time Service",
Self::UserDeviceRegistrationAdmin => "User Device Reg",
Self::CryptoDpapiOperational => "Crypto DPAPI",
Self::KerberosOperational => "Kerberos",
Self::SystemLog => "System",
Self::Other(name) => name.as_str(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogEntry {
pub id: u64,
pub channel: EventLogChannel,
pub channel_display: String,
pub provider: String,
pub event_id: u32,
pub severity: EventLogSeverity,
pub timestamp: String,
pub computer: Option<String>,
pub message: String,
pub correlation_activity_id: Option<String>,
pub source_file: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogChannelSummary {
pub channel: EventLogChannel,
pub channel_display: String,
pub entry_count: u32,
pub error_count: u32,
pub warning_count: u32,
pub timestamp_bounds: Option<IntuneTimestampBounds>,
pub source_file: String,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum EventLogCorrelationKind {
TimeWindowChannelMatch,
ErrorCodeMatch,
EnrollmentContextMatch,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogCorrelationLink {
pub event_log_entry_id: u64,
pub linked_intune_event_id: Option<u64>,
pub linked_diagnostic_id: Option<String>,
pub correlation_kind: EventLogCorrelationKind,
pub time_delta_secs: Option<f64>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum EventLogAnalysisSource {
#[default]
Bundle,
Live,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum EventLogLiveQueryStatus {
#[default]
Success,
Empty,
Failed,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventLogLiveQueryChannelResult {
pub channel: EventLogChannel,
pub channel_display: String,
pub channel_path: String,
pub source_file: String,
pub status: EventLogLiveQueryStatus,
pub entry_count: u32,
pub error_message: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EventLogLiveQueryMetadata {
pub attempted_channel_count: u32,
pub successful_channel_count: u32,
pub channels_with_results_count: u32,
pub failed_channel_count: u32,
pub per_channel_entry_limit: u32,
#[serde(default)]
pub channels: Vec<EventLogLiveQueryChannelResult>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EventLogAnalysis {
pub source_kind: EventLogAnalysisSource,
pub entries: Vec<EventLogEntry>,
pub channel_summaries: Vec<EventLogChannelSummary>,
pub correlation_links: Vec<EventLogCorrelationLink>,
pub parsed_file_count: u32,
pub total_entry_count: u32,
pub error_entry_count: u32,
pub warning_entry_count: u32,
pub timestamp_bounds: Option<IntuneTimestampBounds>,
pub live_query: Option<EventLogLiveQueryMetadata>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneAnalysisResult {
pub events: Vec<IntuneEvent>,
pub downloads: Vec<DownloadStat>,
pub summary: IntuneSummary,
#[serde(default)]
pub diagnostics: Vec<IntuneDiagnosticInsight>,
pub source_file: String,
#[serde(default)]
pub source_files: Vec<String>,
#[serde(default)]
pub diagnostics_coverage: IntuneDiagnosticsCoverage,
#[serde(default)]
pub diagnostics_confidence: IntuneDiagnosticsConfidence,
#[serde(default)]
pub repeated_failures: Vec<IntuneRepeatedFailureGroup>,
#[serde(default)]
pub evidence_bundle: Option<EvidenceBundleMetadata>,
#[serde(default)]
pub event_log_analysis: Option<EventLogAnalysis>,
#[serde(default)]
pub guid_registry: HashMap<String, GuidRegistryEntry>,
#[serde(default)]
pub policy_metadata: HashMap<String, AppPolicyMetadata>,
}
impl Serialize for IntuneAnalysisResult {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("IntuneAnalysisResult", 13)?;
state.serialize_field("events", &self.events)?;
state.serialize_field("downloads", &self.downloads)?;
state.serialize_field("summary", &self.summary)?;
state.serialize_field("diagnostics", &self.diagnostics)?;
state.serialize_field("sourceFile", &self.source_file)?;
state.serialize_field("sourceFiles", &self.source_files)?;
state.serialize_field("diagnosticsCoverage", &self.diagnostics_coverage)?;
state.serialize_field("diagnosticsConfidence", &self.diagnostics_confidence)?;
state.serialize_field("repeatedFailures", &self.repeated_failures)?;
state.serialize_field("evidenceBundle", &self.evidence_bundle)?;
state.serialize_field("eventLogAnalysis", &self.event_log_analysis)?;
state.serialize_field("guidRegistry", &self.guid_registry)?;
state.serialize_field("policyMetadata", &self.policy_metadata)?;
state.end()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IntuneSummary {
pub total_events: u32,
pub win32_apps: u32,
pub winget_apps: u32,
pub scripts: u32,
pub remediations: u32,
pub succeeded: u32,
pub failed: u32,
pub in_progress: u32,
pub pending: u32,
pub timed_out: u32,
pub total_downloads: u32,
pub successful_downloads: u32,
pub failed_downloads: u32,
pub failed_scripts: u32,
pub log_time_span: Option<String>,
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use super::{
EvidenceBundleArtifactCounts, EvidenceBundleMetadata, IntuneAnalysisResult,
IntuneDiagnosticsConfidence, IntuneDiagnosticsCoverage, IntuneSummary,
};
#[test]
fn intune_analysis_result_serializes_evidence_bundle_metadata() {
let result = IntuneAnalysisResult {
events: Vec::new(),
downloads: Vec::new(),
summary: IntuneSummary {
total_events: 0,
win32_apps: 0,
winget_apps: 0,
scripts: 0,
remediations: 0,
succeeded: 0,
failed: 0,
in_progress: 0,
pending: 0,
timed_out: 0,
total_downloads: 0,
successful_downloads: 0,
failed_downloads: 0,
failed_scripts: 0,
log_time_span: None,
},
diagnostics: Vec::new(),
source_file: "bundle-root".to_string(),
source_files: Vec::new(),
diagnostics_coverage: IntuneDiagnosticsCoverage::default(),
diagnostics_confidence: IntuneDiagnosticsConfidence::default(),
repeated_failures: Vec::new(),
event_log_analysis: None,
guid_registry: HashMap::new(),
policy_metadata: HashMap::new(),
evidence_bundle: Some(EvidenceBundleMetadata {
manifest_path: "bundle-root/manifest.json".to_string(),
notes_path: Some("bundle-root/notes.md".to_string()),
evidence_root: Some("bundle-root/evidence".to_string()),
primary_entry_points: vec!["bundle-root/evidence/logs".to_string()],
available_primary_entry_points: vec!["bundle-root/evidence/logs".to_string()],
bundle_id: Some("CMTRACE-123".to_string()),
bundle_label: Some("intune-endpoint-evidence".to_string()),
created_utc: Some("2026-03-12T16:00:54Z".to_string()),
case_reference: Some("case-123".to_string()),
summary: Some("Collected bundle".to_string()),
collector_profile: Some("intune-windows-endpoint-v1".to_string()),
collector_version: Some("1.1.0".to_string()),
collected_utc: Some("2026-03-12T16:00:54Z".to_string()),
device_name: Some("GELL-VM-5879648".to_string()),
primary_user: Some("AzureAD\\AdamGell".to_string()),
platform: Some("Windows".to_string()),
os_version: Some("Windows 11".to_string()),
tenant: Some("CDWWorkspaceLab".to_string()),
artifact_counts: Some(EvidenceBundleArtifactCounts {
collected: 55,
missing: 7,
failed: 2,
skipped: 0,
}),
}),
};
let value = serde_json::to_value(&result).expect("serialize result");
assert_eq!(
value["evidenceBundle"]["bundleId"].as_str(),
Some("CMTRACE-123")
);
assert_eq!(
value["evidenceBundle"]["artifactCounts"]["collected"].as_u64(),
Some(55)
);
}
}