categories/Telemetry.ps1

# Telemetry category: DiagTrack, dmwappushservice, CEIP/compatibility scheduled tasks,
# and the AllowTelemetry policy value.

function Get-OctaTelemetryCategory {
    [CmdletBinding()]
    param()
    return [pscustomobject]@{
        Id                   = 'telemetry'
        DisplayName          = 'Telemetry'
        Description          = 'DiagTrack, dmwappushservice, CEIP/compatibility scheduled tasks'
        RequiresElevation    = $true
        ContainsIrreversibleActions = $false
        GetActionsFunction   = 'Get-OctaTelemetryActions'
        ApplyActionFunction  = 'Set-OctaTelemetryAction'
    }
}

function Get-OctaTelemetryActions {
    [CmdletBinding()]
    param()
    $actions = @()

    foreach ($svcName in @('DiagTrack', 'dmwappushservice')) {
        $svc = Get-CimInstance -ClassName Win32_Service -Filter "Name='$svcName'" -ErrorAction SilentlyContinue
        if ($svc -and $svc.StartMode -ne 'Disabled') {
            $actions += New-OctaAction -TargetType Service -TargetIdentifier $svcName `
                -CurrentValue $svc.StartMode -PlannedValue 'Disabled' -Reversible $true
        }
    }

    $tasks = @(
        @{ Path = '\Microsoft\Windows\Customer Experience Improvement Program\'; Name = 'Consolidator' },
        @{ Path = '\Microsoft\Windows\Customer Experience Improvement Program\'; Name = 'UsbCeip' },
        @{ Path = '\Microsoft\Windows\Application Experience\'; Name = 'Microsoft Compatibility Appraiser' },
        @{ Path = '\Microsoft\Windows\Application Experience\'; Name = 'ProgramDataUpdater' }
    )
    foreach ($t in $tasks) {
        $task = Get-ScheduledTask -TaskPath $t.Path -TaskName $t.Name -ErrorAction SilentlyContinue
        if ($task -and $task.State -ne 'Disabled') {
            $actions += New-OctaAction -TargetType ScheduledTask -TargetIdentifier ('{0}|{1}' -f $t.Path, $t.Name) `
                -CurrentValue $task.State.ToString() -PlannedValue 'Disabled' -Reversible $true
        }
    }

    $regPath = 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection'
    $current = $null
    if (Test-Path $regPath) {
        $current = (Get-ItemProperty -Path $regPath -Name AllowTelemetry -ErrorAction SilentlyContinue).AllowTelemetry
    }
    if ($null -eq $current -or $current -ne 0) {
        $displayCurrent = if ($null -eq $current) { '(not set)' } else { $current }
        $actions += New-OctaAction -TargetType Registry -TargetIdentifier "$regPath|AllowTelemetry" `
            -CurrentValue $displayCurrent -PlannedValue 0 -Reversible $true
    }

    return $actions
}

function Set-OctaTelemetryAction {
    [CmdletBinding()]
    param([Parameter(Mandatory)]$Action)

    switch ($Action.TargetType) {
        'Service' {
            Set-Service -Name $Action.TargetIdentifier -StartupType Disabled
        }
        'ScheduledTask' {
            $parts = $Action.TargetIdentifier -split '\|', 2
            Disable-ScheduledTask -TaskPath $parts[0] -TaskName $parts[1] | Out-Null
        }
        'Registry' {
            $keyPath, $valueName = $Action.TargetIdentifier -split '\|', 2
            if (-not (Test-Path $keyPath)) {
                New-Item -Path $keyPath -Force | Out-Null
            }
            Set-ItemProperty -Path $keyPath -Name $valueName -Value $Action.PlannedValue -Type DWord
        }
    }
}