<# .SYNOPSIS Bölüm 00 · Adım 0.4 — Altın VHDX Kilitleme, Differencing Disk Üretimi ve DC01 İlk Açılışı .DESCRIPTION Sysprep sonrası kapanan WS2025-Gold-Builder üzerindeki olası .avhdx farklarını ana diske birleştirir, geçici builder VM tanımını siler, WS2025-Gold.vhdx dosyasını sıkıştırıp Salt Okunur (IsReadOnly=True) kilitler, 4 MB differencing disk ile DC01 ve SYNC01 makinelerini üretir ve DC01'i ilk OOBE için açar. .EXAMPLE powershell -ExecutionPolicy Bypass -File "D:\Lab\Scripts\03-Seal-GoldAndSpawn-DC01.ps1" #> #Requires -RunAsAdministrator param( [string]$LabRoot = "D:\Lab", [string]$BuilderVM = "WS2025-Gold-Builder", [string]$SwitchName = "Lab-Internal-NAT", [string]$DcName = "DC01", [string]$SyncName = "SYNC01" ) $ErrorActionPreference = "Stop" $GoldVhd = Join-Path $LabRoot "BaseImages\WS2025-Gold.vhdx" if (-not (Test-Path $GoldVhd)) { throw "Altın imaj bulunamadı: $GoldVhd. Önce Adım 0.3'ü tamamlayın." } $vm = Get-VM -Name $BuilderVM -ErrorAction SilentlyContinue if ($vm) { if ($vm.State -ne 'Off') { throw "$BuilderVM hâlâ '$($vm.State)' durumunda. Sysprep kapanışı tamamlanmadan veya disk etkinliği bitmeden mühürleme yapılamaz." } Write-Host "[1/5] Olası otomatik checkpoint (.avhdx) ana VHDX içine birleştiriliyor..." -ForegroundColor Cyan Set-VM -Name $BuilderVM -AutomaticCheckpointsEnabled $false $snaps = Get-VMSnapshot -VMName $BuilderVM -ErrorAction SilentlyContinue if ($snaps) { $snaps | Remove-VMSnapshot -IncludeAllChildSnapshots while (Get-ChildItem (Join-Path $LabRoot "BaseImages") -Filter "WS2025-Gold*.avhdx" -ErrorAction SilentlyContinue) { Start-Sleep -Seconds 2 } } Write-Host "[2/5] Geçici $BuilderVM VM tanımı kaldırılıyor (WS2025-Gold.vhdx korunur)..." -ForegroundColor Cyan Remove-VM -Name $BuilderVM -Force } $goldSizeGB = [math]::Round((Get-Item $GoldVhd).Length / 1GB, 2) if ($goldSizeGB -lt 5) { throw "WS2025-Gold.vhdx beklenenden küçük ($goldSizeGB GB). Kurulum veya disk birleştirme tamamlanmamış." } if (-not (Get-Item $GoldVhd).IsReadOnly) { Write-Host "[3/5] WS2025-Gold.vhdx ($goldSizeGB GB) sıkıştırılıyor ve Salt Okunur (IsReadOnly=True) kilitleniyor..." -ForegroundColor Cyan Optimize-VHD -Path $GoldVhd -Mode Full Set-ItemProperty -Path $GoldVhd -Name IsReadOnly -Value $true } else { Write-Host "[3/5] WS2025-Gold.vhdx zaten Salt Okunur durumda." -ForegroundColor Yellow } function New-LabServerVM { param( [Parameter(Mandatory)][string]$Name, [int64]$StartupRAM = 4GB, [int64]$MaxRAM = 4GB, [int]$vCPU = 2 ) $VmPath = Join-Path $LabRoot "VMs\$Name" $DiffVHD = Join-Path $VmPath "$Name.vhdx" New-Item -ItemType Directory -Path $VmPath -Force | Out-Null if (-not (Test-Path $DiffVHD)) { New-VHD -Path $DiffVHD -ParentPath $GoldVhd -Differencing | Out-Null } if (-not (Get-VM -Name $Name -ErrorAction SilentlyContinue)) { New-VM -Name $Name -MemoryStartupBytes $StartupRAM -Generation 2 -VHDPath $DiffVHD -SwitchName $SwitchName | Out-Null } Set-VM -Name $Name -ProcessorCount $vCPU -DynamicMemory -MemoryMinimumBytes 2GB -MemoryMaximumBytes $MaxRAM -CheckpointType Standard -AutomaticCheckpointsEnabled $false Set-VMFirmware -VMName $Name -EnableSecureBoot On Set-VMKeyProtector -VMName $Name -NewLocalKeyProtector Enable-VMTPM -VMName $Name } Write-Host "[4/5] Differencing Disk ile $DcName ve $SyncName üretiliyor..." -ForegroundColor Cyan New-LabServerVM -Name $DcName -StartupRAM 4GB -MaxRAM 4GB -vCPU 2 New-LabServerVM -Name $SyncName -StartupRAM 4GB -MaxRAM 4GB -vCPU 2 Write-Host "[5/5] Disk doğrulama tablosu:" -ForegroundColor Green Get-Item $GoldVhd, (Join-Path $LabRoot "VMs\$DcName\$DcName.vhdx"), (Join-Path $LabRoot "VMs\$SyncName\$SyncName.vhdx") | Select-Object Name, IsReadOnly, @{N='SizeMB';E={[math]::Round($_.Length/1MB,2)}} | Format-Table -AutoSize Write-Host "$DcName ilk OOBE kurulumu için başlatılıyor..." -ForegroundColor Green Start-VM -Name $DcName vmconnect.exe localhost $DcName