Update ip-settings.ps1

This commit is contained in:
Dejan 2026-02-20 11:22:11 +00:00
parent e79773a42f
commit d8217d16a6

View file

@ -3,6 +3,14 @@
function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green } function Write-OK($msg) { Write-Host " [OK] $msg" -ForegroundColor Green }
function Write-Info($msg) { Write-Host " [..] $msg" -ForegroundColor Yellow } function Write-Info($msg) { Write-Host " [..] $msg" -ForegroundColor Yellow }
function Write-Err($msg) { Write-Host " [!!] $msg" -ForegroundColor Red } 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 # Admin check
$id = [Security.Principal.WindowsIdentity]::GetCurrent() $id = [Security.Principal.WindowsIdentity]::GetCurrent()
@ -60,7 +68,28 @@ $nicName = $selected.Adapter.Name
$nicIdx = $selected.Adapter.InterfaceIndex $nicIdx = $selected.Adapter.InterfaceIndex
Write-OK "Selected: $nicName (Index $nicIdx)" Write-OK "Selected: $nicName (Index $nicIdx)"
# STEP 3 - Choose profile # 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 ""
Write-Host " Profiles:" -ForegroundColor White 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 " [1] Camera Control -> 192.168.178.22 /24 GW: 192.168.178.1 DNS: 192.168.178.10 / 192.168.178.14" -ForegroundColor White
@ -70,7 +99,25 @@ Write-Host " [4] DHCP (automatic)
Write-Host "" Write-Host ""
$profileChoice = Read-Host " Enter profile number" $profileChoice = Read-Host " Enter profile number"
# STEP 4 - Apply # 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..." Write-Info "Applying settings..."
if ($profileChoice -eq "1") { if ($profileChoice -eq "1") {
@ -80,6 +127,21 @@ if ($profileChoice -eq "1") {
$dns1 = "192.168.178.10" $dns1 = "192.168.178.10"
$dns2 = "192.168.178.14" $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-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue
Remove-NetRoute -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 New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null
@ -96,6 +158,21 @@ elseif ($profileChoice -eq "2") {
$prefix = 24 $prefix = 24
$gateway = "192.168.1.1" $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-NetIPAddress -InterfaceIndex $nicIdx -Confirm:$false -ErrorAction SilentlyContinue
Remove-NetRoute -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 New-NetIPAddress -InterfaceIndex $nicIdx -IPAddress $newIP -PrefixLength $prefix -DefaultGateway $gateway | Out-Null