Private/Get-TechCareManagedState.ps1
|
function Get-TechCareManagedState { <# .SYNOPSIS Devuelve los motivos por los que el equipo esta gestionado centralmente. .DESCRIPTION Se consultan tres origenes independientes porque ninguno cubre todos los casos: WMI ve el dominio clasico, dsregcmd ve Entra ID, y la rama Enrollments del registro ve la inscripcion en MDM o Intune. Una coleccion vacia significa equipo autonomo, no "no se pudo comprobar". #> [CmdletBinding()] [OutputType([string[]])] param() $reasons = New-Object -TypeName System.Collections.ArrayList if (-not (Test-TechCarePlatform)) { return [string[]]@() } try { $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop if ($cs.PartOfDomain) { [void]$reasons.Add("Equipo unido al dominio '$($cs.Domain)'") } } catch { [void]$reasons.Add('No se pudo comprobar la pertenencia a dominio por WMI: ' + $_.Exception.Message) } $dsreg = Join-Path -Path $env:SystemRoot -ChildPath 'System32\dsregcmd.exe' if (Test-Path -LiteralPath $dsreg) { try { $out = & $dsreg '/status' 2>$null | Out-String # Se buscan ambas claves en ingles porque dsregcmd no se localiza. if ($out -match 'AzureAdJoined\s*:\s*YES') { [void]$reasons.Add('Equipo unido a Microsoft Entra ID') } if ($out -match 'DomainJoined\s*:\s*YES') { [void]$reasons.Add('Equipo unido a un dominio (dsregcmd)') } } catch { [void]$reasons.Add('dsregcmd no respondio: ' + $_.Exception.Message) } } $enrollRoot = 'HKLM:\SOFTWARE\Microsoft\Enrollments' if (Test-Path -LiteralPath $enrollRoot) { foreach ($key in (Get-ChildItem -LiteralPath $enrollRoot -ErrorAction SilentlyContinue)) { $props = Get-ItemProperty -LiteralPath $key.PSPath -ErrorAction SilentlyContinue if ($null -eq $props) { continue } # EnrollmentState 1 con proveedor MS DM Server es la inscripcion activa; # las demas subclaves son restos de inscripciones anteriores. $provider = $props.PSObject.Properties['ProviderID'] $stateVal = $props.PSObject.Properties['EnrollmentState'] if ($null -eq $provider -or $null -eq $stateVal) { continue } if ($provider.Value -eq 'MS DM Server' -and $stateVal.Value -eq 1) { [void]$reasons.Add('Equipo administrado por MDM / Intune') break } } } return [string[]]$reasons.ToArray() } |