network-chhange-scripts/ip-settings.ps1
2026-02-20 10:42:17 +00:00

116 lines
4.9 KiB
PowerShell

# 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 ""
Read-Host "Press Enter to exit"