Update ip-settings.ps1

This commit is contained in:
Dejan 2026-02-20 10:46:15 +00:00
parent 2656e382cc
commit 95c220fdd7

View file

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