@@ -24,7 +24,6 @@ function Get-PrefixLength {
2424 }
2525}
2626
27-
2827# Define the path to save the configuration securely
2928$configPath = " $env: USERPROFILE \static_ip_config.xml"
3029
@@ -118,10 +117,50 @@ function Set-DHCP {
118117
119118 Write-Host " Switching to DHCP configuration..." - ForegroundColor Cyan
120119
121- Set-NetIPInterface - InterfaceAlias $InterfaceName - Dhcp Enabled - ErrorAction Stop
122- Set-DnsClientServerAddress - InterfaceAlias $InterfaceName - ResetServerAddresses - ErrorAction Stop
120+ try {
121+ # Validate interface alias
122+ $interface = Get-NetIPInterface - InterfaceAlias $InterfaceName - ErrorAction Stop
123+
124+ Write-Host " Releasing IP address..." - ForegroundColor Yellow
125+ Remove-NetIPAddress - InterfaceAlias $InterfaceName - Confirm:$false - ErrorAction Stop
126+
127+ Write-Host " Flushing DNS cache..." - ForegroundColor Yellow
128+ Clear-DnsClientCache - ErrorAction Stop
129+
130+ Write-Host " Enabling DHCP..." - ForegroundColor Yellow
131+ Set-NetIPInterface - InterfaceAlias $InterfaceName - Dhcp Enabled - ErrorAction Stop
132+ Set-DnsClientServerAddress - InterfaceAlias $InterfaceName - ResetServerAddresses - ErrorAction Stop
133+
134+ Write-Host " Renewing DHCP lease..." - ForegroundColor Yellow
135+ ipconfig / release > $null 2>&1
136+ ipconfig / renew > $null 2>&1
137+
138+ Start-Sleep - Seconds 5 # Wait for DHCP process
123139
124- Write-Host " DHCP configuration applied successfully." - ForegroundColor Green
140+ # Fetch IP configuration
141+ $ipConfig = Get-NetIPConfiguration - InterfaceAlias $InterfaceName
142+
143+ if ($ipConfig.IPv4Address.IPAddress -like " 169.254.*" ) {
144+ Write-Host " Error: Still assigned an APIPA address. Possible network issue." - ForegroundColor Red
145+ Write-Host " Performing dynamic ping test..." - ForegroundColor Cyan
146+ $gateway = $ipConfig.IPv4DefaultGateway.NextHop
147+ if ($gateway ) {
148+ Test-Connection - ComputerName $gateway - Count 4 | Format-Table - AutoSize
149+ } else {
150+ Write-Host " No gateway detected. Please verify network connectivity." - ForegroundColor Red
151+ }
152+ } else {
153+ Write-Host " DHCP configuration applied successfully!" - ForegroundColor Green
154+ Write-Host " IP Address: $ ( $ipConfig.IPv4Address.IPAddress ) " - ForegroundColor Cyan
155+ Write-Host " Subnet Mask: /$ ( $ipConfig.IPv4Address.PrefixLength ) " - ForegroundColor Cyan
156+ Write-Host " Default Gateway: $ ( $ipConfig.IPv4DefaultGateway.NextHop ) " - ForegroundColor Cyan
157+ Write-Host " DNS Servers: $ ( $ipConfig.DnsServer.ServerAddresses -join ' , ' ) " - ForegroundColor Cyan
158+ }
159+ } catch {
160+ Write-Host " Error: Unable to apply DHCP configuration. Please check the interface name and network settings." - ForegroundColor Red
161+ Write-Host " Available interfaces:" - ForegroundColor Yellow
162+ Get-NetIPInterface | Select-Object - Property InterfaceAlias, AddressFamily, Dhcp
163+ }
125164}
126165
127166# Function to show IP configuration
0 commit comments