<# .SYNOPSIS CanerCetinkaya Enterprise Security Lab — Bağımlılık Sıralı Hyper-V Güç ve Isı Yöneticisi .DESCRIPTION İşiniz bittiğinde sanal makineleri ters bağımlılık sırasıyla (WIN11 -> MECM/SYNC -> DC01 en son) güvenli kapatır; çalışacağınız zaman yalnızca ihtiyaç duyduğunuz profili (Core, Identity, Hybrid, CloudOnly, MECM, Full) doğru sırayla (önce DC01 AD DS/DNS, sonra sunucu ve istemciler) açar. .EXAMPLE pwsh -File "D:\Lab\Scripts\Manage-LabPower.ps1" -Action Status pwsh -File "D:\Lab\Scripts\Manage-LabPower.ps1" -Action Stop pwsh -File "D:\Lab\Scripts\Manage-LabPower.ps1" -Action Start -Profile Core #> param( [ValidateSet('Status', 'Start', 'Stop')] [string]$Action = 'Status', [ValidateSet('Core', 'Identity', 'Hybrid', 'CloudOnly', 'MECM', 'Full')] [string]$Profile = 'Full', [string[]]$VMs ) $ErrorActionPreference = 'Stop' $Profiles = @{ Core = @('DC01') Identity = @('DC01', 'SYNC01') Hybrid = @('DC01', 'SYNC01', 'WIN11-01') CloudOnly = @('WIN11-02') MECM = @('DC01', 'MECM01', 'WIN11-01') Full = @('DC01', 'SYNC01', 'MECM01', 'WIN11-01', 'WIN11-02') } $LabVmNames = @('DC01', 'SYNC01', 'MECM01', 'WIN11-01', 'WIN11-02') function Show-LabStatus { Write-Host "`n=== HYPER-V LAB DURUMU & KAYNAK KULLANIMI ===" -ForegroundColor Cyan $vms = @(Get-VM -Name $LabVmNames -ErrorAction SilentlyContinue | Sort-Object Name) if (-not $vms) { Write-Host "Henüz oluşturulmuş laboratuvar VM'i bulunamadı." -ForegroundColor Yellow return } $vms | Select-Object Name, State, @{N='CPU_%';E={$_.CPUUsage}}, @{N='RAM_GB';E={[math]::Round($_.MemoryAssigned/1GB, 2)}}, @{N='Uptime';E={ if ($_.State -eq 'Running') { $_.Uptime.ToString('hh\:mm\:ss') } else { '-' } }} | Format-Table -AutoSize $running = @($vms | Where-Object State -eq 'Running') $totalRam = [math]::Round((($running | Measure-Object -Property MemoryAssigned -Sum).Sum / 1GB), 2) Write-Host "Çalışan VM: $($running.Count)/$($vms.Count) | Toplam Ayrılan RAM: $totalRam GB" -ForegroundColor Green } function Wait-VmOff([string[]]$names, [int]$timeoutSec = 120) { $sw = [Diagnostics.Stopwatch]::StartNew() while ($sw.Elapsed.TotalSeconds -lt $timeoutSec) { $stillRunning = Get-VM -Name $names -ErrorAction SilentlyContinue | Where-Object { $_.State -ne 'Off' } if (-not $stillRunning) { return $true } Start-Sleep -Seconds 3 } return $false } function Stop-LabGracefully([string[]]$targetNames) { $phase1 = $targetNames | Where-Object { $_ -in @('WIN11-01', 'WIN11-02') } $phase2 = $targetNames | Where-Object { $_ -in @('MECM01', 'SYNC01') } $phase3 = $targetNames | Where-Object { $_ -eq 'DC01' } foreach ($phase in @( @{ Label = 'Faz 1 (İstemciler: WIN11-01 / WIN11-02)'; Names = $phase1 }, @{ Label = 'Faz 2 (Sunucular: MECM01 SQL / SYNC01)'; Names = $phase2 }, @{ Label = 'Faz 3 (Domain Controller: DC01 - En Son)'; Names = $phase3 } )) { if (-not $phase.Names -or $phase.Names.Count -eq 0) { continue } $running = @(Get-VM -Name $phase.Names -ErrorAction SilentlyContinue | Where-Object { $_.State -eq 'Running' }) if (-not $running) { continue } Write-Host "`n[*] $($phase.Label) güvenli kapatılıyor..." -ForegroundColor Yellow foreach ($vm in $running) { Write-Host " -> Stop-VM (ACPI Shutdown): $($vm.Name)" Stop-VM -Name $vm.Name -AsJob | Out-Null } if (-not (Wait-VmOff -names ($running.Name) -timeoutSec 120)) { Get-VM -Name $running.Name | Where-Object State -ne 'Off' | Stop-VM -Force } Write-Host " [+] $($phase.Label) kapandı." -ForegroundColor Green } Get-Job | Where-Object State -in @('Completed','Failed') | Remove-Job -Force -ErrorAction SilentlyContinue Show-LabStatus } function Start-LabOrdered([string[]]$targetNames) { if ($targetNames -contains 'DC01') { $dc = Get-VM -Name 'DC01' -ErrorAction SilentlyContinue if ($dc -and $dc.State -ne 'Running') { Write-Host "`n[*] Faz 1: DC01 başlatılıyor..." -ForegroundColor Cyan Start-VM -Name 'DC01' $sw = [Diagnostics.Stopwatch]::StartNew() while ($sw.Elapsed.TotalSeconds -lt 90) { if ((Get-VMIntegrationService -VMName 'DC01' -Name 'Heartbeat').PrimaryStatusDescription -eq 'OK') { break } Start-Sleep -Seconds 3 } Start-Sleep -Seconds 15 } } foreach ($name in ($targetNames | Where-Object { $_ -in @('SYNC01', 'MECM01') })) { $vm = Get-VM -Name $name -ErrorAction SilentlyContinue if ($vm -and $vm.State -ne 'Running') { Start-VM -Name $name; Start-Sleep -Seconds 10 } } foreach ($name in ($targetNames | Where-Object { $_ -in @('WIN11-01', 'WIN11-02') })) { $vm = Get-VM -Name $name -ErrorAction SilentlyContinue if ($vm -and $vm.State -ne 'Running') { Start-VM -Name $name; Start-Sleep -Seconds 8 } } Show-LabStatus } switch ($Action) { 'Status' { Show-LabStatus } 'Stop' { Stop-LabGracefully -targetNames $(if ($VMs) { $VMs } else { $LabVmNames }) } 'Start' { Start-LabOrdered -targetNames $(if ($VMs) { $VMs } else { $Profiles[$Profile] }) } }