# --- БЛОК НАСТРОЕК ---
$NewHostName = "DC"
$DomainName = "sbcgeek.com"
$NetBIOSName = "SBCGEEK"
$StaticIP = "192.168.1.100"
$MaskPrefix = 24
$Gateway = "192.168.1.1"
$DNSPrimary = "192.168.1.1"
$DSRMPassword = ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
$ScriptPath = "C:\Users\Administrator\Desktop\setup-dc.ps1"
# ---------------------
Set-TimeZone -Id "Russian Standard Time"
# Функция для добавления скрипта в автозапуск после перезагрузки
function Set-RunOnce {
$Command = "powershell.exe -ExecutionPolicy Bypass -File $ScriptPath"
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "DC_Setup" -Value $Command
}
# ЭТАП 1: Настройка сети и имени
if ($env:COMPUTERNAME -ne $NewHostName) {
Write-Host "ЭТАП 1: Настройка сети и имени..." -ForegroundColor Cyan
# Настройка IP
$Interface = Get-NetAdapter | Where-Object { $_.Status -eq "Up" } | Select-Object -First 1
New-NetIPAddress -InterfaceIndex $Interface.InterfaceIndex -IPAddress $StaticIP -PrefixLength $MaskPrefix -DefaultGateway $Gateway -Confirm:$false
Set-DnsClientServerAddress -InterfaceIndex $Interface.InterfaceIndex -ServerAddresses $DNSPrimary
# Добавляем скрипт в RunOnce и переименовываем
Set-RunOnce
Rename-Computer -NewName $NewHostName -Force
Restart-Computer -Force
exit
}
# ЭТАП 2: Установка роли AD DS
if (!(Get-WindowsFeature -Name AD-Domain-Services).Installed) {
Write-Host "ЭТАП 2: Установка роли AD DS..." -ForegroundColor Cyan
Set-RunOnce
Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools
Restart-Computer -Force
exit
}
# ЭТАП 3: Развертывание леса AD
# Проверяем роль сервера через WMI. 4 и 5 — это Domain Controller.
$DomainRole = (Get-CimInstance Win32_ComputerSystem).DomainRole
if ($DomainRole -lt 4) {
Write-Host "ЭТАП 3: Развертывание леса Active Directory..." -ForegroundColor Cyan
Import-Module ADDSDeployment
Install-ADDSForest `
-CreateDnsDelegation:$false `
-DatabasePath "C:\Windows\NTDS" `
-DomainMode "WinThreshold" `
-DomainName $DomainName `
-DomainNetbiosName $NetBIOSName `
-ForestMode "WinThreshold" `
-InstallDns:$true `
-LogPath "C:\Windows\NTDS" `
-NoRebootOnCompletion:$false `
-SysvolPath "C:\Windows\SYSVOL" `
-SafeModeAdministratorPassword $DSRMPassword `
-Force:$true
} else {
Write-Host "Сервер уже является контроллером домена (DomainRole: $DomainRole)." -ForegroundColor Green
# Удаляем файл скрипта, если нужно
Remove-Item $ScriptPath -Force
}