Functions/Set-IPAddress.ps1
function Set-IPAddress { [CmdletBinding()] param ( [Parameter(Mandatory, ParameterSetName = 'IP')] [string] $IP, [Parameter(Mandatory, ParameterSetName = 'IP')] [string] $Prefix, [Parameter(Mandatory, ParameterSetName = 'IP')] [string] $Gateway, [Parameter(Mandatory, ParameterSetName = 'IP')] [string[]] $DNSServers, [Parameter(ParameterSetName = 'Dynamic')] [switch] $Dynamic, [Parameter()] [int] $IfIndex ) if (-not($IfIndex)) { if ((Get-NetAdapter).count -gt 1) { Get-NetAdapter Write-Host "Choose interface index" -ForegroundColor Yellow $IfIndex = Read-Host -Prompt "Choose interface index" if (Get-NetAdapter -ifIndex $IfIndex -ea 0) { } else { Write-Host "Interface with index $IfIndex does not exist" -ForegroundColor Red Remove-Variable IfIndex } } else { $IfIndex = (Get-NetAdapter).InterfaceIndex } } if ($IfIndex) { if ($Dynamic) { Remove-NetIPAddress -InterfaceIndex $IfIndex -Confirm:$false Remove-NetRoute -ifIndex $IfIndex -Confirm:$false Set-DnsClientServerAddress -ResetServerAddresses -InterfaceIndex $IfIndex Set-NetIPInterface -ifIndex $IfIndex -Dhcp Enabled ipconfig.exe /renew } else { Remove-NetIPAddress -InterfaceIndex $IfIndex -Confirm:$false Remove-NetRoute -ifIndex $IfIndex -Confirm:$false New-NetIPAddress -IPAddress $IP -DefaultGateway $Gateway -PrefixLength $Prefix -InterfaceIndex $IfIndex Set-DnsClientServerAddress -ServerAddresses $DNSServers -InterfaceIndex $IfIndex } } } |