96 lines
3.7 KiB
PowerShell
96 lines
3.7 KiB
PowerShell
param(
|
|
[ValidateSet('DHCP','STATIC','INFO')] [string]$Mode,
|
|
[string]$Interface = 'Ethernet',
|
|
[string]$IP,
|
|
[string]$Mask,
|
|
[string]$Gateway,
|
|
[string[]]$Dns
|
|
)
|
|
|
|
function MaskToPrefix($m){
|
|
$b = ($m -split '\.') | ForEach-Object {[Convert]::ToString([int]$_,2).PadLeft(8,'0')}
|
|
($b -join '').ToCharArray() | Where-Object {$_ -eq '1'} | Measure-Object | Select-Object -ExpandProperty Count
|
|
}
|
|
|
|
function EnsureInterface($alias){
|
|
$a = Get-NetAdapter -InterfaceAlias $alias -ErrorAction SilentlyContinue
|
|
if(-not $a){ throw "Interface not found: $alias" }
|
|
}
|
|
|
|
function ConfigureDhcp($alias){
|
|
EnsureInterface $alias
|
|
Set-NetIPInterface -InterfaceAlias $alias -AddressFamily IPv4 -Dhcp Enabled
|
|
Set-DnsClientServerAddress -InterfaceAlias $alias -ResetServerAddresses
|
|
ipconfig /flushdns | Out-Null
|
|
}
|
|
|
|
function ConfigureStatic($alias,$ip,$mask,$gw,$dns){
|
|
EnsureInterface $alias
|
|
$prefix = MaskToPrefix $mask
|
|
Get-NetIPConfiguration -InterfaceAlias $alias | Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | Remove-NetIPAddress -Confirm:$false -ErrorAction SilentlyContinue
|
|
Get-NetIPConfiguration -InterfaceAlias $alias | Get-DnsClientServerAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | ForEach-Object { Set-DnsClientServerAddress -InterfaceAlias $alias -ResetServerAddresses }
|
|
New-NetIPAddress -InterfaceAlias $alias -IPAddress $ip -PrefixLength $prefix -DefaultGateway $gw | Out-Null
|
|
if($dns){ Set-DnsClientServerAddress -InterfaceAlias $alias -ServerAddresses $dns }
|
|
ipconfig /flushdns | Out-Null
|
|
}
|
|
|
|
function ShowNicInfo($alias){
|
|
$na = Get-NetAdapter -InterfaceAlias $alias -ErrorAction SilentlyContinue
|
|
if(-not $na){ throw "Interface not found: $alias" }
|
|
$adv = Get-NetAdapterAdvancedProperty -InterfaceDescription $na.InterfaceDescription -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -match 'Duplex|Speed' }
|
|
[pscustomobject]@{
|
|
Name = $na.Name
|
|
InterfaceAlias = $na.InterfaceAlias
|
|
InterfaceDescription = $na.InterfaceDescription
|
|
Status = $na.Status
|
|
MediaConnection = $na.MediaConnectionState
|
|
LinkSpeed = $na.LinkSpeed
|
|
DuplexSetting = ($adv | Where-Object { $_.DisplayName -match 'Duplex' } | Select-Object -First 1 -ExpandProperty DisplayValue)
|
|
}
|
|
}
|
|
|
|
if(-not $Mode){
|
|
Write-Host "1) DHCP 2) STATIC 3) INFO"
|
|
$sel = Read-Host "Select mode [1/2/3]"
|
|
if($sel -eq '1'){ $Mode='DHCP' }
|
|
elseif($sel -eq '2'){ $Mode='STATIC' }
|
|
elseif($sel -eq '3'){ $Mode='INFO' }
|
|
}
|
|
|
|
if($Mode -eq 'DHCP'){
|
|
if(-not $PSBoundParameters.ContainsKey('Interface')){
|
|
$Interface = Read-Host "Interface alias (default: Ethernet)"
|
|
if([string]::IsNullOrWhiteSpace($Interface)){ $Interface='Ethernet' }
|
|
}
|
|
ConfigureDhcp -alias $Interface
|
|
Get-NetIPConfiguration -InterfaceAlias $Interface
|
|
exit
|
|
}
|
|
|
|
if($Mode -eq 'STATIC'){
|
|
if(-not $PSBoundParameters.ContainsKey('Interface')){
|
|
$Interface = Read-Host "Interface alias (default: Ethernet)"
|
|
if([string]::IsNullOrWhiteSpace($Interface)){ $Interface='Ethernet' }
|
|
}
|
|
if(-not $IP){ $IP = Read-Host "IPv4 address" }
|
|
if(-not $Mask){ $Mask = Read-Host "Subnet mask" }
|
|
if(-not $Gateway){ $Gateway = Read-Host "Gateway" }
|
|
if(-not $Dns){
|
|
$DnsStr = Read-Host "DNS (comma-separated, optional)"
|
|
if($DnsStr){ $Dns = $DnsStr -split '\s*,\s*' }
|
|
}
|
|
ConfigureStatic -alias $Interface -ip $IP -mask $Mask -gw $Gateway -dns $Dns
|
|
Get-NetIPConfiguration -InterfaceAlias $Interface
|
|
exit
|
|
}
|
|
|
|
if($Mode -eq 'INFO'){
|
|
if(-not $PSBoundParameters.ContainsKey('Interface')){
|
|
$Interface = Read-Host "Interface alias (default: Ethernet)"
|
|
if([string]::IsNullOrWhiteSpace($Interface)){ $Interface='Ethernet' }
|
|
}
|
|
ShowNicInfo -alias $Interface | Format-List
|
|
exit
|
|
}
|
|
|