diff --git a/ip-settings.ps1 b/ip-settings.ps1 index 032456c..55cb01b 100644 --- a/ip-settings.ps1 +++ b/ip-settings.ps1 @@ -1,116 +1,141 @@ -# ip-settings.ps1 - Run as Administrator - -function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } -function Write-Info($msg) { Write-Host " [..] $msg" -ForegroundColor Yellow } -function Write-Err($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } - -# Admin check - no line continuation -$id = [Security.Principal.WindowsIdentity]::GetCurrent() -$wp = New-Object Security.Principal.WindowsPrincipal($id) -$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator -if (-not $wp.IsInRole($adminRole)) { - Write-Err "Please run this script as Administrator!" - Read-Host "Press Enter to exit" - exit 1 -} - -Clear-Host -Write-Host "============================================" -ForegroundColor Cyan -Write-Host " LAN Network Profile Configurator " -ForegroundColor Cyan -Write-Host "============================================" -ForegroundColor Cyan - -# STEP 1 - List adapters -Write-Info "Scanning network adapters..." -$adapters = Get-NetAdapter | Where-Object { $_.HardwareInterface -eq $true } | Sort-Object InterfaceIndex - -if ($adapters.Count -eq 0) { - Write-Err "No network adapters found." - Read-Host "Press Enter to exit" - exit 1 -} - -Write-Host "" -Write-Host " Available Network Adapters:" -ForegroundColor White -Write-Host "" - -$i = 1 -$adapterList = @() -foreach ($a in $adapters) { - $ipInfo = Get-NetIPAddress -InterfaceIndex $a.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue - $iface = Get-NetIPInterface -InterfaceIndex $a.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue - $currentIP = if ($ipInfo) { $ipInfo.IPAddress } else { "No IP" } - $dhcpStatus = if ($iface -and $iface.Dhcp -eq "Enabled") { "DHCP" } else { "Static" } - $col = if ($a.Status -eq "Up") { "Green" } else { "DarkGray" } - Write-Host (" [{0}] {1,-35} Status: {2,-12} Mode: {3,-8} IP: {4}" -f $i, $a.Name, $a.Status, $dhcpStatus, $currentIP) -ForegroundColor $col - $adapterList += [PSCustomObject]@{ Index = $i; Adapter = $a } - $i++ -} - -# STEP 2 - Pick adapter -Write-Host "" -$adapterChoice = Read-Host " Enter adapter number" -$selected = $adapterList | Where-Object { $_.Index -eq [int]$adapterChoice } -if (-not $selected) { - Write-Err "Invalid selection." - Read-Host "Press Enter to exit" - exit 1 -} - -$nicName = $selected.Adapter.Name -$nicIdx = $selected.Adapter.InterfaceIndex -Write-OK "Selected: $nicName (Index $nicIdx)" - -# STEP 3 - Choose profile -Write-Host "" -Write-Host " Profiles:" -ForegroundColor White -Write-Host " [1] Camera Control -> 192.168.178.22 /24 GW: 192.168.178.1" -ForegroundColor White -Write-Host " [2] LAN Profile 2 -> 192.168.1.222 /24 GW: 192.168.1.1" -ForegroundColor White -Write-Host " [3] DHCP (automatic)" -ForegroundColor White -Write-Host "" -$profileChoice = Read-Host " Enter profile number" - -# STEP 4 - Apply -Write-Info "Applying settings..." - -if ($profileChoice -eq "1") { - $newIP = "192.168.178.22"; $prefix = 24; $gateway = "192.168.178.1" - Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue - Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue - New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null - Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ServerAddresses $gateway | Out-Null - Write-OK "Camera Control applied -> $newIP/$prefix GW: $gateway" -} -elseif ($profileChoice -eq "2") { - $newIP = "192.168.1.222"; $prefix = 24; $gateway = "192.168.1.1" - Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue - Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue - New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null - Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ServerAddresses $gateway | Out-Null - Write-OK "LAN Profile 2 applied -> $newIP/$prefix GW: $gateway" -} -elseif ($profileChoice -eq "3") { - Set-NetIPInterface -InterfaceIndex $nicIdx -Dhcp Enabled - Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ResetServerAddresses - Write-OK "Adapter '$nicName' switched to DHCP." -} -else { - Write-Err "Invalid profile choice. No changes made." - Read-Host "Press Enter to exit" - exit 1 -} - -# STEP 5 - Verify -Write-Info "Verifying new config..." -Start-Sleep -Seconds 2 -$verify = Get-NetIPAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue -if ($verify) { - Write-Host "" - Write-Host " Current IP : $($verify.IPAddress)/$($verify.PrefixLength)" -ForegroundColor Green -} else { - Write-Info "No IP yet (normal for DHCP)" -} - -Write-Host "" -Write-Host " Done!" -ForegroundColor Cyan -Write-Host "" +# ip-settings.ps1 - Run as Administrator + +function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } +function Write-Info($msg) { Write-Host " [..] $msg" -ForegroundColor Yellow } +function Write-Err($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } + +# Admin check +$id = [Security.Principal.WindowsIdentity]::GetCurrent() +$wp = New-Object Security.Principal.WindowsPrincipal($id) +$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator +if (-not $wp.IsInRole($adminRole)) { + Write-Err "Please run this script as Administrator!" + Read-Host "Press Enter to exit" + exit 1 +} + +Clear-Host +Write-Host "============================================" -ForegroundColor Cyan +Write-Host " LAN Network Profile Configurator " -ForegroundColor Cyan +Write-Host "============================================" -ForegroundColor Cyan + +# STEP 1 - List adapters +Write-Info "Scanning network adapters..." +$adapters = Get-NetAdapter | Where-Object { $_.HardwareInterface -eq $true } | Sort-Object InterfaceIndex + +if ($adapters.Count -eq 0) { + Write-Err "No network adapters found." + Read-Host "Press Enter to exit" + exit 1 +} + +Write-Host "" +Write-Host " Available Network Adapters:" -ForegroundColor White +Write-Host "" + +$i = 1 +$adapterList = @() +foreach ($a in $adapters) { + $ipInfo = Get-NetIPAddress -InterfaceIndex $a.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue + $iface = Get-NetIPInterface -InterfaceIndex $a.InterfaceIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue + $currentIP = if ($ipInfo) { $ipInfo.IPAddress } else { "No IP" } + $dhcpStatus = if ($iface -and $iface.Dhcp -eq "Enabled") { "DHCP" } else { "Static" } + $col = if ($a.Status -eq "Up") { "Green" } else { "DarkGray" } + Write-Host (" [{0}] {1,-35} Status: {2,-12} Mode: {3,-8} IP: {4}" -f $i, $a.Name, $a.Status, $dhcpStatus, $currentIP) -ForegroundColor $col + $adapterList += [PSCustomObject]@{ Index = $i; Adapter = $a } + $i++ +} + +# STEP 2 - Pick adapter +Write-Host "" +$adapterChoice = Read-Host " Enter adapter number" +$selected = $adapterList | Where-Object { $_.Index -eq [int]$adapterChoice } +if (-not $selected) { + Write-Err "Invalid selection." + Read-Host "Press Enter to exit" + exit 1 +} + +$nicName = $selected.Adapter.Name +$nicIdx = $selected.Adapter.InterfaceIndex +Write-OK "Selected: $nicName (Index $nicIdx)" + +# STEP 3 - Choose profile +Write-Host "" +Write-Host " Profiles:" -ForegroundColor White +Write-Host " [1] Camera Control -> 192.168.178.22 /24 GW: 192.168.178.1 DNS: 192.168.178.10 / 192.168.178.14" -ForegroundColor White +Write-Host " [2] LAN Profile 2 -> 192.168.1.222 /24 GW: 192.168.1.1 DNS: Automatic" -ForegroundColor White +Write-Host " [3] DHCP (automatic) DNS: Automatic" -ForegroundColor White +Write-Host "" +$profileChoice = Read-Host " Enter profile number" + +# STEP 4 - Apply +Write-Info "Applying settings..." + +if ($profileChoice -eq "1") { + $newIP = "192.168.178.22" + $prefix = 24 + $gateway = "192.168.178.1" + $dns1 = "192.168.178.10" + $dns2 = "192.168.178.14" + + Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue + Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue + New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null + Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ServerAddresses ($dns1, $dns2) | Out-Null + + Write-OK "Camera Control applied" + Write-OK " IP : $newIP/$prefix" + Write-OK " Gateway : $gateway" + Write-OK " DNS 1 : $dns1" + Write-OK " DNS 2 : $dns2" +} +elseif ($profileChoice -eq "2") { + $newIP = "192.168.1.222" + $prefix = 24 + $gateway = "192.168.1.1" + + Remove-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue + Remove-NetRoute -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue + New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null + Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ResetServerAddresses | Out-Null + + Write-OK "LAN Profile 2 applied" + Write-OK " IP : $newIP/$prefix" + Write-OK " Gateway : $gateway" + Write-OK " DNS : Automatic" +} +elseif ($profileChoice -eq "3") { + Set-NetIPInterface -InterfaceIndex $nicIdx -Dhcp Enabled + Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ResetServerAddresses + + Write-OK "Adapter '$nicName' switched to DHCP" + Write-OK " IP : Automatic" + Write-OK " DNS : Automatic" +} +else { + Write-Err "Invalid profile choice. No changes made." + Read-Host "Press Enter to exit" + exit 1 +} + +# STEP 5 - Verify +Write-Info "Verifying new config..." +Start-Sleep -Seconds 2 +$verify = Get-NetIPAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue +$dnsVerify = Get-DnsClientServerAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue + +Write-Host "" +if ($verify) { + Write-Host " IP : $($verify.IPAddress)/$($verify.PrefixLength)" -ForegroundColor Green +} +if ($dnsVerify -and $dnsVerify.ServerAddresses) { + Write-Host " DNS : $($dnsVerify.ServerAddresses -join ' / ')" -ForegroundColor Green +} else { + Write-Host " DNS : Automatic" -ForegroundColor Green +} + +Write-Host "" +Write-Host " Done!" -ForegroundColor Cyan +Write-Host "" Read-Host "Press Enter to exit" \ No newline at end of file