Add Get-RDSUsersFromUVHD.ps1

This commit is contained in:
2026-05-25 12:57:01 +00:00
parent 5f46fb4de4
commit 0fe3ce4dfc
+95
View File
@@ -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
}