From 0fe3ce4dfc5eea65cd2ee1273cef0b445ca4c802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0t=C4=9Bp=C3=A1n?= Date: Mon, 25 May 2026 12:57:01 +0000 Subject: [PATCH] Add Get-RDSUsersFromUVHD.ps1 --- Get-RDSUsersFromUVHD.ps1 | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Get-RDSUsersFromUVHD.ps1 diff --git a/Get-RDSUsersFromUVHD.ps1 b/Get-RDSUsersFromUVHD.ps1 new file mode 100644 index 0000000..b433002 --- /dev/null +++ b/Get-RDSUsersFromUVHD.ps1 @@ -0,0 +1,95 @@ +<# +.SYNOPSIS + Zjistí uživatele AD na základě názvů UVHD souborů (User Profile Disks). + +.DESCRIPTION + Prochází složku s UVHD disky, extrahuje SID z názvu souboru + ve formátu UVHD-S-1-5-21-*.vhdx a dohledá odpovídajícího uživatele v AD. + +.PARAMETER UVHDPath + Cesta ke složce s UVHD soubory. + +.PARAMETER ExportCsv + Volitelná cesta pro export výsledků do CSV. + +.EXAMPLE + .\Get-UVHDUsers.ps1 -UVHDPath "\\fileserver\UPD$" + .\Get-UVHDUsers.ps1 -UVHDPath "D:\UPD" -ExportCsv "C:\Temp\uvhd_users.csv" +#> + +[CmdletBinding()] +param ( + [Parameter(Mandatory = $true)] + [string]$UVHDPath, + + [Parameter(Mandatory = $false)] + [string]$ExportCsv +) + +# Ověření dostupnosti AD modulu +if (-not (Get-Module -ListAvailable -Name ActiveDirectory)) { + Write-Error "Modul ActiveDirectory není dostupný. Nainstaluj RSAT nebo spusť na DC/serveru s RSAT." + exit 1 +} + +Import-Module ActiveDirectory + +# Načtení UVHD souborů +$vhdxFiles = Get-ChildItem -Path $UVHDPath -Filter "UVHD-S-1-5-21-*.vhdx" -File -ErrorAction Stop + +if ($vhdxFiles.Count -eq 0) { + Write-Warning "Ve složce '$UVHDPath' nebyly nalezeny žádné UVHD soubory." + exit 0 +} + +Write-Host "Nalezeno $($vhdxFiles.Count) UVHD souborů. Dohledávám uživatele v AD...`n" -ForegroundColor Cyan + +$results = foreach ($file in $vhdxFiles) { + # Extrakce SID z názvu souboru: UVHD-S-1-5-21-xxx-xxx-xxx-xxxx.vhdx + $sidString = $file.BaseName -replace '^UVHD-', '' + + $adUser = $null + $status = $null + + try { + $sid = [System.Security.Principal.SecurityIdentifier]::new($sidString) + $adUser = Get-ADUser -Identity $sid -Properties DisplayName, EmailAddress, Enabled, DistinguishedName -ErrorAction Stop + $status = "OK" + } + catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] { + $status = "Uživatel nenalezen v AD" + } + catch { + $status = "Chyba: $($_.Exception.Message)" + } + + [PSCustomObject]@{ + Soubor = $file.Name + SID = $sidString + SamAccountName = $adUser.SamAccountName + DisplayName = $adUser.DisplayName + Email = $adUser.EmailAddress + Enabled = $adUser.Enabled + DistinguishedName = $adUser.DistinguishedName + VelikostMB = [math]::Round($file.Length / 1MB, 0) + PosledniZmena = $file.LastWriteTime + Status = $status + } +} + +# Výpis do konzole +$results | Format-Table -AutoSize Soubor, SamAccountName, DisplayName, Enabled, VelikostMB, PosledniZmena, Status + +# Souhrn +$ok = ($results | Where-Object { $_.Status -eq "OK" }).Count +$missing = ($results | Where-Object { $_.Status -ne "OK" }).Count + +Write-Host "`nSouhrn:" -ForegroundColor Cyan +Write-Host " Nalezeni v AD : $ok" -ForegroundColor Green +Write-Host " Nenalezeni : $missing" -ForegroundColor $(if ($missing -gt 0) { "Yellow" } else { "Green" }) + +# Volitelný export do CSV +if ($ExportCsv) { + $results | Export-Csv -Path $ExportCsv -NoTypeInformation -Encoding UTF8 + Write-Host "`nVýsledky exportovány do: $ExportCsv" -ForegroundColor Cyan +} \ No newline at end of file