43 lines
1.8 KiB
PowerShell
43 lines
1.8 KiB
PowerShell
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
|
|
Write-Error "Exécutez ce script en tant qu'administrateur."
|
|
exit 1
|
|
}
|
|
|
|
$ScopeName = 'LAN_EntrepriseXYZ'
|
|
$StartIP = '192.168.147.200'
|
|
$EndIP = '192.168.147.220'
|
|
$SubnetMask = '255.255.255.0'
|
|
$ScopeId = '192.168.147.0'
|
|
$DnsServer = '192.168.147.10'
|
|
$Router = '192.168.147.1'
|
|
$Domain = 'entreprisexyz.local'
|
|
$ResName = 'Admin-PC'
|
|
$ResIP = '192.168.147.201'
|
|
$ResMac = '00-11-22-33-44-55'
|
|
|
|
|
|
$dhcpFeature = Get-WindowsFeature -Name 'DHCP'
|
|
if (-not $dhcpFeature.Installed) {
|
|
Write-Host "Installation du rôle DHCP..."
|
|
Install-WindowsFeature -Name 'DHCP' -IncludeManagementTools -ErrorAction Stop | Out-Null
|
|
Write-Host "Rôle DHCP installé." -ForegroundColor Green
|
|
}
|
|
|
|
try {
|
|
if (-not (Get-DhcpServerv4Scope -ScopeId $ScopeId -ErrorAction SilentlyContinue)) {
|
|
Add-DhcpServerv4Scope -Name $ScopeName -StartRange $StartIP -EndRange $EndIP -SubnetMask $SubnetMask -State Active -LeaseDuration (New-TimeSpan -Days 8) -ErrorAction Stop
|
|
Write-Host "Étendue '$ScopeName' créée : $StartIP - $EndIP"
|
|
}
|
|
else {
|
|
Write-Host "Étendue '$ScopeName' existe déjà."
|
|
}
|
|
|
|
Set-DhcpServerv4OptionValue -ScopeId $ScopeId -DnsServer $DnsServer -Router $Router -DnsDomain $Domain -ErrorAction Stop
|
|
Write-Host "Options DHCP configurées : DNS=$DnsServer, Passerelle=$Router, Domaine=$Domain"
|
|
|
|
Add-DhcpServerv4Reservation -ScopeId $ScopeId -IPAddress $ResIP -ClientId $ResMac -Name $ResName -Description 'Réservation poste administratif' -ErrorAction Stop
|
|
Write-Host "Réservation ajoutée : $ResName -> $ResIP ($ResMac)"
|
|
}
|
|
catch {
|
|
Write-Warning "Opération échouée : $_"
|
|
} |