TP2
This commit is contained in:
43
scripts/dhcp_config.ps1
Normal file
43
scripts/dhcp_config.ps1
Normal file
@@ -0,0 +1,43 @@
|
||||
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'
|
||||
|
||||
# Install DHCP Role if not present
|
||||
$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 : $_"
|
||||
}
|
||||
32
scripts/dns_config.ps1
Normal file
32
scripts/dns_config.ps1
Normal file
@@ -0,0 +1,32 @@
|
||||
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
|
||||
}
|
||||
|
||||
Import-Module DnsServer -ErrorAction Stop
|
||||
|
||||
$zoneName = 'entreprisexyz.local'
|
||||
$aName = 'srv-dc1'
|
||||
$aIP = '192.168.147.10'
|
||||
$forwarders = @('8.8.8.8','8.8.4.4')
|
||||
|
||||
if (-not (Get-DnsServerZone -Name $zoneName -ErrorAction SilentlyContinue)) {
|
||||
Add-DnsServerPrimaryZone -Name $zoneName -ZoneFile "$zoneName.dns" -DynamicUpdate None -ErrorAction Stop
|
||||
Write-Host "Zone primaire '$zoneName' créée."
|
||||
} else {
|
||||
Write-Host "Zone '$zoneName' existe déjà."
|
||||
}
|
||||
|
||||
if (-not (Get-DnsServerResourceRecord -ZoneName $zoneName -Name $aName -RRType 'A' -ErrorAction SilentlyContinue)) {
|
||||
Add-DnsServerResourceRecordA -Name $aName -ZoneName $zoneName -IPv4Address $aIP -TimeToLive ([TimeSpan]::FromHours(1)) -ErrorAction Stop
|
||||
Write-Host "Enregistrement A ajouté : $aName.$zoneName -> $aIP"
|
||||
} else {
|
||||
Write-Host "Enregistrement A $aName.$zoneName existe déjà."
|
||||
}
|
||||
|
||||
try {
|
||||
Set-DnsServerForwarder -IPAddress $forwarders -ErrorAction Stop
|
||||
Write-Host "Redirecteurs DNS configurés : $($forwarders -join ', ')"
|
||||
} catch {
|
||||
Write-Warning "Échec configuration redirecteurs : $_"
|
||||
}
|
||||
Reference in New Issue
Block a user