network-chhange-scripts/ip-settings.ps1
2026-02-20 11:22:11 +00:00

231 lines
9.4 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 }
function Write-Warn($msg) { Write-Host " [!!] $msg" -ForegroundColor DarkYellow }
function Test-IPInUse($ip) {
Write-Info "Checking if $ip is already in use on the network..."
$ping = New-Object System.Net.NetworkInformation.Ping
$result = $ping.Send($ip, 1000)
return ($result.Status -eq "Success")
}
# 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 - Show current config
Write-Host ""
Write-Host " Current Configuration:" -ForegroundColor Magenta
Write-Host " ----------------------------------------" -ForegroundColor DarkGray
$curIP = Get-NetIPAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue
$curIface = Get-NetIPInterface -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue
$curRoute = Get-NetRoute -InterfaceIndex $nicIdx -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue
$curDns = Get-DnsClientServerAddress -InterfaceIndex $nicIdx -AddressFamily IPv4 -ErrorAction SilentlyContinue
$curMode = if ($curIface -and $curIface.Dhcp -eq "Enabled") { "DHCP (Automatic)" } else { "Static" }
$curIPStr = if ($curIP) { "$($curIP.IPAddress)/$($curIP.PrefixLength)" } else { "Not assigned" }
$curGWStr = if ($curRoute) { $curRoute.NextHop } else { "None" }
$curDnsStr = if ($curDns -and $curDns.ServerAddresses) { $curDns.ServerAddresses -join " / " } else { "Automatic" }
Write-Host (" Mode : {0}" -f $curMode) -ForegroundColor White
Write-Host (" IP : {0}" -f $curIPStr) -ForegroundColor White
Write-Host (" Gateway : {0}" -f $curGWStr) -ForegroundColor White
Write-Host (" DNS : {0}" -f $curDnsStr) -ForegroundColor White
Write-Host " ----------------------------------------" -ForegroundColor DarkGray
# STEP 4 - 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 " [4] DHCP (automatic) DNS: 192.168.178.10 / 192.168.178.14" -ForegroundColor White
Write-Host ""
$profileChoice = Read-Host " Enter profile number"
# STEP 5 - Confirmation
$profileNames = @{ "1" = "Camera Control -> 192.168.178.22 /24"; "2" = "LAN Profile 2 -> 192.168.1.222 /24"; "3" = "DHCP (automatic)"; "4" = "DHCP + custom DNS" }
$chosenName = if ($profileNames.ContainsKey($profileChoice)) { $profileNames[$profileChoice] } else { "Unknown" }
Write-Host ""
Write-Host " You are about to apply:" -ForegroundColor Yellow
Write-Host " Profile : $chosenName" -ForegroundColor Yellow
Write-Host " Adapter : $nicName" -ForegroundColor Yellow
Write-Host ""
$confirm = Read-Host " Confirm? (Y/N)"
if ($confirm -notmatch "^[Yy]$") {
Write-Host ""
Write-Info "Cancelled. No changes were made."
Write-Host ""
Read-Host "Press Enter to exit"
exit 0
}
# STEP 6 - 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"
if (Test-IPInUse $newIP) {
Write-Host ""
Write-Warn "WARNING: $newIP is already responding on the network!"
Write-Warn "Another device may be using this IP address."
Write-Host ""
$forceApply = Read-Host " Apply anyway? (Y/N)"
if ($forceApply -notmatch "^[Yy]$") {
Write-Info "Cancelled. No changes were made."
Read-Host "Press Enter to exit"
exit 0
}
} else {
Write-OK "$newIP is free - no conflict detected"
}
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"
if (Test-IPInUse $newIP) {
Write-Host ""
Write-Warn "WARNING: $newIP is already responding on the network!"
Write-Warn "Another device may be using this IP address."
Write-Host ""
$forceApply = Read-Host " Apply anyway? (Y/N)"
if ($forceApply -notmatch "^[Yy]$") {
Write-Info "Cancelled. No changes were made."
Read-Host "Press Enter to exit"
exit 0
}
} else {
Write-OK "$newIP is free - no conflict detected"
}
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"
}
elseif ($profileChoice -eq "4") {
$dns1 = "192.168.178.10"
$dns2 = "192.168.178.14"
Set-NetIPInterface -InterfaceIndex $nicIdx -Dhcp Enabled
Set-DnsClientServerAddress -InterfaceIndex $nicIdx -ServerAddresses ($dns1, $dns2) | Out-Null
Write-OK "Adapter '$nicName' switched to DHCP with custom DNS"
Write-OK " IP : Automatic"
Write-OK " DNS 1 : $dns1"
Write-OK " DNS 2 : $dns2"
}
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"