Created
September 6, 2024 17:19
-
-
Save djad442/705d05aa5d50aa17a464ab6c4c5ba60e to your computer and use it in GitHub Desktop.
Powershell Script to extract dhcp config from windows server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the DHCP Server | |
$dhcpServer = "MYSERVER" # Replace with the name or IP address of your DHCP server | |
# Import the DHCP Server Module (in case it's not loaded) | |
Import-Module DhcpServer | |
# Output file for the Linux-compatible dhcpd.conf | |
$outputFile = "C:\dhcpd.conf" | |
# Clear the file if it exists, to avoid appending to old data | |
if (Test-Path $outputFile) { | |
Remove-Item $outputFile | |
} | |
# Retrieve all DHCP scopes (IPv4) | |
$scopes = Get-DhcpServerv4Scope -ComputerName $dhcpServer | |
# Retrieve DHCP global options (like DNS, Gateway, etc.) | |
$globalOptions = Get-DhcpServerv4OptionValue -ComputerName $dhcpServer -ScopeId 0 | |
# Process each scope to create Linux-compatible config | |
foreach ($scope in $scopes) { | |
$scopeID = $scope.ScopeId | |
$subnetMask = $scope.SubnetMask | |
$leaseRangeStart = $scope.StartRange | |
$leaseRangeEnd = $scope.EndRange | |
$leaseTime = $scope.LeaseDuration.TotalSeconds | |
# Write the scope block for each scope | |
Add-Content -Path $outputFile -Value @" | |
subnet $scopeID netmask $subnetMask { | |
range $leaseRangeStart $leaseRangeEnd; | |
default-lease-time $leaseTime; | |
max-lease-time $leaseTime; | |
"@ | |
# Get scope-specific options | |
$scopeOptions = Get-DhcpServerv4OptionValue -ComputerName $dhcpServer -ScopeId $scopeID | |
# Write DHCP options for the scope | |
foreach ($option in $scopeOptions) { | |
switch ($option.OptionId) { | |
3 { # Router/Gateway | |
$gateway = ($option.Value) | |
Write-Host "Data $gateway" | |
Add-Content -Path $outputFile -Value " option routers $gateway;" | |
} | |
6 { # DNS Servers | |
$dnsServers = ($option.Value) -join ", " | |
Add-Content -Path $outputFile -Value " option domain-name-servers $dnsServers;" | |
} | |
15 { # Domain Name | |
$domainName = ($option.Value) | |
Add-Content -Path $outputFile -Value " option domain-name $domainName;" | |
} | |
# Add more options as needed | |
} | |
} | |
# Get DHCP reservations (static leases) for the scope | |
$reservations = Get-DhcpServerv4Reservation -ComputerName $dhcpServer -ScopeId $scopeID | |
# Write reservation hosts into the scope block | |
foreach ($reservation in $reservations) { | |
$reservationIP = $reservation.IPAddress | |
$reservationMAC = $reservation.ClientId | |
$reservationHostname = $reservation.Name | |
Add-Content -Path $outputFile -Value @" | |
host $reservationHostname { | |
hardware ethernet $reservationMAC; | |
fixed-address $reservationIP; | |
} | |
"@ | |
} | |
# End of the scope block | |
Add-Content -Path $outputFile -Value "}\n" | |
} | |
# Add global options to the configuration file (if needed) | |
foreach ($option in $globalOptions) { | |
switch ($option.OptionId) { | |
3 { # Router/Gateway | |
$gateway = ($option.Value).Data | |
Add-Content -Path $outputFile -Value "option routers $gateway;" | |
} | |
6 { # DNS Servers | |
$dnsServers = ($option.Value).Data -join ", " | |
Add-Content -Path $outputFile -Value "option domain-name-servers $dnsServers;" | |
} | |
15 { # Domain Name | |
$domainName = ($option.Value).Data | |
Add-Content -Path $outputFile -Value "option domain-name \"$domainName\";" | |
} | |
# Add more global options as needed | |
} | |
} | |
Write-Host "DHCP configuration, including reservations, has been exported to $outputFile in Linux-compatible format." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment