# ================================ # PROFIL WĘDRUJĄCY (MOBILNY) IS\agent — Windows Server 2019 # ================================ # --- PARAMETRY (możesz dostosować) --- $DriveLetter = 'E' # Litera woluminu na profile $ProfilesFolderName = 'Profiles' # Nazwa folderu głównego profili $OuName = 'Mandatory' # Nazwa OU dla ucznia $UserName = 'agent' # Konto ucznia $UserPass = 'zaq1@WSX' # Hasło ucznia $ShareName = "${ProfilesFolderName}$" # Udział ukryty (Profiles$) $RootPath = "${DriveLetter}:\${ProfilesFolderName}" # --- Funkcje logujące (kolory) --- function Write-Info($m){ Write-Host $m -ForegroundColor Cyan } function Write-Ok($m){ Write-Host $m -ForegroundColor Green } function Write-Warn($m){ Write-Host $m -ForegroundColor Yellow } function Write-Err($m){ Write-Host $m -ForegroundColor Red } # --- Wymagany moduł ActiveDirectory --- try { Import-Module ActiveDirectory -ErrorAction Stop Write-Info "Załadowano moduł ActiveDirectory." } catch { Write-Err "Brak modułu ActiveDirectory (RSAT). Zainstaluj i uruchom ponownie." throw } # --- Pobierz poprawny DN domeny (RootDSE) --- try { $DomainDN = (Get-ADRootDSE).DefaultNamingContext Write-Info "Wykryto DN domeny: $DomainDN" } catch { Write-Err "Nie można pobrać DN domeny z RootDSE. Sprawdź łączność z DC." throw } # --- Przygotowanie woluminu E: (RAW -> inicjalizacja; istniejący -> format gdy jest E:\Profiles) --- Write-Info "Przygotowanie woluminu ${DriveLetter}: dla profili…" $rawDisk = Get-Disk | Where-Object PartitionStyle -eq 'RAW' | Select-Object -First 1 if ($rawDisk) { Write-Info ("Wykryto dysk RAW #{0}. Inicjuję jako {1}:" -f $rawDisk.Number, $DriveLetter) Initialize-Disk -Number $rawDisk.Number -PartitionStyle GPT New-Partition -DiskNumber $rawDisk.Number -UseMaximumSize -DriveLetter $DriveLetter | Out-Null Format-Volume -DriveLetter $DriveLetter -FileSystem NTFS -NewFileSystemLabel 'ProfilesDisk' -Confirm:$false | Out-Null Write-Ok "Zainicjowano i sformatowano wolumin ${DriveLetter}:" } else { $vol = Get-Volume -DriveLetter $DriveLetter -ErrorAction SilentlyContinue if ($vol) { if (Test-Path $RootPath) { Write-Warn "Katalog $RootPath istnieje — formatuję wolumin ${DriveLetter}: (NTFS, 'ProfilesDisk')." Format-Volume -DriveLetter $DriveLetter -FileSystem NTFS -NewFileSystemLabel 'ProfilesDisk' -Force -Confirm:$false | Out-Null Write-Ok "Format ${DriveLetter}: zakończony." } else { Write-Info "Wolumin ${DriveLetter}: istnieje — pomijam format." } } else { Write-Err "Nie znaleziono dysku RAW ani woluminu ${DriveLetter}: — przerwano." throw "Brak woluminu ${DriveLetter}: i dysku RAW do inicjalizacji." } } # --- Utwórz świeży folder E:\Profiles --- if (Test-Path $RootPath) { Write-Warn "Usuwam istniejący $RootPath i tworzę na czysto…" Remove-Item -Path $RootPath -Force -Recurse -ErrorAction SilentlyContinue } New-Item -Path $RootPath -ItemType Directory | Out-Null Write-Ok "Utworzono katalog: $RootPath" # --- Przygotuj zlokalizowane nazwy kont z SID-ów (działa na PL/EN/itd.) --- $adminsSID = New-Object System.Security.Principal.SecurityIdentifier 'S-1-5-32-544' # BUILTIN\Administrators $authSID = New-Object System.Security.Principal.SecurityIdentifier 'S-1-5-11' # Authenticated Users $adminsNT = $adminsSID.Translate([System.Security.Principal.NTAccount]).Value $authNT = $authSID.Translate([System.Security.Principal.NTAccount]).Value Write-Info "Zmapowano SID-y: Administrators → $adminsNT, Authenticated Users → $authNT" # --- Utwórz / skonfiguruj udział SMB Profiles$ (z użyciem nazw zmapowanych z SID) --- $existingShare = Get-SmbShare -Name $ShareName -ErrorAction SilentlyContinue if (-not $existingShare) { Write-Info "Tworzę udział SMB $ShareName → $RootPath (bez uprawnień, nadam je za chwilę)…" # Najpierw utwórz udział (domyślnie dziedziczy ograniczone uprawnienia) New-SmbShare -Name $ShareName -Path $RootPath -CachingMode None | Out-Null } else { Write-Warn "Udział $ShareName już istnieje." } # Usuń Everyone na udziale (jeśli widnieje) i nadaj prawidłowe uprawnienia try { Revoke-SmbShareAccess -Name $ShareName -AccountName 'Everyone' -Force -ErrorAction SilentlyContinue } catch {} # Nadaj pełną kontrolę dla Administratorów i Użytkowników uwierzytelnionych Grant-SmbShareAccess -Name $ShareName -AccountName $adminsNT -AccessRight Full -Force | Out-Null Grant-SmbShareAccess -Name $ShareName -AccountName $authNT -AccessRight Full -Force | Out-Null Write-Ok "Udział $ShareName skonfigurowany (Full: $adminsNT, $authNT)." # --- Ustaw uprawnienia NTFS na E:\Profiles (po SID — niezależne od PL/EN) --- Write-Info "Konfiguruję NTFS ACL dla $RootPath…" icacls $RootPath /inheritance:r | Out-Null icacls $RootPath /grant "*S-1-5-18:(OI)(CI)(F)" "*S-1-5-32-544:(OI)(CI)(F)" "*S-1-3-0:(OI)(CI)(F)" "*S-1-5-11:(OI)(CI)(M)" | Out-Null Write-Ok "Uprawnienia NTFS ustawione (SID)." # --- Utwórz OU 'Mandatory' (jeśli brak) --- $ouExists = Get-ADOrganizationalUnit -LDAPFilter "(ou=$OuName)" -SearchBase $DomainDN -ErrorAction SilentlyContinue if (-not $ouExists) { Write-Info "Tworzę OU=$OuName w $DomainDN" New-ADOrganizationalUnit -Name $OuName -Path $DomainDN Write-Ok "Utworzono OU=$OuName" } else { Write-Info "OU=$OuName już istnieje." } $OuPath = "OU=${OuName},${DomainDN}" # --- Utwórz użytkownika 'agent' w OU (jeśli brak) --- # Szukaj po samAccountName, bez wyjątku gdy brak: $userExists = Get-ADUser -Filter "samAccountName -eq '$UserName'" -SearchBase $DomainDN -ErrorAction SilentlyContinue if (-not $userExists) { $sec = ConvertTo-SecureString $UserPass -AsPlainText -Force Write-Info "Tworzę użytkownika $UserName w $OuPath…" New-ADUser -Name $UserName -SamAccountName $UserName -UserPrincipalName "$UserName@$(Get-ADDomain).DNSRoot" ` -Path $OuPath -Enabled $true -AccountPassword $sec -PasswordNeverExpires $true -ChangePasswordAtLogon $false Write-Ok "Utworzono użytkownika: $UserName" } else { Write-Info "Użytkownik $UserName już istnieje — uzupełniam właściwości." } # --- Ustaw ścieżkę profilu wędrującego dla 'agent' --- # Windows dopisze sufiks wersji (np. .V6) przy pierwszym logowaniu na Win10/11 $ServerShareUNC = "\\$($env:COMPUTERNAME)\${ShareName}" Set-ADUser -Identity $UserName -ProfilePath "$ServerShareUNC\$UserName" Write-Ok "Ustawiono w AD ProfilePath: $ServerShareUNC\$UserName dla użytkownika $UserName" # --- Odśwież polityki na serwerze --- Write-Info "gpupdate /force …" gpupdate /force | Out-Null Write-Ok "gpupdate zakończone." # === UZUPEŁNIENIE BRAKUJĄCYCH PARAMETRÓW KONTA 'agent' === Import-Module ActiveDirectory $UserName = 'agent' $adDomain = Get-ADDomain $dnsRoot = $adDomain.DNSRoot # np. rol00.edu.pl $netbios = $adDomain.NetBIOSName # np. IS $upn = "$UserName@$dnsRoot" # UPN: agent@rol00.edu.pl # Sprawdź, czy konto istnieje $user = Get-ADUser -Identity $UserName -Properties sn -ErrorAction SilentlyContinue if (-not $user) { Write-Host "Konto '${UserName}' nie istnieje w domenie. Najpierw je utwórz." -ForegroundColor Red return } # 1) Imię + nazwa wyświetlana Set-ADUser -Identity $UserName -GivenName $UserName -DisplayName $UserName # 1a) Wyczyść Nazwisko (LDAP: sn) tylko jeśli jest ustawione if ($user.sn -and $user.sn.Trim().Length -gt 0) { Set-ADUser -Identity $UserName -Clear sn } # 2) Nazwy logowania: # - UPN: agent@ # - sAMAccountName: 'agent' (GUI pokaże \agent dla „Win2000-”) Set-ADUser -Identity $UserName -UserPrincipalName $upn -SamAccountName $UserName # 3) Opcje konta: # - hasło nigdy nie wygasa # - użytkownik nie może zmieniać hasła # - brak wymuszenia zmiany hasła przy następnym logowaniu Set-ADUser -Identity $UserName -PasswordNeverExpires $true -CannotChangePassword $true -ChangePasswordAtLogon $false # (Informacyjne echo) Write-Host "UPN ustawiony: $upn" -ForegroundColor Green Write-Host ("Nazwa logowania (Win2000-): {0}\{1}" -f $netbios, $UserName) -ForegroundColor Green Write-Host "Imię/DisplayName: $UserName | Nazwisko (sn): (wyczyszczone jeśli było) | Hasło nigdy nie wygasa: TAK | Użytkownik nie może zmieniać hasła: TAK | Zmiana hasła przy następnym logowaniu: NIE" -ForegroundColor Green # --- Podsumowanie i wskazówki --- Write-Warn "`n== Na KOMPUTERZE KLIENTA (Windows 10/11) Wybierz ikone sesji podstawowej == TERAZ: zaloguj się jako IS\${UserName}. Po pełnym logowaniu wyloguj się." Write-Warn "Windows utworzy: ${RootPath}\${UserName}.V6 oraz plik NTUSER.DAT (profil wędrujący)." Write-Ok "Część serwerowa dla PROFILU WĘDRUJĄCEGO zakończona."