Convert Dynamic IP to static IP in a Windows computer or server
Make sure the Powershell should be opened with elevated privilege
$Domain = read-host "Enter the domain name for the DNS Suffix "
#Collect Network Configuration variables
$IfName = (Get-NetIPInterface -AddressFamily IPv4 -Dhcp Enabled).InterfaceAlias
$IfIndex = (Get-NetIPInterface -AddressFamily IPv4 -Dhcp Enabled).ifIndex
$IP = (Get-NetIPConfiguration -InterfaceIndex $IfIndex | Select-Object -ExpandProperty IPv4Address).IPv4Address
Start-Sleep -Seconds 20
Write-host "IP Address set as: $IP."
$Gateway = (Get-NetIPConfiguration -InterfaceIndex $IfIndex | Select-Object -ExpandProperty IPv4DefaultGateway).NextHop
Write-host "Gateway set as: $Gateway."
$PrefixLength = Get-NetIPAddress -InterfaceIndex $IfIndex | Select-Object -ExpandProperty PrefixLength
$DNSServers = @()
$DNS = @()
$DNS = Get-DnsClientServerAddress -InterfaceIndex $IfIndex | Select-Object -ExpandProperty ServerAddresses
foreach($item in $DNS)
{
$DNSServers += $item
}
Write-host "Start-WindowsAMI - Preparing to modify IP configuration."
#remove existing IP configuration, then reconfigure new static IP configuration
Try
{
Remove-NetIPAddress -IPAddress $IP -DefaultGateway $Gateway -Confirm:$false -ErrorAction STOP
New-NetIPAddress -InterfaceIndex $IfIndex -IPAddress $IP -PrefixLength 22 -DefaultGateway $Gateway -Confirm:$false -ErrorAction STOP
Start-Sleep -Seconds 10
Set-DnsClientServerAddress -InterfaceIndex $IfIndex -ServerAddresses $DNSServers -Confirm:$false -ErrorAction STOP
Set-NetAdapterBinding -Name $IfName -DisplayName "Internet Protocol Version 6 (TCP/IPv6)" -Enabled:$false -Confirm:$false -ErrorAction STOP
Write-host "Start-WindowsAMI - IP modifications complete."
}
catch
{
Write-host "Start-WindowsAMI - Failed IP Modifications.`r`n$_"
}
# Setting DNS Address prefix
Try
{
Set-DnsClientGlobalSetting -SuffixSearchList @($Domain)
Set-DnsClient -InterfaceIndex $IfIndex -ConnectionSpecificSuffix $Domain
Write-host "Start-WindowsAMI - DNS Prefix complete"
}
catch
{
Write-host "Start-WindowsAMI - Failed DNS Prefix"
}
Comments
Post a Comment