@@ -11,8 +11,7 @@ function Get-PrefixLength {
1111 if ($SubnetInput -match " ^\d+(\.\d+){3}$" ) {
1212 # It's a subnet mask like 255.255.255.0
1313 $binarySubnetMask = [Convert ]::ToString([IPAddress ]::Parse($SubnetInput ).Address, 2 ).PadLeft(32 , ' 0' )
14- $prefixLength = ($binarySubnetMask -split ' ' ).Where ({ $_ -eq ' 1' }).Count
15- return $prefixLength
14+ return ($binarySubnetMask -split ' ' ).Where ({ $_ -eq ' 1' }).Count
1615 } elseif ($SubnetInput -match " ^\d+$" ) {
1716 # It's a prefix length like 24
1817 return [int ]$SubnetInput
@@ -26,6 +25,22 @@ function Get-PrefixLength {
2625
2726# Define the path to save the configuration securely
2827$configPath = " $env: USERPROFILE \static_ip_config.xml"
28+ $interfacePath = " $env: USERPROFILE \selected_interface.txt"
29+
30+ # Function to save selected interface
31+ function Save-SelectedInterface {
32+ param ([string ]$InterfaceName )
33+ Set-Content - Path $interfacePath - Value $InterfaceName
34+ }
35+
36+ # Function to load selected interface
37+ function Load-SelectedInterface {
38+ if (Test-Path $interfacePath ) {
39+ return Get-Content - Path $interfacePath
40+ } else {
41+ return $null
42+ }
43+ }
2944
3045# Function to save static IP configuration
3146function Save-StaticIPConfig {
@@ -59,22 +74,6 @@ function Load-StaticIPConfig {
5974 }
6075}
6176
62- # Function to manually enter static IP configuration
63- function Enter-StaticIPConfig {
64- Write-Host " Enter the static IP configuration:" - ForegroundColor Cyan
65- $IPAddress = Read-Host " Enter IP Address (e.g., 192.168.1.25)"
66- $SubnetMask = Read-Host " Enter Subnet Mask or Prefix Length (e.g., 255.255.255.0, 24, or /24)"
67- $Gateway = Read-Host " Enter Gateway (e.g., 192.168.1.1)"
68- $PrimaryDNS = Read-Host " Enter Primary DNS (e.g., 1.1.1.1)"
69- $SecondaryDNS = Read-Host " Enter Secondary DNS (e.g., 1.0.0.1)"
70-
71- # Validate and convert subnet input
72- $prefixLength = Get-PrefixLength - SubnetInput $SubnetMask
73-
74- # Save both forms for future use
75- Save-StaticIPConfig - IPAddress $IPAddress - SubnetMask $SubnetMask - Gateway $Gateway - PrimaryDNS $PrimaryDNS - SecondaryDNS $SecondaryDNS
76- }
77-
7877# Function to set static IP configuration
7978function Set-StaticIP {
8079 param (
@@ -88,6 +87,7 @@ function Set-StaticIP {
8887
8988 Write-Host " Setting static IP configuration..." - ForegroundColor Cyan
9089
90+ # Validate and convert subnet input
9191 $prefixLength = Get-PrefixLength - SubnetInput $SubnetMask
9292
9393 # Remove existing IP configurations to avoid conflicts
@@ -111,7 +111,6 @@ function Set-StaticIP {
111111 Write-Host " Static IP configuration applied successfully." - ForegroundColor Green
112112}
113113
114- # Function to set DHCP configuration
115114function Set-DHCP {
116115 param ([string ]$InterfaceName )
117116
@@ -167,7 +166,7 @@ function Set-DHCP {
167166function Show-IPInfo {
168167 param ([string ]$InterfaceName )
169168
170- Write-Host " Fetching current IP configuration..." - ForegroundColor Cyan
169+ Write-Host " Fetching current IP configuration for interface: $InterfaceName ..." - ForegroundColor Cyan
171170
172171 $ipInfo = Get-NetIPAddress - InterfaceAlias $InterfaceName - ErrorAction SilentlyContinue
173172 $dnsInfo = Get-DnsClientServerAddress - InterfaceAlias $InterfaceName - ErrorAction SilentlyContinue
@@ -193,66 +192,151 @@ function Show-IPInfo {
193192 }
194193}
195194
196- # Function to select network interface
195+ # Function to select network interface with advanced options
197196function Select-NetworkInterface {
198- $interfaces = Get-NetAdapter | Where-Object { $_.Status -eq " Up" }
199- if ($interfaces.Count -eq 0 ) {
200- Write-Host " No active network interfaces found." - ForegroundColor Red
201- exit
202- }
197+ $showDownInterfaces = $false # Toggle for showing/hiding down interfaces
198+
199+ while ($true ) {
200+ # Filter interfaces based on the toggle
201+ if ($showDownInterfaces ) {
202+ $interfaces = Get-NetAdapter
203+ } else {
204+ $interfaces = Get-NetAdapter | Where-Object { $_.Status -eq " Up" }
205+ }
206+
207+ if ($interfaces.Count -eq 0 ) {
208+ Write-Host " No network interfaces found with the current filter." - ForegroundColor Red
209+ $showDownInterfaces = $true # Automatically show down interfaces in case of no results
210+ continue
211+ }
203212
204- Write-Host " Available Network Interfaces:" - ForegroundColor Cyan
205- $interfaces | ForEach-Object { Write-Host " $ ( $_.InterfaceIndex ) : $ ( $_.Name ) " }
213+ Write-Host " `n Available Network Interfaces:" - ForegroundColor Cyan
214+ $interfaces | ForEach-Object {
215+ Write-Host " $ ( $_.InterfaceIndex ) : $ ( $_.Name ) (MAC: $ ( $_.MacAddress ) ) - Status: $ ( $_.Status ) "
216+ }
206217
207- $selectedIndex = Read-Host " Enter the number corresponding to the desired interface"
208- $selectedInterface = $interfaces | Where-Object { $_.InterfaceIndex -eq [int ]$selectedIndex }
218+ Write-Host " `n Options:"
219+ Write-Host " Enter the number corresponding to the desired interface."
220+ Write-Host " Press 'r' to rescan interfaces."
221+ Write-Host " Press 't' to toggle hiding/unhiding down interfaces."
222+ Write-Host " Press 'q' to quit interface selection."
209223
210- if ($null -eq $selectedInterface ) {
211- Write-Host " Invalid selection. Exiting..." - ForegroundColor Red
212- exit
213- }
224+ $input = Read-Host " Your choice"
214225
215- Write-Host " Selected Interface: $ ( $selectedInterface.Name ) " - ForegroundColor Green
216- return $selectedInterface.Name
226+ switch ($input.ToLower ()) {
227+ " r" {
228+ Write-Host " Rescanning interfaces..." - ForegroundColor Yellow
229+ continue # Rescan interfaces
230+ }
231+ " t" {
232+ $showDownInterfaces = -not $showDownInterfaces
233+ Write-Host " Toggled interface visibility. Showing down interfaces: $showDownInterfaces " - ForegroundColor Yellow
234+ continue # Refresh list
235+ }
236+ " q" {
237+ Write-Host " Exiting interface selection..." - ForegroundColor Cyan
238+ return $null
239+ }
240+ default {
241+ if ($input -match " ^\d+$" ) {
242+ $inputInt = [int ]$input # Convert input to integer
243+ $selectedInterface = $interfaces | Where-Object { $_.InterfaceIndex -eq $inputInt }
244+ if ($null -ne $selectedInterface ) {
245+ Write-Host " Selected Interface: $ ( $selectedInterface.Name ) " - ForegroundColor Green
246+ Save-SelectedInterface - InterfaceName $selectedInterface.Name
247+ return $selectedInterface.Name
248+ } else {
249+ Write-Host " Invalid selection. Please try again." - ForegroundColor Red
250+ }
251+ } else {
252+ Write-Host " Invalid input. Please enter a valid number." - ForegroundColor Red
253+ }
254+ }
255+ }
256+ }
217257}
218258
219259# Main logic
220- $interfaceName = Select-NetworkInterface
260+ $interfaceName = Load- SelectedInterface
261+ if (-not $interfaceName ) {
262+ $interfaceName = Select-NetworkInterface
263+ }
221264
222265while ($true ) {
223- Write-Host " Please select an option:" - ForegroundColor Yellow
224- Write-Host " 1. Set static IP configuration (use saved settings) "
266+ Write-Host " `n Please select an option:" - ForegroundColor Yellow
267+ Write-Host " 1. Set static IP configuration manually "
225268 Write-Host " 2. Set DHCP configuration"
226269 Write-Host " 3. Show current IP configuration"
227270 Write-Host " 4. Enter and save static IP configuration"
228- Write-Host " 5. Exit"
271+ Write-Host " 5. Load saved static IP configuration"
272+ Write-Host " 6. Change network interface"
273+ Write-Host " 7. Exit"
229274
230275 $choice = Read-Host " Enter your choice"
231276
232277 switch ($choice ) {
233278 " 1" {
279+ # Set static IP configuration manually
280+ $IPAddress = Read-Host " Enter IP Address (e.g., 192.168.1.25)"
281+ $SubnetMask = Read-Host " Enter Subnet Mask (e.g., 255.255.255.0)"
282+ $Gateway = Read-Host " Enter Gateway (e.g., 192.168.1.1)"
283+ $PrimaryDNS = Read-Host " Enter Primary DNS (e.g., 1.1.1.1)"
284+ $SecondaryDNS = Read-Host " Enter Secondary DNS (e.g., 1.0.0.1)"
285+ Set-StaticIP - InterfaceName $interfaceName `
286+ - IPAddress $IPAddress `
287+ - SubnetMask $SubnetMask `
288+ - Gateway $Gateway `
289+ - PrimaryDNS $PrimaryDNS `
290+ - SecondaryDNS $SecondaryDNS
291+ }
292+ " 2" {
293+ # Set DHCP configuration
294+ Set-DHCP - InterfaceName $interfaceName
295+ }
296+ " 3" {
297+ # Show current IP configuration
298+ Show-IPInfo - InterfaceName $interfaceName
299+ }
300+ " 4" {
301+ # Save static IP configuration
302+ $IPAddress = Read-Host " Enter IP Address (e.g., 192.168.1.25)"
303+ $SubnetMask = Read-Host " Enter Subnet Mask (e.g., 255.255.255.0)"
304+ $Gateway = Read-Host " Enter Gateway (e.g., 192.168.1.1)"
305+ $PrimaryDNS = Read-Host " Enter Primary DNS (e.g., 1.1.1.1)"
306+ $SecondaryDNS = Read-Host " Enter Secondary DNS (e.g., 1.0.0.1)"
307+ Save-StaticIPConfig - IPAddress $IPAddress `
308+ - SubnetMask $SubnetMask `
309+ - Gateway $Gateway `
310+ - PrimaryDNS $PrimaryDNS `
311+ - SecondaryDNS $SecondaryDNS
312+ }
313+ " 5" {
314+ # Load and apply saved static IP configuration
234315 $config = Load- StaticIPConfig
235316 if ($config ) {
317+ Write-Host " Loaded Configuration:" - ForegroundColor Green
318+ Write-Host " IP Address: $ ( $config.IPAddress ) "
319+ Write-Host " Subnet Mask: $ ( $config.SubnetMask ) "
320+ Write-Host " Gateway: $ ( $config.Gateway ) "
321+ Write-Host " Primary DNS: $ ( $config.PrimaryDNS ) "
322+ Write-Host " Secondary DNS: $ ( $config.SecondaryDNS ) "
323+
236324 Set-StaticIP - InterfaceName $interfaceName `
237325 - IPAddress $config.IPAddress `
238326 - SubnetMask $config.SubnetMask `
239327 - Gateway $config.Gateway `
240328 - PrimaryDNS $config.PrimaryDNS `
241329 - SecondaryDNS $config.SecondaryDNS
242330 } else {
243- Write-Host " No saved configuration to apply ." - ForegroundColor Red
331+ Write-Host " No static IP configuration found. Please ensure the configuration is saved ." - ForegroundColor Yellow
244332 }
245333 }
246- " 2" {
247- Set-DHCP - InterfaceName $interfaceName
248- }
249- " 3" {
250- Show-IPInfo - InterfaceName $interfaceName
251- }
252- " 4" {
253- Enter-StaticIPConfig
334+ " 6" {
335+ # Change network interface
336+ $interfaceName = Select-NetworkInterface
254337 }
255- " 5" {
338+ " 7" {
339+ # Exit the script
256340 Write-Host " Exiting..." - ForegroundColor Cyan
257341 exit
258342 }
0 commit comments