diff --git a/src/Api.ps1 b/src/Api.ps1 index e41958f..6439203 100644 --- a/src/Api.ps1 +++ b/src/Api.ps1 @@ -34,6 +34,7 @@ function Invoke-ApiHandler { "POST /api/groups" { return New-GroupEndpoint -Body $Body } "POST /api/groups/check" { return Test-GroupNameEndpoint -Body $Body } "POST /api/groups/resolve-upns" { return Resolve-UpnsEndpoint -Body $Body } + "POST /api/groups/resolve-devices" { return Resolve-DevicesEndpoint -Body $Body } "GET /api/users" { return Search-UsersEndpoint -Query $Query } @@ -1583,6 +1584,104 @@ function Resolve-UpnsEndpoint { return @{ items = @($resolved); found = $found.Count; notFound = $notFound.Count } } +function Resolve-DevicesEndpoint { + # Loest Geraete-Bezeichner (GERAETENAME ODER SERIENNUMMER, auto) zu Entra- + # Geraete-OBJEKT-IDs auf (fuer die Gruppen-Aufnahme). Rueckgabeformat wie + # Resolve-UpnsEndpoint (Upn/UserId/DisplayName/Mail/Found) -> die CSV-Vorschau + # und der Import funktionieren unveraendert; UserId ist hier die Entra-Objekt-Id. + param($Body) + $err = Test-Connected + if ($err) { return $err } + + $idsRaw = @($Body.identifiers | ForEach-Object { [string]$_.Trim() } | Where-Object { $_ -ne '' }) + if ($idsRaw.Count -eq 0) { return @{ __status = 400; error = 'identifiers fehlt oder leer' } } + $ids = @($idsRaw | Select-Object -Unique) + + Write-Host "[IMPORT] Loese $($ids.Count) Geraete auf (Name/Serial)..." -ForegroundColor DarkCyan + $sel = 'id,deviceName,serialNumber,azureADDeviceId,manufacturer,model' + + # Pass 1: managedDevice per Geraetename (exakt). + $md = @{} # identifier(lower) -> managedDevice + $reqs = @(); $i = 0 + foreach ($id in $ids) { + $esc = $id -replace "'", "''" + $f = [Uri]::EscapeDataString("deviceName eq '$esc'") + $reqs += @{ id = "n$i"; method = 'GET'; url = "/deviceManagement/managedDevices?`$filter=$f&`$select=$sel&`$top=1" } + $i++ + } + $b1 = Invoke-GraphBatch -Requests $reqs + for ($j = 0; $j -lt $ids.Count; $j++) { + $r = $b1["n$j"] + if ($r -and [int]$r.status -eq 200 -and @($r.body.value).Count -gt 0) { $md[$ids[$j].ToLower()] = @($r.body.value)[0] } + } + + # Pass 2: nicht per Name Gefundene per Seriennummer. + $unresolved = @($ids | Where-Object { -not $md.ContainsKey($_.ToLower()) }) + if ($unresolved.Count -gt 0) { + $reqs = @(); $i = 0 + foreach ($id in $unresolved) { + $esc = $id -replace "'", "''" + $f = [Uri]::EscapeDataString("serialNumber eq '$esc'") + $reqs += @{ id = "s$i"; method = 'GET'; url = "/deviceManagement/managedDevices?`$filter=$f&`$select=$sel&`$top=1" } + $i++ + } + $b2 = Invoke-GraphBatch -Requests $reqs + for ($j = 0; $j -lt $unresolved.Count; $j++) { + $r = $b2["s$j"] + if ($r -and [int]$r.status -eq 200 -and @($r.body.value).Count -gt 0) { $md[$unresolved[$j].ToLower()] = @($r.body.value)[0] } + } + } + + # Pass 3: azureADDeviceId -> Entra-Geraete-Objekt-Id (fuer die Gruppen-Aufnahme). + $aads = @($md.Values | ForEach-Object { [string]$_.azureADDeviceId } | Where-Object { $_ -and $_ -ne '00000000-0000-0000-0000-000000000000' } | Select-Object -Unique) + $entraByAad = @{} + if ($aads.Count -gt 0) { + $reqs = @(); $i = 0 + foreach ($aad in $aads) { + $f = [Uri]::EscapeDataString("deviceId eq '$aad'") + $reqs += @{ id = "e$i"; method = 'GET'; url = "/devices?`$filter=$f&`$select=id,displayName,deviceId&`$top=1" } + $i++ + } + $b3 = Invoke-GraphBatch -Requests $reqs + for ($j = 0; $j -lt $aads.Count; $j++) { + $r = $b3["e$j"] + if ($r -and [int]$r.status -eq 200 -and @($r.body.value).Count -gt 0) { $entraByAad[$aads[$j]] = @($r.body.value)[0] } + } + } + + $resolved = [System.Collections.Generic.List[object]]::new() + foreach ($id in $ids) { + $m = $null + if ($md.ContainsKey($id.ToLower())) { $m = $md[$id.ToLower()] } + $objId = ''; $info = '' + $dn = '' + if ($m) { + $dn = [string]$m.deviceName + $aad = [string]$m.azureADDeviceId + if ($aad -and $entraByAad.ContainsKey($aad)) { + $ent = $entraByAad[$aad] + if ($ent.id) { $objId = [string]$ent.id } + } + $sn = [string]$m.serialNumber + $hw = ((@([string]$m.manufacturer, [string]$m.model) | Where-Object { $_ }) -join ' ').Trim() + $parts = @(); if ($sn) { $parts += "SN $sn" }; if ($hw) { $parts += $hw } + $info = ($parts -join ' - ') + } + $resolved.Add([pscustomobject]@{ + Upn = $id + UserId = $objId + DisplayName = $dn + Mail = $info + Found = [bool]$objId + }) + } + + $found = @($resolved | Where-Object { $_.Found }) + $notFound = @($resolved | Where-Object { -not $_.Found }) + Write-Host " -> $($found.Count) Geraete aufgeloest, $($notFound.Count) nicht gefunden" -ForegroundColor DarkGray + return @{ items = @($resolved); found = $found.Count; notFound = $notFound.Count } +} + function Remove-GroupMemberEndpoint { param([string]$GroupId, [string]$UserId) $err = Test-Connected diff --git a/www/app.js b/www/app.js index 398fbca..2f4ddc5 100644 --- a/www/app.js +++ b/www/app.js @@ -5580,6 +5580,7 @@ const CsvImport = { resolved: [], // Ergebnis des letzten Resolve-Calls groupId: '', groupName: '', + mode: 'users', // 'users' | 'devices' }; // Datei-Upload -> Textarea füllen @@ -5644,20 +5645,74 @@ function parseCsvUpns(raw) { return [...new Set(upns)]; } +// Geräte-Bezeichner (Gerätename ODER Seriennummer) aus CSV extrahieren. +function parseCsvDevices(raw) { + const lines = raw.split(/\r?\n/).map(l => l.trim()).filter(Boolean); + if (lines.length === 0) return []; + const HEADERS = ['devicename', 'gerätename', 'geraetename', 'name', 'serialnumber', 'seriennummer', 'serial', 'sn']; + const firstFields = splitCsvLine(lines[0]).map(f => f.toLowerCase()); + let colIdx = -1; + for (const h of HEADERS) { const idx = firstFields.indexOf(h); if (idx !== -1) { colIdx = idx; break; } } + const startLine = colIdx !== -1 ? 1 : 0; + if (colIdx === -1) colIdx = 0; // kein Header -> erste Spalte + const out = []; + for (let i = startLine; i < lines.length; i++) { + const val = (splitCsvLine(lines[i])[colIdx] || '').trim(); + if (val) out.push(val); + } + return [...new Set(out)]; +} + +// Beschriftungen je Modus (Benutzer vs. Geräte) umschalten. +function csvResolveLabel() { return CsvImport.mode === 'devices' ? 'Geräte auflösen' : 'Benutzer auflösen'; } +function csvApplyModeLabels() { + const dev = CsvImport.mode === 'devices'; + const t = document.getElementById('csvImportTitle'); + if (t) t.textContent = dev ? 'Gerätenamen/Seriennummern einfügen oder CSV hochladen' : 'UPNs einfügen oder CSV hochladen'; + const ta = document.getElementById('csvImportText'); + if (ta) ta.placeholder = dev + ? 'Ein Gerätename oder eine Seriennummer pro Zeile\n(oder erste Spalte einer CSV, Header wird erkannt).' + : 'Eine UPN/E-Mail pro Zeile, z.B.\nmax.mustermann@firma.de\nerika.muster@firma.de\n\nOder erste Spalte einer CSV (Header wird erkannt).'; + const ch = document.getElementById('csvPreviewColHead'); + if (ch) ch.textContent = dev ? 'Gerät (Name/Serial)' : 'UPN / E-Mail'; + const rb = document.getElementById('csvResolveBtn'); + if (rb) rb.textContent = csvResolveLabel(); +} + +document.querySelectorAll('input[name="csvImportMode"]').forEach(radio => { + radio.addEventListener('change', () => { + if (!radio.checked) return; + CsvImport.mode = radio.value === 'devices' ? 'devices' : 'users'; + CsvImport.resolved = []; + document.getElementById('csvPreviewWrap')?.classList.add('hidden'); + document.getElementById('csvResultBanner')?.classList.add('hidden'); + document.getElementById('csvResolveStats').textContent = ''; + csvApplyModeLabels(); + updateCsvImportNote(); + }); +}); + // Resolve-Button document.getElementById('csvResolveBtn')?.addEventListener('click', async () => { const raw = document.getElementById('csvImportText').value; - const upns = parseCsvUpns(raw); - if (upns.length === 0) { toast('Keine gültigen UPNs/E-Mails gefunden.', 'warn'); return; } - const btn = document.getElementById('csvResolveBtn'); const stats = document.getElementById('csvResolveStats'); + const dev = CsvImport.mode === 'devices'; + + const entries = dev ? parseCsvDevices(raw) : parseCsvUpns(raw); + if (entries.length === 0) { + toast(dev ? 'Keine Gerätenamen/Seriennummern gefunden.' : 'Keine gültigen UPNs/E-Mails gefunden.', 'warn'); + return; + } + btn.disabled = true; btn.textContent = 'Löse auf…'; - stats.textContent = `${upns.length} Einträge werden aufgelöst…`; + stats.textContent = `${entries.length} Einträge werden aufgelöst…`; try { - const res = await api('/api/groups/resolve-upns', { method: 'POST', body: { upns } }); + const res = dev + ? await api('/api/groups/resolve-devices', { method: 'POST', body: { identifiers: entries } }) + : await api('/api/groups/resolve-upns', { method: 'POST', body: { upns: entries } }); CsvImport.resolved = res.items || []; renderCsvPreview(); stats.textContent = `${res.found} gefunden, ${res.notFound} nicht gefunden`; @@ -5666,7 +5721,7 @@ document.getElementById('csvResolveBtn')?.addEventListener('click', async () => stats.textContent = ''; } finally { btn.disabled = false; - btn.textContent = 'Benutzer auflösen'; + btn.textContent = csvResolveLabel(); } }); @@ -5704,11 +5759,12 @@ function updateCsvImportNote() { const note = document.getElementById('csvImportNote'); const importBtn = document.getElementById('csvImportBtn'); const found = CsvImport.resolved.filter(r => r.Found).length; + const what = CsvImport.mode === 'devices' ? 'Geräte' : 'Benutzer'; if (!CsvImport.groupId) { - note.textContent = 'Gruppe links auswählen, dann Benutzer auflösen.'; + note.textContent = `Gruppe links auswählen, dann ${what} auflösen.`; importBtn.disabled = true; } else if (found === 0) { - note.textContent = `Gruppe: ${CsvImport.groupName} — keine aufgelösten Benutzer.`; + note.textContent = `Gruppe: ${CsvImport.groupName} — keine aufgelösten ${what}.`; importBtn.disabled = true; } else { note.textContent = `Gruppe: ${CsvImport.groupName}`; diff --git a/www/index.html b/www/index.html index 325be5c..c89abd7 100644 --- a/www/index.html +++ b/www/index.html @@ -342,7 +342,11 @@